ansible 之 ansible-playbook简单使用

原创
07/29 20:06
阅读数 117
  1. 临时命令模式

之前我们最常用的就是命令模式,也简称为“临时命令模式”。如下:

[root@chuangxindasai-prometheus ansible]# ansible ceph -m ping
172.18.0.132 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.18.0.133 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
172.18.0.131 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}

[root@chuangxindasai-prometheus ansible]# ansible ceph -m shell -a "hostname"
172.18.0.131 | CHANGED | rc=0 >>
ceph-admin
172.18.0.133 | CHANGED | rc=0 >>
ceph-node2
172.18.0.132 | CHANGED | rc=0 >>
ceph-node1

2 剧本模式

这只能做一下简单的操作,如果遇到复杂的操作就不试用了。下面记录一下简单的剧本操作

# 剧本以yaml 格式
# 该剧本里面包含了两个剧本
[root@chuangxindasai-prometheus ansible]# cat test.yml 
---
# 第一个剧本(hosts 定义主机组)
- hosts: ceph
  tasks:
  - name: install httpd
# 定义yum 模块
    yum:
      name: httpd
      state: latest

# 第二个剧本
- hosts: ceph
  tasks:
  - name: restart firewalld
    service:
      name: firewalld
      state: restarted

# 一个剧本中多个task,{{ ansible_ssh_pass }} 还调用了vars 中的参数
---
- hosts: ceph
  tasks:
  - name: install httpd {{ ansible_ssh_pass }}
    yum:
      name: httpd
      state: latest
  - name: restart firewalld
    service:
      name: firewalld
      state: restarted
  - name: command
    command: echo `hostname`

# 上面调用的参数在下面这里配置的
[root@chuangxindasai-prometheus ansible]# cat /etc/ansible/group_vars/ceph.yml
---
ansible_ssh_port: 22
ansible_ssh_user: root
ansible_ssh_pass: ccssoft

运行playbook

[root@chuangxindasai-prometheus ansible]# ansible-playbook /opt/ansible/test2.yml 

PLAY [ceph] ******************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************
ok: [172.18.0.133]
ok: [172.18.0.132]
ok: [172.18.0.131]

TASK [install httpd ccssoft] *************************************************************************************************************************************************************
ok: [172.18.0.131]
ok: [172.18.0.133]
ok: [172.18.0.132]

TASK [restart firewalld] *****************************************************************************************************************************************************************
changed: [172.18.0.132]
changed: [172.18.0.133]
changed: [172.18.0.131]

TASK [command] ***************************************************************************************************************************************************************************
changed: [172.18.0.132]
changed: [172.18.0.131]
changed: [172.18.0.133]

PLAY RECAP *******************************************************************************************************************************************************************************
172.18.0.131               : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.0.132               : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.0.133               : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

2.1 剧本模式--引用子剧本

playbook里面包含的task太多的话就很麻烦,不利于运维。include 应用而生。include 可以简单理解为把子文件中的task应用进来,而且还可以传入参数

[root@chuangxindasai-prometheus tasks]# cat ../test2.yml 
---
- hosts: ceph
  tasks:
 # 引用参数文件中的参数
  - name: install httpd {{ ansible_ssh_pass }}
    yum:
      name: httpd
      state: latest
  - name: restart firewalld
    service:
      name: firewalld
      state: restarted
  - name: command
    command: date
  # 传入子剧本中的参数test_name=wzl
  - include: tasks/test_include.yml test_name=wzl  # include 应用子yml文件,并传入参数。参数在子文件中可以调用
[root@chuangxindasai-prometheus tasks]# cat test_include.yml 
---
# 调用子剧本中传入的参数
- name: test for include {{test_name}}
  command: date

2.2 剧本模式--切换用户执行剧本

ansible剧本中也可以类似于shell中切换用户运行shell脚本。比如sudo sh  ***.sh ,下面给出案例

---
- hosts: second_node
  # 可以切换用户
  become: yes
  # 切换的用户为root,默认也是root
  become_user: root
  tasks:
  - name: copy shell script to remote node
    copy:
      src: /opt/ansible/second_node/run_node_exporter.sh
      dest: /opt/run_node_exporter.sh
      owner: root
      group: root
      mode: 0740
  - name: copy docker image to remote node
    copy:
      src: /opt/ansible/second_node/node_exporter.tar.gz
      dest: /opt/node_exporter.tar.gz
      owner: root
      group: root
      mode: 0740

  - include: ./start_docker.yml
  - include: ./check.yml

2.3  剧本模式--输出结果

    默认剧本模式中运行的脚本都只是显示运行状态,不会显示运行结果。如果想要运行结果,比如说:sh XXX.sh 脚本中输出的结果。就需要采用下面的方式:

---
- name: run the shell
  shell: "sh /opt/run_node_exporter.sh"
  # 报错输出结果到变量mon_num中
  ignore_errors: true
  register: mon_num

# debug模式下输出结果 
- name: show_check
  debug: var=mon_num verbosity=0

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部