详解ansible批量管理服务(2)

运行剧本

[root@m01 ansible_playbook]#ansible-playbook test_通知功能.yaml 

PLAY [172.16.1.41] ***********************************************************************************

TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.41]

TASK [boot server] ***********************************************************************************
ok: [172.16.1.41]

TASK [check server boot] *****************************************************************************
changed: [172.16.1.41]

TASK [debug] *****************************************************************************************
ok: [172.16.1.41] => {
 "msg": [
  "tcp  0  0 0.0.0.0:873    0.0.0.0:*    LISTEN  3708/rsync   ", 
  "tcp6  0  0 :::873     :::*     LISTEN  3708/rsync   "
 ]
}

PLAY RECAP *******************************************************************************************
172.16.1.41    : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 

1.6.3 剧本信息判断功能

nfs服务客户端三台主机
centos7 10.0.0.7、centos6 10.0.0.8、centos7 10.0.0.9
此时在批量启动的时候需要进行判断,因为centos6,centos7启动命令不一样
判断的格式
- hosts: nfs_client
tasks:
- name: boot centos7 nfs
shell: systemctl start nfs 
判断: 如果是centos7 ???
- name: boot centos6 nfs 
shell: /etc/init.d/nfs start 
判断: 如果是centos6 ???

setup模块:收集远程主机信息
语法:
[root@m01 ansible_playbook]#ansible 172.16.1.41 -m setup -a "filter=ansible_hostname"
172.16.1.41 | SUCCESS => {
 "ansible_facts": {
  "ansible_hostname": "backup", 
  "discovered_interpreter_python": "/usr/bin/python"
 }, 
 "changed": false
}

# filter 过滤 筛选

实现收集子信息的方法

问题: 获取主机信息,以及子信息

方法一:

- hosts: rsync
 tasks:
 - name: touch file
  file: path=/etc/oldboy01.txt state=touch
  when: (ansible_eth1.ipv4.address == "172.16.1.41")

方法二:

- hosts: rsync
 tasks:
 - name: touch file
  file: path=/etc/oldboy01.txt state=touch
  when: (ansible_eth1["ipv4"]["address"] == "172.16.1.41")

setup模块常用来收集的信息

根据 ip 地址进行判断创建目录

[root@m01 ansible_playbook]#vim test_判断功能.yaml
- hosts: nfs_client
 tasks:
 - name: create file for 41 host
  file: path=/tmp/172.16.1.41 state=directory
  when: (ansible_hostname == "backup")
 - name: create file for 7 host
  file: path=/tmp/172.16.1.7 state=directory
  when: (ansible_hostname == "web01")
      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1594.html