Ansible中的任务执行控制

一、循环

循环迭代任务

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

二、条件

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列表中

#多条条件组合#
when:
 条件1 and 条件2
  - 条件1
  - 条件2
when:
 条件1 or 条件2
when: >
 条件1
 or
 条件2

true和false

三、触发器

notify:      触发器当遇到更改是触发handlers
handlers:   触发器触发后执行的动作

#实例#
---
- name: create virtualhost for web server
  hosts: 172.25.0.254
  vars_files:
    ./vhost_list.yml
  tasks:
    - name: create document
      file:
        path: "{{web2.document}}"
        state: directory
    - name: create vhost.conf
      copy:
        dest: /etc/httpd/conf.d/vhost.conf
        content:
          "<VirtualHost *:{{web1.port}}>\n\tServerName {{web1.name}}\n\tDocumentRoot 
{{web1.document}}\n\tCustomLog logs/{{web1.name}}.log combined\n</VirtualHost>\n\n<VirtualHost *:
{{web2.port}}>\n\tServerName {{web2.name}}\n\tDocumentRoot {{web2.document}}\n\tCustomLog logs/
{{web2.name}}.log combined\n</VirtualHost>"
      notify:
        restart apache
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

四、处理失败任务

1.ignore_errors

#作用:

当play遇到任务失败时会终止

ignore_errors: yes 将会忽略任务失败使下面的任务继续运行

#实例#
- name: check file play
  hosts: all
  tasks:
    - name: check file
      shell:
        test -e /mnt/file
      ignore_errors: yes
      register: check_state

    - name: show message
      debug:
        msg: hello world

2.force_handlers

#作用:

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

#example
---
- name: apache change port
  hosts: 172.25.0.254
  force_handlers: yes
  vars:
    http_port: 80
  tasks:
    - name: configure apache conf file
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen {{ http_port }}"
      notify: restart apache
      
    - name: install error
      dnf:
        name: westos
        state: latest
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
        enabled: yes

3.changed_when

#作用:

#控制任务在何时报告它已进行更改;强制更改:true,强制不更改:false

---
- name: apache change port
  hosts: 172.25.0.254
  force_handlers: yes
  vars:
    http_port: 8080
  tasks:
    - name: configure apache conf file
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen {{ http_port }}"
      changed_when: true
      notify: restart apache
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
        enabled: yes

4.failed_when

#当符合条件时强制任务失败

#强制某个play为失败状态

---
- name: test
  hosts: 172.25.0.254
  tasks:
    - name: shell
      shell: echo hello
      register: westos
      failed_when: "'hello' in westos.stdout"

5.block

block:       ##定义要运行的任务
rescue:      ##定义当block句子中出现失败任务后运行的任务
             ##block运行成功,则rescue不运行
always:     ##定义最终独立运行的任务

五、 练习

建立playbook ~/westos.yml要求如下:
建立大小为1500M名为/dev/vdb1的设备
如果/dev/vdb不存在请输入:
 /dev/vdb is not exist
如果/dev/vdb大小不足1.5G请输出:
 /dev/vdb is less then 1.5G
并建立800M大小的/dev/vdb1
此设备挂载到/westos上

- name: test fdisk
  hosts: all
  tasks:
    - name: check sdb
      debug:
        msg: /dev/sdb is not exist
      when: ansible_facts['devices']['sdb'] is not defined

    - name: create /dev/sdb1
      block:
        - name: check size
          parted:
            device: /dev/sdb
            number: 1
            state: present
            part_end: 1.5GiB
          when: ansible_facts['devices']['sdb'] is defined
        - name: show size is not enough 1.5G
          debug:
            msg: /dev/sdb is not enough 1.5G
      rescue:
        - name: create /dev/sdb1
          parted:
            device: /dev/sdb
            number: 1
            state: present
            part_end: 800MiB
          notify:
            - set filesystem
            - mount /dev/sdb1
          when: ansible_facts['devices']['sdb'] is defined
      always:
        - name: create mount point
          file:
            path: /westos
            state: directory

  handlers:
    - name: set filesystem
      filesystem:
        fstype: ext4
        dev: /dev/sdb1
    - name: mount /dev/sdb1
      mount:
        path: /westos/
        src: /dev/sdb1
        fstype: ext4
        state: mounted
相关推荐
我的运维人生19 小时前
利用Python与Ansible实现高效网络配置管理
网络·python·ansible·运维开发·技术共享
qlau200720 小时前
基于kolla-ansible在AnolisOS8.6上部署all-in-one模式OpenStack-Train
ansible·openstack
Shenqi Lotus1 天前
Ansible——Playbook基本功能
运维·ansible·playbook
qlau20074 天前
基于kolla-ansible在openEuler 22.03 SP4上部署OpenStack-2023.2
ansible·openstack
水彩橘子4 天前
Semaphore UI --Ansible webui
ui·ansible
happy_king_zi4 天前
ansible企业实战
运维·ansible·devops
码上飞扬5 天前
深入浅出 Ansible 自动化运维:从入门到实战
运维·ansible·自动化运维
theo.wu5 天前
Ansible自动化部署kubernetes集群
kubernetes·自动化·ansible
xidianjiapei0015 天前
Ubuntu Juju 与 Ansible的区别
linux·ubuntu·云原生·ansible·juju
打败4045 天前
ansible_find模块
linux·ansible