• Ansible中的任务执行控制


    目录

    一.循环

    1.简单循环

    2.循环散列或字典列表

    二.条件

    when: - 条件1 - 条件

     多条条件组合

    三.触发器

    四.处理失败任务

    1.ignore_errors

    2.force_handlers

     3.changed_when

    4.failed_when

    5.block

    测试练习

    磁盘划分

    2.建立userlist.yml中的用户,并且使用户密码加密,


    一.循环

    循环迭代任务

    1.简单循环

    - name: test play

      hosts: westos

      tasks:

        - debug:

            msg: "{{item}}"

          loop:

            - westos

            - linux

            - lee

    2.循环散列或字典列表

    - name: test play

       hosts: westos

       tasks:

         - debug:

             msg: "{{item.name}} {{item.age}}"

           loop:

             - { name: westos, age: 18 }

             - { name: linux, age: 19 }

             - { name: lee, age: 20 }

     

    二.条件

    when: - 条件1 - 条件

    2     条件判断

    =

    value == "字符串",value == 数字

    <

    value < 数字

    >

    value > 数字

    <=

    value <= 数字

    >=

    value >= 数字

    !=

    value != 数字

    is defined

    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的值为true

    bool变量 false

    value的值为false

     多条条件组合

    when

    条件1 and 条件2

    - 条件1

    - 条件2

    when:

    条件1 or 条件2

    when: >

    条件1

    or

    条件2

    三.触发器

    notify: 触发器当遇到更改是触发handlers

    handlers:触发器触发后执行的动作

    实例:安装Apache,更改端口为8080,如果更改,并重启Apache,触发触发器;如果端口号是8080未被修改,则不需要重启,触发器不被触发

    - name: apache server

      hosts: westos

      vars:

        port: 8080

      tasks:

        - name: install httpd

          dnf:

            name: httpd

            state: present

        - name: set config

          lineinfile:

            path: /etc/httpd/conf/httpd.conf

            regexp: ^Listen

            line: "Listen {{port}}"

          notify: restart apache

        - name: enable filewalld

          firewalld:

            port: "{{port}}/tcp"

            permanent: yes

            immediate: yes

            state: enabled

    - name: state apache

          service:

            name: httpd

            state: started

            enabled: yes

      handlers:

        - name: restart apache

          service:

            name: httpd

            state: restarted

    四.处理失败任务

    1.ignore_errors

    作用:

    当play遇到任务失败时会终止,ignore_errors: yes将会忽略任务失败使下面的任务继续运行

    实例:执行install software play失败后会执行show some message play模块

    - name: test play

      hosts: westos

      tasks:

        - name: inatall software

          dnf:

            name: http(错误)

            state: present

          ignore_errors: yes

        - name: show some message

          debug:

            msg: hello westos

     

    2.force_handlers

    作用:当任务失败后play被终止,也会调用触发器进程

    - name: test play

      hosts: westos

      force_handlers: yes

      tasks:

        - name: test

          shell: ls /mnt/

        - name: install software

          dnf:

            name: http(错误)

            state: present

        - name: show some massages

          debug:

            msg: hello westos

      handlers:

        - name: notify messages

          debug:

            msg: hello notify

     

     3.changed_when

    作用: 控制任务在何时报告它已进行更改,无论有没有更改,都视为更改

    - name: test play

      hosts: westos

      tasks:

        - name: install software

          dnf:

            name: http(错误)

            state: present

          changed_when: yes

          notify: notify message

        - name: show some message

          debug:

            msg: hello westos

       handlers:

         - name: notify message

           debug:

             msg: hello notify

    4.failed_when

    当符合条件时强制任务失败 (无论是不是失败的,都强制失败)

    - name: test play

      hosts: westos

      tasks:

        - name: install software

          dnf:

            name: http(错误)

            state: present

          failed_when: yes

          notify: notify message

        - name: show some message

          debug:

            msg: hello westos

       handlers:

         - name: notify message

           debug:

             msg: hello notify

     

    5.block

    block:           定义要运行的任务

    rescue:          定义当block句子中出现失败任务后运行的任务

    always:       定义最终独立运行的任务

    - name: test play

      hosts: westos

      force_handlers: yes

      tasks:

        - name: test block

          block:

            - name: install software

              dnf:

                name: http(错误)

                state: present

          rescue:

            - name: show error

              debug:

                msg: http is not exist

          always:

            - name: show message

              debug:

                msg: over

    测试练习

    磁盘划分

    1.建立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上

    - name: test

      hosts: westos

      tasks:

        - name: /dev/sdb is not exist

          debug:

            msg: /dev/sdb is not exist

          when:

            ansible_facts['devices']['sdb'] is not defined

        - name: create_storage

          block:

            - name: create 1500M

              parted:

                device: /dev/sdb

                number: 1

                state: present

                part_end: 1500MiB

              when:

                ansible_facts['devices']['sdb'] is  defined

          rescue:

            - name: create 800M

              parted:

                device: /dev/sdb

                number: 1

                state: present

                part_end: 800MiB

              when:

                ansible_facts['devices']['sdb'] is defined

            - name: device messages

              debug:

                msg: /dev/sdb is less 2G

              when:

                ansible_facts['devices']['sdb'] is defined

          always:

            - name: create filesystem

              filesystem:

                fstype: xfs

                dev: /dev/sdb1

            - name:

              file:

                path: /westos

                state: directory

            - name: mount devices

              mount:

                path: /westos

                src: /dev/sdb1

                fstype: xfs

                state: mounted

          when:

            ansible_facts['devices']['sdb'] is defined

     

    2.建立userlist.yml中的用户,并且使用户密码加密,

    vim userlist.yml

     

    vim useradd.yml 

  • 相关阅读:
    利用机器学习重构视频中的人脸
    数据结构与算法——19.红黑树
    combit Report Server 29
    SAP 报表COOIS增强(BADI : WORKORDER_INFOSYSTEM / Method: TABLES_MODIFY_LAY )<转载>
    LDO(线性稳压器)设计检查
    Spring切面动态代理注册器AspectJAutoProxyRegistrar
    Three.js对模型进行多区域染色
    企业如何选型iPaaS平台
    图片压缩软件有哪些?分享三个好用的图片压缩软件
    ES6 入门教程 25 Module 的加载实现 25.4 循环加载
  • 原文地址:https://blog.csdn.net/LRJ2034203479/article/details/126074557