when条件语句

- name: Install vimhosts: alltasks:- name: Install VIM via yumyum:name: vim-enhancedstate: installedwhen: ansible_os_family =="RedHat"- name: Install VIM via aptapt:name: vimstate: installedwhen: ansible_os_family =="Debian"- name: Unexpected OS familydebug: msg="OS Family {{ ansible_os_family }} is not supported"when: not ansible_os_family =="RedHat" or ansible_os_family =="Debian"
- hosts: node1tasks:- name: debug memory sizedebug:msg: "{{ inventory_hostname }} memory size is {{ ansible_memtotal_mb }}"when: ansible_memtotal_mb >= 4096 #比较数值大小
条件判断
条件判断与block:
例子:
-hosts: node1tasks:-block:-name: install apacheyum:name: httpdstate: present-name: started apacheservice:name: httpdstate: startedenable: yeswhen: ansible_os_family == "RedHat"
rescue
当block执行失败时,rescue中的任务才会被执行;block除了能跟和when一起使用之外,还能进行错误处理。这个时候就需要用到rescue关键字:
-hosts: testtasks:-block:-shell: 'ls /testdir'rescue:-debug:msg: '/testdir is not exits'
always
无论block执行成功还是失败,always中的任务都会被执行
-hosts: testtasks:-block:-shell: 'ls /testdir'rescue:-debug:msg: '/testdir is not exists'always:-debug:msg: 'This task always executes'
判断变量:
defined:判断变量是否已定义,已定义则返回真
undefined:判断变量是否未定义,未定义则返回真
none:判断变量的值是否为空,如果变量已定义且值为空,则返回真
例子:
- hosts: testgather_facts: novars:testvar: "test"testvar1:tasks:- debug:msg: "testvar is defined"when: testvar is defined- debug:msg: "testvar2 is undefined"when: testvar2 is undefined- debug:msg: "testvar1 is none"when: testvar1 is none
判断文件类型:
file:判断指定路径是否为一个文件,是则为真
directory:判断指定路径是否为一个目录,是则为真
link:判断指定路径是否为一个软链接,是则为真
mount:判断指定路径是否为一个挂载点,是则为真
exists:判断指定路径是否存在,存在则为真
特别注意:关于路径的所有判断均是判断主控端上的路径,而非被控端上的路径
- hosts: testvars:testpath: /testdirtasks:- debug:msg: "file exist"when: testpath is exists- hosts: testvars:testpath: /testdir1tasks:- debug:msg: "file not exist"when: not testpath is exists
[admin@ansible ansible]$ cat when3.yml- hosts: node1vars:filepath: /home/admin/ansible/ansible.cfgtasks:- debug:msg: "the file is exists"when: filepath is exists #filepath是主控端的文件
- hosts: testgather_facts: novars:testpath1: "/testdir/test"testpath2: "/testdir"tasks:- debug:msg: "file"when: testpath1 is file- debug:msg: "directory"when: testpath2 is directory
判断执行结果:
ok:目标状态与期望值一致,没有发生变更
change或changed:目标发生变更,无论成功还是失败,都是change
sucess或succeeded:目标状态与期望值一致,或者任务执行成功
failure或failed:任务执行失败
skip或skipped:任务被跳过
- hosts: testtasks:- shell: 'cat /testdir/aaa'register: resultignore_errors: true- debug:msg: "success"when: result is success- debug:msg: "failed"when: result is failure- debug:msg: "changed"when: result is change- debug:msg: "skip"when: result is skip
判断字符串
lower:判断字符串中的所有字母是否都是小写,是则为真
upper:判断字符串中的所有字母是否都是大写,是则为真
- hosts: node1gather_facts: novars:str1: "abc"str2: "ABC"tasks:- debug:msg: "str1 is all lowercase"when: str1 is lower- debug:msg: "str2 is all uppercase"when: str2 is upper
判断整数:
even:判断数值是否为偶数,是则为真
odd:判断数值是否为奇数,是则为真
divisibleby(num):判断是否可以整除指定的数值,是则为真
- hosts: node1gather_facts: novars:num1: 6num2: 8num3: 15tasks:- debug:msg: "num1 is an even number"when: num1 is even- debug:msg: "num2 is an odd number"when: num2 is odd- debug:msg: "num3 can be divided exactly by"when: num3 is divisibleby(3)
其他条件测试:
subset 判断一个list是不是另一个list的子集:when: a is subset(b)
superset 判断一个list是不是另一个list的父集:when: b is superset(a)
in 判断一个字符串是否存在于另一个字符串中,也可用于判断某个特定的值是否存在于列表中
supported_distros:
- RedHat
- CentOS
when: ansible_distribution in supported_distros
string 判断对象是否为一个字符串,是则为真 when: var1 is string
number 判断对象是否为一个数字,是则为真 when: var3 is number
- hosts: node1vars:package:- httpd- nginxstring: abcnum: 100tasks:- name: nginx is in packagedebug:msg: "nginx is in package"when: '"nginx" in package'- debug:msg: a is in abcwhen: '"a" in string'- debug:msg: "{{ inventory_hostname }} is in web group"when: '"web" in group_names'- debug:msg: "{{ string }} is string"when: 'string is string'- debug:msg: "{{ num }} is number"when: 'num is number'
- hosts: node1tasks:- block:- name: install httpdyum:name: httpdstate: present- name: started httpdservice:name: httpdenabled: yesstate: startedrescue:- name: install nginxyum:name: nginxstate: presentalways:- name: install mariadbyum:name: mariadb*state: presentwhen: "ansible_os_family == 'RedHat'"
playbook循环语句
with_items:循环列表
- hosts: node1vars:users:- username: user11useruid: 1111- username: user22useruid: 1112- username: user33useruid: 1113tasks:- name: create useruser:name: "{{ item.username }}"uid: "{{ item.useruid }}"state: presentwith_items: "{{ users }}"- name: debug itemdebug:msg: "{{ item }}"with_items: "{{ users }}"
with_dict:循环字典
- hosts: node1gather_facts: novars:users:alice:name: Alice Appleworthtelephone: 123-456-7890bob:name: Bob Bananaramatelephone: 987-654-3210tasks:- name: display user telephonedebug:msg: "{{ item.value.name }} telephone is {{ item.value.telephone }}"with_dict: "{{ users }}"
loop默认循环列表
loop: “{{ users }}”
如果循环字典,就需要进行转换
loop: “{{users | dict2items }}”
fail主动错误
使用fail模块中断playbook输出
- hosts: testtasks:- shell: echo "Just a test--error"register: result- fail:msg: "Conditions established,Interrupt running playbook"when: "'error' in result.stdout"- debug:msg: "I never execute,Because the playbook has stopped"
[admin@ansible ansible]$ cat fail.yml- hosts: node1tasks:- shell: echo "Just a test--error"register: resultfailed_when: "'error' in result.stdout"- debug:msg: "I never execute,Because the playbook has stopped"
比较运算符
逻辑运算符

