when条件语句

image.png

  1. - name: Install vim
  2. hosts: all
  3. tasks:
  4. - name: Install VIM via yum
  5. yum:
  6. name: vim-enhanced
  7. state: installed
  8. when: ansible_os_family =="RedHat"
  9. - name: Install VIM via apt
  10. apt:
  11. name: vim
  12. state: installed
  13. when: ansible_os_family =="Debian"
  14. - name: Unexpected OS family
  15. debug: msg="OS Family {{ ansible_os_family }} is not supported"
  16. when: not ansible_os_family =="RedHat" or ansible_os_family =="Debian"
  1. - hosts: node1
  2. tasks:
  3. - name: debug memory size
  4. debug:
  5. msg: "{{ inventory_hostname }} memory size is {{ ansible_memtotal_mb }}"
  6. when: ansible_memtotal_mb >= 4096 #比较数值大小

条件判断

image.png

条件判断与block:

image.png

例子:

  1. -hosts: node1
  2. tasks:
  3. -block:
  4. -name: install apache
  5. yum:
  6. name: httpd
  7. state: present
  8. -name: started apache
  9. service:
  10. name: httpd
  11. state: started
  12. enable: yes
  13. when: ansible_os_family == "RedHat"

rescue

当block执行失败时,rescue中的任务才会被执行;block除了能跟和when一起使用之外,还能进行错误处理。这个时候就需要用到rescue关键字:

  1. -hosts: test
  2. tasks:
  3. -block:
  4. -shell: 'ls /testdir'
  5. rescue:
  6. -debug:
  7. msg: '/testdir is not exits'

always

无论block执行成功还是失败,always中的任务都会被执行

  1. -hosts: test
  2. tasks:
  3. -block:
  4. -shell: 'ls /testdir'
  5. rescue:
  6. -debug:
  7. msg: '/testdir is not exists'
  8. always:
  9. -debug:
  10. msg: 'This task always executes'

判断变量:

defined:判断变量是否已定义,已定义则返回真
undefined:判断变量是否未定义,未定义则返回真
none:判断变量的值是否为空,如果变量已定义且值为空,则返回真

例子:

  1. - hosts: test
  2. gather_facts: no
  3. vars:
  4. testvar: "test"
  5. testvar1:
  6. tasks:
  7. - debug:
  8. msg: "testvar is defined"
  9. when: testvar is defined
  10. - debug:
  11. msg: "testvar2 is undefined"
  12. when: testvar2 is undefined
  13. - debug:
  14. msg: "testvar1 is none"
  15. when: testvar1 is none

判断文件类型:

file:判断指定路径是否为一个文件,是则为真
directory:判断指定路径是否为一个目录,是则为真
link:判断指定路径是否为一个软链接,是则为真
mount:判断指定路径是否为一个挂载点,是则为真
exists:判断指定路径是否存在,存在则为真
特别注意:关于路径的所有判断均是判断主控端上的路径,而非被控端上的路径

  1. - hosts: test
  2. vars:
  3. testpath: /testdir
  4. tasks:
  5. - debug:
  6. msg: "file exist"
  7. when: testpath is exists
  8. - hosts: test
  9. vars:
  10. testpath: /testdir1
  11. tasks:
  12. - debug:
  13. msg: "file not exist"
  14. when: not testpath is exists
  1. [admin@ansible ansible]$ cat when3.yml
  2. - hosts: node1
  3. vars:
  4. filepath: /home/admin/ansible/ansible.cfg
  5. tasks:
  6. - debug:
  7. msg: "the file is exists"
  8. when: filepath is exists #filepath是主控端的文件
  1. - hosts: test
  2. gather_facts: no
  3. vars:
  4. testpath1: "/testdir/test"
  5. testpath2: "/testdir"
  6. tasks:
  7. - debug:
  8. msg: "file"
  9. when: testpath1 is file
  10. - debug:
  11. msg: "directory"
  12. when: testpath2 is directory

判断执行结果:

ok:目标状态与期望值一致,没有发生变更
change或changed:目标发生变更,无论成功还是失败,都是change
sucess或succeeded:目标状态与期望值一致,或者任务执行成功
failure或failed:任务执行失败
skip或skipped:任务被跳过

  1. - hosts: test
  2. tasks:
  3. - shell: 'cat /testdir/aaa'
  4. register: result
  5. ignore_errors: true
  6. - debug:
  7. msg: "success"
  8. when: result is success
  9. - debug:
  10. msg: "failed"
  11. when: result is failure
  12. - debug:
  13. msg: "changed"
  14. when: result is change
  15. - debug:
  16. msg: "skip"
  17. when: result is skip

