天天看点

ansible中的block用法

一、概述

block是ansible在2.0版本引入的一个特性 ##ansible2.0之下的版本无法使用
块功能可以将任务进行逻辑分组,并且可以在块级别上应用任务变量。
同时也可以使用类似于其他编程语言处理异常那样的方法,来处理块内部的任务异常。

原理:block中的组任务,都会继承block的属相(支持when,不支持with_items)
部署时会分别执行组中的任务,并且都会继承block的属相(在任务后添加block的when条件)

块错误处理
rescue        当block中出现错误时,执行相应任务模块。
always        无论block中是否出现错误,最终都执行相应任务模块。      

二、试验

1、使用块任务分组

---
- hosts: localhost
  tasks:   
    - block:
        - yum: name={{ item }} state=installed
          with_items:
             - httpd
             - memcached
        - template: src=templates/src.j2 dest=/etc/foo.conf
        - name: start service
          service: name=bar state=started enabled=True
      when: ansible_distribution == 'CentOS'
      become: true
      become_user: root      

2、异常处理

1、例子一
首先备份配置文件,执行一次对nginx.conf文件的修改,修改完配置文件会执行nginx -t。如果验证配置文件有问题,则将目前有问题的配置文件打
上-failed标签,且回滚之前备份的配置文件。
无论block和rescue中是否发生错误,通过always,都会记录操作日志。
---
- name: test nginx config
  hosts: nginx_module
  gather_facts: False
  tasks:
  - name: modify config and test
    block:
      - name: backup config file
        command: cp /etc/nginx/nginx.conf /etc/nginx/conf-backup/nginx.conf
 
      - name: nginx config file add "include"
        lineinfile:
          path: /etc/nginx/nginx.conf
          insertafter: 'include /etc/nginx/conf.d/'
          line: 'include /etc/nginx/site-enabled/*.conf;'
 
      - name: nginx config test
        command: nginx -t
    rescue:
      - name: move failed config file
        command: mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf-failed
      - name: copy backup file
        command: mv /etc/nginx/conf-backup/nginx.conf /etc/nginx/
    always:
      - name: add operation recode
        shell: echo `date` modify config >> /etc/nginx/conf-backup/modify-config.log
        
2、例子二
---
- hosts: localhost 
  tasks:
   - block:
       - debug: msg='I execute normally'
       - command: /bin/false
       - debug: msg='I never execute, due to the above task failing'
     rescue:
       - debug: msg='I caught an error'
       - command: /bin/false
       - debug: msg='I also never execute :-('
     always:
       - debug: msg="this always executes"      

三、陷阱

1、2.3版本添加了块的name特性

在2.0中添加了块特性,在2.3中添加了块的name特性---
- hosts: localhost
  tasks:
    - name: bbbb          #2.3以下的正确姿势应该去掉block的name
      block:  
        - name: bbbb
          shell: echo bbbb
      when: false      

2、block的子任务中不能添加注册的变量

原因:如果block的when结果是false,就不会执行任务获得注册变量的值,但是组中有些任务调用此注册变量,就会任务失败。
---
- hosts: localhost
  tasks:
    - block:
        - name: aaaa
          shell: echo aaaa
        - name: bbbb
          shell: echo bbbb
          register: results               #-------后面调用会导致失败
        - name: echo {{results.stdout}}   #-------调用了,此任务会失败
          shell: echo cccc
      when: false
 
 解决办法:可以给block的vars属相添加变量,在block的组任务中进行调用
 ---
- hosts: localhost
  tasks:
    - name: bbbb
      shell: echo bbb
      register: result                    #-------注册result变量
    - block:
        - name: aaaa
          shell: echo aaaa
        - name: echo {{results}}          #-------调用了
          shell: echo cccc
      when: false
      vars:
        results: "{{result.stdout}}"      #-------使用vars,将注册的result放入block中results变量中      

3、block没有with_items属相

---
- hosts: localhost
  tasks:
    - block:
        - name: aaaa
          shell: echo aaaa
      with_items:                #-----会报错,block不支持此属相
        - myname: dxx          
      when: false
    - name: dddd
      shell: echo dddd      

继续阅读