• Ansible任务控制loop循环、when和block条件判断介绍演示


    目录

    一.循环迭代(loop/with_items)

    1.作用

    2.案例演示

    (1)遍历输出以列表形式定义的abc

    (2)遍历输出以变量定义的webservers组内主机

    (3)遍历输出以字典定义的数据

    (4)遍历嵌套定义的loop

    二.条件判断(when)

    1.作用

    2.when语句的比较运算符

    (1)!=/==用于比较两个对象是否不等/相等,可用于字符串、数字类型

    (2)>/<,>=/<=用于比较两个对象的大小

    3.when语句的逻辑运算符

    (1)and,逻辑与,两个对象同真为真

    (2)or,逻辑或,两个对象见真为真

    (3)not,逻辑否,取反

    (4)(),作为一个更大的表达式包含一组表达式

    4.when语句的exists判断

    (1)判断目录是否存在

    (2)判断文件是否不存在,取反操作

    5.when语句对于变量的判断

    (1)defined,变量已被定义

    (2)变量未被定义

    (3)变量被定义为空

    6.when语句对于结果的判断

    (1)success/succeeded,failure/failed判断任务任性成功与否

    (2)change/changed,skip/skipped判断任务执行状态

    三.整体条件判断block

    1.作用

    2.block用法示例

    3.block错误处理时用法演示

    4.block固定执行用法演示


     

    一.循环迭代(loop/with_items)

    1.作用

    主要是用于让管理节点在需要编写多个任务时的情况下仅用一个任务来进行循环迭代完成任务,使用loop关键字对目标进行迭代,在ansible相对比较旧的版本中使用的with_item平替,现在我的2.9.27仍然兼容,loop关键字内可以是列表、变量、字典等

    2.案例演示

    (1)遍历输出以列表形式定义的abc

    1. [root@main playkongzhi]# cat loop1.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: test item
    6.     debug:
    7.       msg: "{{ item }}"
    8.     loop:
    9.       - 'a'
    10.       - 'b'
    11.       - 'c'
    12.        
    13. [root@main playkongzhi]# ansible-playbook loop1.yaml
    14. ok: [servera] => (item=a) => {
    15.   "msg": "a"
    16. }
    17. ok: [servera] => (item=b) => {
    18.   "msg": "b"
    19. }
    20. ok: [servera] => (item=c) => {
    21.   "msg": "c"
    22. }

    (2)遍历输出以变量定义的webservers组内主机

    1. [root@main playkongzhi]# cat loop1.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: test item
    6.     debug:
    7.       msg: "{{ item }}"
    8.     loop: "{{ groups.webservers }}"
    9.      
    10. [root@main playkongzhi]# ansible-playbook loop1.yaml
    11. ok: [servera] => (item=servera) => {
    12.   "msg": "servera"
    13. }
    14. ok: [servera] => (item=serverb) => {
    15.   "msg": "serverb"
    16. }

    (3)遍历输出以字典定义的数据

    循环输出hello和nihao

    1. [root@main playkongzhi]# cat loop1.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: test item
    6.     debug:
    7.       msg: "{{ item }}"
    8.     loop:
    9.       - x1: 'hello'
    10.       - x2: 'nihao'
    11. [root@main playkongzhi]# ansible-playbook loop1.yaml
    12. ok: [servera] => (item={u'x1': u'hello'}) => {
    13.   "msg": {
    14.       "x1": "hello"
    15.   }
    16. }
    17. ok: [servera] => (item={u'x2': u'nihao'}) => {
    18.   "msg": {
    19.       "x2": "nihao"
    20.   }
    21. }

    (4)遍历嵌套定义的loop

    循环创建user1和user2

    1. [root@main playkongzhi]# cat loopuser.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: create user1 and user2
    6.     user:
    7.       name: "{{ item.name }}"
    8.       state: present
    9.       uid: "{{ item.uid }}"
    10.     loop:
    11.       - name: user1
    12.         uid: 1011
    13.       - name: user2
    14.         uid: 1012
    15. [root@main playkongzhi]# ansible servera -a 'id user1'
    16. servera | CHANGED | rc=0 >>
    17. uid=1011(user1) gid=1011(user1) groups=1011(user1)
    18. [root@main playkongzhi]# ansible servera -a 'id user2'
    19. servera | CHANGED | rc=0 >>
    20. uid=1012(user2) gid=1012(user2) groups=1012(user2)

    二.条件判断(when)

    1.作用

    (1)条件判断用于此任务的结果依赖变量、fact或者是上一个任务的结果,用条件判断就可以决定如何去执行对应的任务。在节点上建立了限制,捕获那些命令的输出,结果不同,执行的任务要求不同。

    (2)可以判断某服务的配置文件是否发生变动时考虑是否重启。

    (3)可以根据cpu状况来定制合适的调优策略

    2.when语句的比较运算符

    (1)!=/==用于比较两个对象是否不等/相等,可用于字符串、数字类型

    示例

    1. [root@main playkongzhi]# cat bijiao.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: com them
    6.     debug:
    7.       msg: '等于'
    8.     when: name == owner
    9. vars:
    10.   name: sulibao
    11.   owner: sulibao
    12. ok: [servera] => {
    13.   "msg": "等于"
    14. }

    (2)>/<,>=/<=用于比较两个对象的大小

    示例

    1. [root@main playkongzhi]# cat bijiao.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: com them
    6.     debug:
    7.       msg: 'hello'
    8.     when: name >= owner
    9. vars:
    10.   name: sul
    11.   owner: li
    12.    
    13. ok: [servera] => {
    14.   "msg": "hello"
    15. }

    3.when语句的逻辑运算符

    (1)and,逻辑与,两个对象同真为真

    1. [root@main playkongzhi]# cat when.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: dis info
    6.     debug:
    7.       msg: "yes"
    8.     when: 1 > 0 and 1 < 9
    9.      
    10. ok: [servera] => {
    11.   "msg": "yes"
    12. }

    (2)or,逻辑或,两个对象见真为真

    1. [root@main playkongzhi]# cat when.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: dis info
    6.     debug:
    7.       msg: "yes"
    8.     when: 1 > 0 or 1 < 9
    9. ok: [servera] => {
    10.   "msg": "yes"
    11. }

    (3)not,逻辑否,取反

    1. [root@main playkongzhi]# cat when.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: dis info
    6.     debug:
    7.       msg: "yes"
    8.     when: not 1 > 0 and 1 < 9
    9. skipping: [servera]

    (4)(),作为一个更大的表达式包含一组表达式

    1. [root@main playkongzhi]# cat when.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: dis info
    6.     debug:
    7.       msg: "yes"
    8.     when: (not 1 > 0 and 1 < 9) or 5 < 6
    9.      
    10. ok: [servera] => {
    11.   "msg": "yes"
    12. }

    4.when语句的exists判断

    (1)判断目录是否存在

    1. [root@main playkongzhi]# cat test.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - debug:
    6.       msg: "directory exist"
    7.     when: path is exists
    8. vars:
    9.   path: /root
    10.    
    11. ok: [servera] => {
    12.   "msg": "directory exist"
    13. }

    (2)判断文件是否不存在,取反操作

    1. [root@main playkongzhi]# cat test.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - debug:
    6.       msg: "file not exist"
    7.     when: not path is exists
    8. vars:
    9.   path: /root/ansible.bak
    10.    
    11. ok: [servera] => {
    12.   "msg": "file not exist"
    13. }

    5.when语句对于变量的判断

    (1)defined,变量已被定义

    1. [root@main playkongzhi]# cat varswhen.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: test vars
    6.     debug:
    7.       msg: "var defined"
    8.     when: inventory_hostname is defined
    9. ok: [servera] => {
    10.   "msg": "var defined"
    11. }

    (2)变量未被定义

    1. [root@main playkongzhi]# cat varswhen.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: test vars
    6.     debug:
    7.       msg: "var undefined"
    8.     when: a is undefined
    9. ok: [servera] => {
    10.   "msg": "var undefined"
    11. }

    (3)变量被定义为空

    1. [root@main playkongzhi]# cat varswhen.yaml
    2. ---
    3. - hosts: servera
    4. vars:
    5.   a:
    6. tasks:
    7.   - name: test vars
    8.     debug:
    9.       msg: "var none"
    10.     when: a is none
    11.      
    12. ok: [servera] => {
    13.   "msg": "var none"
    14. }

    6.when语句对于结果的判断

    (1)success/succeeded,failure/failed判断任务任性成功与否

    1. [root@main playkongzhi]# cat result.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: cd result
    6.     shell: "cd /cevd"
    7.     ignore_errors: true   #为了测试结果完整,忽略错误
    8.     register: result
    9.   - name: test result
    10.     debug:
    11.       msg: "no directory /cevd"
    12.     when: not result is success
    13. ok: [servera] => {
    14.   "msg": "no directory /cevd"
    15. }

    (2)change/changed,skip/skipped判断任务执行状态

    1. [root@main playkongzhi]# cat result.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: cd result
    6.     shell: "touch /a.txt"
    7.     ignore_errors: true
    8.     register: result
    9.   - name: test result
    10.     debug:
    11.       msg: "touch success"
    12.     when: result is changed
    13. ok: [servera] => {
    14.   "msg": "touch success"
    15. }

    三.整体条件判断block

    1.作用

    解决when语句多个判断需要书写多个when的情况,使用block将多个任务组合起来,对这个整体进行when条件判断,条件成立了才会去执行块中的所有任务。在playbook中可以给block定义name,也可以直接使用when,但不能直接对其使用loop循环。这里主要介绍block常规用法,rescue错误处理用法和always固定执行用法

    2.block用法示例

    1. [root@main playkongzhi]# cat block.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: carry block
    6.     block:
    7.       - name: free
    8.         shell: "free -m"
    9.       - name: echo
    10.         shell: "free -m > /a.txt"
    11.     when: 1 < 2
    12. [root@main playkongzhi]# ansible servera -a 'cat /a.txt'
    13. servera | CHANGED | rc=0 >>
    14.             total       used       free     shared buff/cache   available
    15. Mem:           5848         306       5348         11         193       5313
    16. Swap:         2047           0       2047

    3.block错误处理时用法演示

    没有/my目录,shell模块执行错误,执行rescue部分

    注意:block模块中只要有一个任务执行失败都会跳转执行rescue部分

    1. [root@main playkongzhi]# cat rescue.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: test rescue
    6.     block:
    7.       - shell: "ls /my"
    8.     rescue:
    9.       - debug:
    10.           msg: "no directory"
    11. ok: [servera] => {
    12.   "msg": "no directory"
    13. }

    4.block固定执行用法演示

    不管bloack块执行成功还是失败,都执行alway部分

    1. [root@main playkongzhi]# cat always.yaml
    2. ---
    3. - hosts: servera
    4. tasks:
    5.   - name: test rescue
    6.     block:
    7.       - shell: "ls /my"
    8.     rescue:
    9.       - debug:
    10.           msg: "no directory"
    11.     always:
    12.       - debug:
    13.           msg: "there always"
    14. ok: [servera] => {
    15.   "msg": "no directory"
    16. }
    17. ok: [servera] => {
    18.   "msg": "there always"
    19. }

     

  • 相关阅读:
    19 使用MapReduce编程统计超市1月商品被购买的次数
    java 并发篇
    知乎小说推文渠道怎么找?一级代理机构是哪家?
    计算机组成原理(九)
    mapreduce-maven--30.串联所有单词的字串
    轻量限制流量?阿里云轻量应用服务器月流量包收费说明
    C# 第五章『面向对象』◆第8节:接口
    spring02
    3个g的文件怎么发送给别人?三种方法自由选择!
    Android 12 源码分析 —— 应用层 四(SystemUI的基本布局设计及其基本概念)
  • 原文地址:https://blog.csdn.net/weixin_64334766/article/details/134032136