判断字符串

lower:判断字符串中的所有字母是否都是小写,是则为真
upper:判断字符串中的所有字母是否都是大写,是则为真

  1. - hosts: node1
  2. gather_facts: no
  3. vars:
  4. str1: "abc"
  5. str2: "ABC"
  6. tasks:
  7. - debug:
  8. msg: "str1 is all lowercase"
  9. when: str1 is lower
  10. - debug:
  11. msg: "str2 is all uppercase"
  12. when: str2 is upper

判断整数:

even:判断数值是否为偶数,是则为真
odd:判断数值是否为奇数,是则为真
divisibleby(num):判断是否可以整除指定的数值,是则为真

  1. - hosts: node1
  2. gather_facts: no
  3. vars:
  4. num1: 6
  5. num2: 8
  6. num3: 15
  7. tasks:
  8. - debug:
  9. msg: "num1 is an even number"
  10. when: num1 is even
  11. - debug:
  12. msg: "num2 is an odd number"
  13. when: num2 is odd
  14. - debug:
  15. msg: "num3 can be divided exactly by"
  16. 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

  1. - hosts: node1
  2. vars:
  3. package:
  4. - httpd
  5. - nginx
  6. string: abc
  7. num: 100
  8. tasks:
  9. - name: nginx is in package
  10. debug:
  11. msg: "nginx is in package"
  12. when: '"nginx" in package'
  13. - debug:
  14. msg: a is in abc
  15. when: '"a" in string'
  16. - debug:
  17. msg: "{{ inventory_hostname }} is in web group"
  18. when: '"web" in group_names'
  19. - debug:
  20. msg: "{{ string }} is string"
  21. when: 'string is string'
  22. - debug:
  23. msg: "{{ num }} is number"
  24. when: 'num is number'
  1. - hosts: node1
  2. tasks:
  3. - block:
  4. - name: install httpd
  5. yum:
  6. name: httpd
  7. state: present
  8. - name: started httpd
  9. service:
  10. name: httpd
  11. enabled: yes
  12. state: started
  13. rescue:
  14. - name: install nginx
  15. yum:
  16. name: nginx
  17. state: present
  18. always:
  19. - name: install mariadb
  20. yum:
  21. name: mariadb*
  22. state: present
  23. when: "ansible_os_family == 'RedHat'"

playbook循环语句

with_items:循环列表

  1. - hosts: node1
  2. vars:
  3. users:
  4. - username: user11
  5. useruid: 1111
  6. - username: user22
  7. useruid: 1112
  8. - username: user33
  9. useruid: 1113
  10. tasks:
  11. - name: create user
  12. user:
  13. name: "{{ item.username }}"
  14. uid: "{{ item.useruid }}"
  15. state: present
  16. with_items: "{{ users }}"
  17. - name: debug item
  18. debug:
  19. msg: "{{ item }}"
  20. with_items: "{{ users }}"

with_dict:循环字典

  1. - hosts: node1
  2. gather_facts: no
  3. vars:
  4. users:
  5. alice:
  6. name: Alice Appleworth
  7. telephone: 123-456-7890
  8. bob:
  9. name: Bob Bananarama
  10. telephone: 987-654-3210
  11. tasks:
  12. - name: display user telephone
  13. debug:
  14. msg: "{{ item.value.name }} telephone is {{ item.value.telephone }}"
  15. with_dict: "{{ users }}"

loop默认循环列表

loop: “{{ users }}”

如果循环字典,就需要进行转换
loop: “{{users | dict2items }}”

fail主动错误

使用fail模块中断playbook输出

  1. - hosts: test
  2. tasks:
  3. - shell: echo "Just a test--error"
  4. register: result
  5. - fail:
  6. msg: "Conditions established,Interrupt running playbook"
  7. when: "'error' in result.stdout"
  8. - debug:
  9. msg: "I never execute,Because the playbook has stopped"
  1. [admin@ansible ansible]$ cat fail.yml
  2. - hosts: node1
  3. tasks:
  4. - shell: echo "Just a test--error"
  5. register: result
  6. failed_when: "'error' in result.stdout"
  7. - debug:
  8. msg: "I never execute,Because the playbook has stopped"

比较运算符

image.png

逻辑运算符

image.png