编写循环和条件任务
利用循环迭代任务
通过利用循环,我们无需编写多个使用同一模块的任务。例如,他们不必编写五个任务来确保存在五个用户,而是只需编写一个任务来对含有五个用户的列表迭代,从而确保它们都存在。
Ansible支持使用loop关键字对一组项目迭代任务。可以配置循环以利用列表中的各个项目、列表中各个文件的内容、生成的数字序列或更为复杂的结构来重复任务
---
- hosts: 192.168.194.138
tasks:
- name: install
yum:
name: "{{ item }}"
state: present
loop:
- httpd
[root@localhost ~]# ansible-playbook wh.yml
PLAY [192.168.194.138] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.194.138]
TASK [install] *****************************************************************
changed: [192.168.194.138] => (item=httpd)
PLAY RECAP *********************************************************************
192.168.194.138 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost ~]# ansible-playbook wh.yml
PLAY [192.168.194.138] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.194.138]
TASK [install] *****************************************************************
ok: [192.168.194.138] => (item=httpd)
PLAY RECAP *********************************************************************
192.168.194.138 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在守管主机上查看
[root@localhost ~]# rpm -qa| grep httpd
httpd-2.4.6-93.el7.centos.x86_64
httpd-tools-2.4.6-93.el7.centos.x86_64
当使用循环变量item编写:
---
- hosts: all
tasks:
- name: ssthdcjs
service:
name: "{{ item }}"
state: started
loop:
- postfix
- crond
[root@localhost ~]# ansible-playbook ww.yml
PLAY [192.168.194.139] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.194.139]
TASK [ssthdcjs] ****************************************************************
ok: [192.168.194.139] => (item=postfix)
ok: [192.168.194.139] => (item=crond)
PLAY RECAP *********************************************************************
192.168.194.139 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
可以测试的一个最简单条件是某一布尔变量是True还是False。以下示例中的when语句导致任务仅在run_my_task为True时运行
使用布尔值测试
---
- hosts: all
vars:
my_condition: True
tasks:
- name: Ensyre the httpd is installed
yum:
name: httpd
statee: present
when: my_condition
操作 | 示例 |
---|---|
等于(值为字符串) | ansible_machine == “x86_64” |
等于(值为数字) | max_memory == 512 |
小于 | min_memory < 128 |
大于 | min_memory > 256 |
小于等于 | min_memory <= 256 |
大于等于 | min_memory >= 512 |
不等于 | min_memory != 512 |
变量存在 | min_memory is defined |
变量不存在 | min_memory is not defined |
布尔变量是True。1、True或yes的求值为True | memory_available |
布尔变量是False。0、False或no的求值为False | not memory_available |
第一个变量的值存在,作为第二个变量的列表中的值 | ansible_distribution in supported_distros |
测试多个条件
一个when语句可用于评估多个条件。使用and和or关键字组合条件,并使用括号分组条件
使用or语句时则任意一个条件为真时,则执行相应任务
[root@localhost ~]# vim jjj.yml
---
- hosts: 192.168.194.139
vars:
my_arch:
- RedHat
- CentOS
tasks:
- name: jdgvuid
yum:
name: httpd
state: present
when: ansible_facts['distribution'] in my_arc
[root@localhost ~]# ansible-playbook jjj.yml
PLAY [192.168.194.139] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.194.139]
TASK [jdgvuid] *****************************************************************
ok: [192.168.194.139]
PLAY RECAP *********************************************************************
192.168.194.139 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost ~]# ansible-playbook --syntax-check id.yml
playbook: id.yml
---
- hosts: 192.168.194.138
tasks:
- name: hudjkh
yum:
name: httpd
state: prsent
when: ansible_facts['distrbution'] 'RedHat' and ansible_facts['distribution_version'] == '7.5'
---
- hosts: all
tasks:
- name: hudjkh
yum:
name: httpd
state: absent
when:
- ansible_facts['distrbution'] 'RedHat'
- ansible_facts['distrbution_version']== '7.4'
2.组合循环和有条件任务
循环和条件可以组合使用
[root@localhost ~]# vim i.yml
---
- hosts: 192.168.194.139
tasks:
- name: hjkdf
yum:
name: httpd
state: absent
loop: "{{ ansible_facts['mounts'] }}"
when:
- item.mount == '/'
- item.size_available > 92760410
~
[root@localhost ~]# ansible-playbook i.yml
PLAY [192.168.194.139] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.194.139]
TASK [hjkdf] *******************************************************************
skipping: [192.168.194.139] => (item={'block_used': 33118, 'uuid': '04dd9450-994d-46e3-97d0-4e8dc836e571', 'size_total': 1063256064, 'block_total': 259584, 'mount': '/boot', 'block_available': 226466, 'size_available': 927604736, 'fstype': 'xfs', 'inode_total': 524288, 'options': 'rw,seclabel,relatime,attr2,inode64,noquota', 'device': '/dev/sda1', 'inode_used': 326, 'block_size': 4096, 'inode_available': 523962})
ok: [192.168.194.139] => (item={'block_used': 279995, 'uuid': '6e8789eb-cce5-4dfc-b585-f60a17a5e0f5', 'size_total': 18238930944, 'block_total': 4452864, 'mount': '/', 'block_available': 4172869, 'size_available': 17092071424, 'fstype': 'xfs', 'inode_total': 8910848, 'options': 'rw,seclabel,relatime,attr2,inode64,noquota', 'device': '/dev/mapper/centos-root', 'inode_used': 27062, 'block_size': 4096, 'inode_available': 8883786})
PLAY RECAP *********************************************************************
192.168.194.139 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible处理程序
Ansible模块设计为具有幂等性。这表示,在正确编写的playbook中,playbook及其任务可以运行多次而不会改变受管主机,除非需要进行更改使受管主机进入所需的状态
[root@localhost ~]# vim bu.yml
- name: provides config
template:
src: files/httpd.conf
dest: /etc/httpd/conf/httpd.conf
notify:
- restart apache
- name: start service
service:
name: httpd
state: started
handlers:
- name: restart apche
service:
name: httpd
state: restarted
[root@localhost ~]# ansible-playbook --syntax-check bu.yml
playbook: bu.yml
[root@localhost ~]# ansible-playbook bu.yml
PLAY [192.168.194.139] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.194.139]
TASK [installed httpd] *********************************************************
changed: [192.168.194.139]
TASK [provides config] *********************************************************
fatal: [192.168.194.139]: FAILED! => {"changed": false, "msg": "Could not find or access 'files/httpd.conf'\nSearched in:\n\t/root/templates/files/httpd.conf\n\t/root/files/httpd.conf\n\t/root/templates/files/httpd.conf\n\t/root/files/httpd.conf on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote_src option"}