一.循环
循环迭代任务
1.简单循环
loop:
- value1 ##赋值列表
- value2
- ...
{{item}} ##迭代变量名称
实例:
---
- name: create file
hosts: 172.25.0.254
tasks:
- name: file module
file:
name: /mnt/{{item}}
state: present
loop:
- westos_file1
- westos_file2
2.循环散列或字典列表
---
- name: create file
hosts: 172.25.0.254
tasks:
- name: file module
service:
name: "{{ item.name}}"
state: "{{ item.state }}"
loop:
- name: httpd
state: started
- name: vsftpd
state: stopped
练习:
http://172.25.254.250/RHCE_EXAM/files/hwreport.empty
创建一个名为/home/devops/.ansible/hwreport.yaml 的playbook 它将在所在所有受管节点上含有以下信息的输出文件 /mnt/hwreport.txt 清单主机名称
以MB表示总内存大小
BIOS 版本
磁盘设备 vda 的大小
磁盘设备vdb的大小
输出文件中的每一行含有一个 key=value 对
将它保存到/mnt/hwreport.txt
使用正确的值改为 /mnt/hwreport.txt
如果硬件不存在,相关的值设为NONE
[devops@ansible .ansible]$ cat hwreport.yaml
- name: hwreport
hosts: westos
tasks:
- name: download
get_url:
url: http://172.25.254.250/RHCE_EXAM/files/hwreport.empty
dest: /mnt/hwreport.txt
force: yes
- name: check device
lineinfile:
path: /mnt/hwreport.txt
regexp: "{{item.start}}"
line: "{{item.start}}={{item.reline}}"
loop:
- name: HOST
reline: "HOST={{ansible_facts['fqdn']}}"
- name: MEMERY
reline: "{{ansible_facts['memtotal_mb']}}"
- name: BIOS
reline: "{{ansible_facts['bios_version']}}"
- name: DISK_SIZE_VDA
reline: "{{ansible_facts['devices']['vda']['size'] | default ('NONE')}}"
- name: DISK_SIZE_VDB
reline: "{{ansible_facts['devices']['vdb']['size'] | default ('NONE')}}"
[devops@ansible .ansible]$ ansible-playbook hwreport.yaml
在受控主机中查看:
[root@node1 mnt]# cat hwreport.txt
# Hardware report
HOST=node1.westos.org
MEMERY=818
BIOS=1.11.1-4.module+el8.1.0+4066+0f1aadab
DISK_SIZE_VDA=20.00 GB
DISK_SIZE_VDB=7.86 GB
[root@node1 mnt]#
二.条件
when:
- 条件1
- 条件2
条件判断:
= value == "字符串",value == 数字
< value < 数字
> value > 数字
<= value <= 数字
>= value >= 数字
!= value != 数字
is defined value value is defined 变量存在
is not defined value is not defined 变量不存在
in value is in value 变量为
not in value is not in value 变量不为
bool变量为true value value的值为true
bool变量为false not value value的值为false
value in value2 value的值在value2列表中
[devops@ansible .ansible]$ cat checkfile.yaml
- name: check file
hosts: westos
tasks:
- name: check file
shell: test -e /mnt/test
register: checkout
ignore_errors: yes
- name: debug
debug:
msg: "/mnt/test is not exist"
when: checkout.rc != 0
- name: debug
debug:
msg: "/mnt/test is exist"
when: checkout.rc == 0
[devops@ansible .ansible]$ ansible-playbook checkfile.yaml
[devops@ansible .ansible]$ cat checkfile.yaml
- name: check file
hosts: all
tasks:
- name: check file
shell: test -e /mnt/test
register: checkout
ignore_errors: yes
when: inventory_hostname in groups.westos
- name: debug
debug:
msg: "/mnt/test is not exist"
when:
- inventory_hostname in groups.westos
- checkout.rc != 0
- name: debug
debug:
msg: "/mnt/test is exist"
when:
- inventory_hostname in groups.westos
- checkout.rc == 0
[devops@ansible .ansible]$ ansible-playbook checkfile.yaml
[devops@ansible .ansible]$ cat checkfile.yaml
- name: check file
hosts: all
tasks:
- name: check file
shell: test -e /mnt/test
register: checkout
ignore_errors: yes
- name: debug
debug:
msg: "/mnt/test is not exist {{checkout.rc}}"
when:
- checkout.rc
- name: debug
debug:
msg: "/mnt/test is exist {{checkout.rc}}"
when:
- not checkout.rc
[devops@ansible .ansible]$ ansible-playbook checkfile.yaml
多条件组合
when:
条件1 and 条件2
- 条件1
- 条件2
when:条件1 or 条件2
when: >
条件1
or
条件2
测试题:
建立playbook ~/ansibles/lvm.yml要求如下:
*建立大小为1500M名为exam_lvm的lvm 在westos组中
*如果westos不存在请输出:
vg westos is not exist
*如果westos大小不足1500M请输出:
vg westos is less then 1500M
并建立800M大小的lvm
三.触发器
notify: 触发器当遇到更改是触发handlers
handlers: 触发器触发后执行的动作
[devops@ansible .ansible]$ vim apache.yaml
- name: configure webserver
hosts: westos
tasks:
- name: install httpd
dnf:
name: httpd
state: present
- name: configure httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 8080"
notify:
restart httpd ##当httpd.conf文件不做更改的时,不重启服务(会自动检测)
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
[devops@ansible .ansible]$ ansible-playbook apache.yaml
四.处理失败任务
1.ignore_errors
作用:当play遇到任务失败是会终止
ignore_errors: yes 将会忽略任务失败使下面的任务继续运行
[devops@ansible .ansible]$ vim apache.yaml
- name: configure webserver
hosts: westos
tasks:
- name: install httpd
dnf:
name: http
state: present
ignore_errors: yes
- name: configure httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 80"
notify:
restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
[devops@ansible .ansible]$ ansible-playbook apache.yaml
2.force_handlers
作用:当任务失败后play被终止也会调用触发器进程
[devops@ansible .ansible]$ vim apache.yaml
[devops@ansible .ansible]$ cat apache.yaml
- name: configure webserver
hosts: westos
force_handlers: yes
tasks:
- name: install httpd
dnf:
name: httpd
state: present
- name: configure httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 8080"
notify:
restart httpd
- name: install test ##error
dnf:
name: test
state: present
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
[devops@ansible .ansible]$ ansible-playbook apache.yaml
3.changed_when
作用:控制任务在何时报告它已进行更改
[devops@ansible .ansible]$ cat apache.yaml
- name: configure webserver
hosts: westos
force_handlers: yes
tasks:
- name: install httpd
dnf:
name: httpd
state: present
- name: configure httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 8080"
changed_when: yes ##yes,表示已经做了更改(真实情况可能没有更改),为no时,与真实情况一致
notify:
restart httpd
- name: install test
dnf:
name: test
state: present
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
4.failed_when
当符合条件时强制任务失败
[devops@ansible .ansible]$ vim apache.yaml
[devops@ansible .ansible]$ cat apache.yaml
- name: configure webserver
hosts: westos
force_handlers: yes
tasks:
- name: install httpd
dnf:
name: httpd
state: present
- name: configure httpd.conf
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: "^Listen"
line: "Listen 8080"
changed_when: no
failed_when: yes ##yes强制任务失败,no 和真实情况一致
notify:
restart httpd
- name: install test
dnf:
name: test
state: present
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
[devops@ansible .ansible]$ ansible-playbook apache.yaml
5.block
block: ##定义要运行的任务
rescue: ##定义当block句子中出现失败任务后运行的任务
always: ##定义最终独立运行的任务
[devops@ansible .ansible]$ cat apache.yaml
- name: configure webserver
hosts: westos
force_handlers: yes
tasks:
- name: test
block:
- name: test block
debug:
msg: block message
rescue:
- name: test block
debug:
msg: rescue message
always:
- name: test block
debug:
msg: always
~
[devops@ansible .ansible]$ ansible-playbook apache.yaml
#测试练习#
建立playbook ~/westos.yml要求如下:
建立大小为1500M名为/dev/vdb1的设备
如果/dev/vdb不存在请输入:
/dev/vdb is not exist
如果/dev/vdb大小不足2G请输出:
/dev/vdb is less then 2G
并建立800M大小的/dev/vdb1
此设备挂载到/westos上
[devops@ansible .ansible]$ cat westos.yaml
- name: create vdb1
hosts: westos
force_handlers: yes
tasks:
- name: check /dev/vdb
debug:
msg: "vdb is not exist"
when: ansible_facts['devices']['vdb'] is not defined
- name: create /dev/vdb1
block:
- name: create 1500M
parted:
device: /dev/vdb
number: 1
state: present
part_end: 1500MiB
rescue:
- name: create 800M
parted:
device: /dev/vdb
number: 1
state: present
part_end: 800MiB
- debug:
msg: vdb is less then 1500M
always:
- name: create filesystem
filesystem:
fstype: xfs
dev: /dev/vdb1
- name: mkdir /westos
file:
path: /westos
state: directory
- name: mount /dev/vdb1
mount:
path: /westos
src: /dev/vdb1
fstype: xfs
state: mounted
when: ansible_facts['devicds']['vdb'] is defined
[devops@ansible .ansible]$ ansible-playbook westos.yaml