Ansible中的任务执行控制

##一.循环##

#循环迭代任务#

#1.简单循环#

loop:
- value1         ##赋值列表
- value2
- ...
{{item}}         ##迭代变量名称

建立目录和默认测试页内容
[devops@nodea111 .ansible]$ vim vhost_var.yml 

---
webs:
  - root: /var/www/html
    index: www.westos.com

  - name: linux.westos.com
    root: /var/www/virtual/westos.com/linux
    index: linux.westos.com

  - name: news.westos.com
    root: /var/www/virtual/westos.com/news
    index: news.westos.com
~                                 


[devops@nodea111 .ansible]$ vim web.yml 

- name: web
  hosts: westos
  vars_files: ./vhost_var.yml
  tasks:
    - name: create
      file:
        path: "{{item.root}}"
        state: directory
      loop:
        "{{webs}}"
    - name: copy
      copy:
        dest: "{{item.root}}/index.html"
        content: "{{item.index}}"
      loop:
        "{{webs}}"

#2.循环散列或字典列表#

练习:

http://172.25.254.250/RHCE_EXAM/files/hwreport.empty
创建一个名为/home/devops/.ansible/hwreport.yaml 的playbook 它将在所在所有受管节点上含有以下信息的输出文件 /mnt/hwreport.txt 清单主机名称
以MB表示总内存大小
BIOS 版本
磁盘设备 vda 的大小
磁盘设备vdb的大小
输出文件中的每一行含有一个 key=value 对
将它保存到/mnt/hwreport.txt
使用正确的值改为 /mnt/hwreport.txt
如果硬件不存在,相关的值设为NONE

 [devops@nodea111 .ansible]$ vim hwreport.yml
                         
- name: test
  hosts: westos
  tasks:
    - name: download
      get_url:
        url: http://172.25.254.250/RHCE_EXAM/files/hwreport.empty
        dest: /mnt/hwreport.txt
        force: yes
    - name: lineinfile
      lineinfile:
        path: /mnt/hwreport.txt
        regexp: "{{item.name}}"
        line: '{{item.name}}={{item.replace}}'
      loop:
      - name: HOST
        replace: "{{ansible_facts['fqdn']}}"
      - name: MEMERY
        replace: "{{ansible_facts['memtotal_mb']}}"
      - name: BIOS
        replace: "{{ansible_facts['bios_version']}}"
      - name: DISK_SIZE_VDA
        replace: "{{ ansible_facts['devices']['vda']['size'] | default('none') }}"
      - name: DISK_SIZE_VDB
        replace: "{{ ansible_facts['devices']['vdb']['size'] | default('none') }}"

[root@nodeb mnt]# cat hwreport.txt
# Hardware report
HOST=nodeb.westos.org
MEMERY=818
BIOS=1.11.1-4.module+el8.1.0+4066+0f1aadab
DISK_SIZE_VDA=disk_vda_size
DISK_SIZE_VDB=disk_vdb_size

 

##二.条件##

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

查看文件test是否存在
[devops@nodea111 .ansible]$ cat chect_test.yml
- name: test
  hosts: westos
  tasks:
    - name: check file
      shell: test -e /mnt/test
      register: out
      ignore_errors: yes
      
    - name: debug
      debug:
        msg: "/mnt/test is exist"
      when: out.rc == 0
    
    - name: debug
      debug:
        msg: "/mnt/test is not exist"
      when: out.rc != 0

#多条条件组合#

when:
      条件1 and 条件2
       - 条件1
       - 条件2
when:

条件1 or 条件2
when: >
        条件1
        or
        条件2

[devops@nodea111 .ansible]$ vim chect_test.yml
- name:
 test
  hosts: westos
  tasks:
    - name: check file
      shell: test -e /mnt/test
      register: out
      ignore_errors: yes
      when: inventory_hostname in groups.westos

    - name: debug
      debug:
        msg: "/mnt/test is exist"
      when:
        - not out.rc

    - name: debug
      debug:
        msg: "/mnt/test is not exist"
      when:
        - out.rc

##三.触发器##

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

- name: configure webserver
  hosts: westos
  tasks:
    - name: install httpd
      dnf:
        name: httpd
        state: present
    - name: httpd.conf
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen 8080"
      notify:
        haha

  handlers:
    - name: haha
      service:
        name: httpd
        state: restarted
        enabled: yes

#四.处理失败任务#

#1.ignore_errors#

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

- name: configure webserver
  hosts: westos
  tasks:
    - name: install httpd
      dnf:
        name: http
        state: present
      ignore_errors: yes

    - name: httpd.conf
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen 8080"
      notify:
        haha

  handlers:
    - name: haha
      service:
        name: httpd
        state: restarted
        enabled: yes

#2.force_handlers#

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

---
- 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#

#作用:
#控制任务在何时报告它已进行更改

- 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#

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

---
- 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句子中出现失败任务后运行的任务
always: ##定义最终独立运行的任务

练习:

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

[devops@ansible .ansible]$ cat westos.yaml 
- name: create vdb1
  hosts: westos
  force_handlers: yes
  tasks:
    - name: check /dev/vdb
      debug: 
        msg: "vdb is not exist"
      when: ansible_facts['devices']['vdb'] is not defined

    - name: create /dev/vdb1
      block:
        - name: create 1500M
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 1500MiB
      rescue:
        - name: create 800M
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 800MiB
        - debug:
            msg: vdb is less then 1500M
      always:
        - name: create filesystem
          filesystem:
            fstype: xfs
            dev: /dev/vdb1
        - name: mkdir /westos
          file:
            path: /westos
            state: directory
        - name: mount /dev/vdb1
          mount:
            path: /westos
            src: /dev/vdb1
            fstype: xfs
            state: mounted
      when: ansible_facts['devicds']['vdb'] is defined

[devops@ansible .ansible]$ ansible-playbook westos.yaml 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值