ansible

ansible之条件判断与block

2019-05-09  本文已影响0人  燃燃的爸爸

转载:http://www.zsythink.net/archives/2836

今天刚好在工作中碰到一个难事,官网也找了很久很多网友推荐我看看meta data,发现主要是做依赖关系。
我原先的设计想法大致是这样,但是这里有个问题是因为我用了fail,如果单单安装python3那是非常适合,避免重复安装,但是fail这样一做,在执行role coscmd的时候也会被强制退出导致有python3的情况下coscmd不能安装了,因为判断到了python3已经安装了执行role python3!


image.png

途中经历的过程:

1.想到使用when

在role python里面换成when,但是这样的话每个task都会需要加上when,并且都会去判断一次,虽然这样可以实现功能,但是如果有多个服务器执行的情况下,消耗了多余的判断时间。放弃

2.想到使用tags与import_role结合

在role coscmd 里面加上import role python3,然后在role python3里面fail上加个tags,但是发现role里面无法调用类型程序指令skip-tags

#- import_role:
#       name: inst_py3

所以每次外部调用的时候需要指定skip-tags,这样的操作往往时间久了就会忘记。放弃

3.利用block

该方案我也是偶然在官网看到这个指令,然后看到了我转载的blog,非常棒!
工作中经常用到when,就像我第一个方案那样,这样的话在role python3里面就需要写多个when,个人觉得麻烦。
最后role python3修改成这样,这样的修改办法是block里面的shell如果是执行错误的话,那么就会去执行rescue,否则不会执行rescue。

- block:
   - shell: 'ls /usr/local/bin/python3'
     #ignore_errors: true  #这里不能加上,不然会忽略错误,不执行rescue
  rescue:  
   - name: mkdir /fenku
     shell: "mkdir -p {{ std_dir }}"

   - name: download python3.tar.gz
     get_url: url=http://gcadmin-10049437.cossh.myqcloud.com/Python-3.6.8.tgz dest={{ std_dir }} mode=0744
     
   - name: tar python3.tar.gz and install
     unarchive: src=/fenku/Python-3.6.8.tgz dest={{ std_dir }} copy=no mode=0744
   
   - name: configure make make install
     shell: "cd /{{ std_dir }}/Python-3.6.8;./configure;make;make install;"
   
   - name: check install completeness
     shell: "python3 -V"
     register: ver
     
   - debug: var=ver

一、下面我们来学习下block的一些基本使用方法

1.block+when 该结合可以少写when条件

---
- hosts: test70
  remote_user: root
  tasks:
  - debug:
      msg: "task1 not in block"
  - block:
      - debug:
          msg: "task2 in block1"
      - debug:
          msg: "task3 in block1"
    when: 2 > 1

block里面的是一个整体的概念

2.错误处理功能

如果不使用block,以前的实现方案是:

---
- hosts: test70
  remote_user: root
  tasks:
  - shell: 'ls /ooo'
    register: return_value
    ignore_errors: true
  - debug:
      msg: "I cought an error"
    when: return_value is failed

用block的方案:

---
- hosts: test70
  remote_user: root
  tasks:
  - block:
      - shell: 'ls /ooo'
    rescue:
      - debug:
          msg: 'I caught an error'

rescue当且仅当block执行有错误的时候才会执行,这样看上去精简一点。
block的优势:

---
- hosts: test70
  remote_user: root
  tasks:
  - block:
      - shell: 'ls /opt'
      - shell: 'ls /testdir'
      - shell: 'ls /c'
    rescue:
      - debug:
          msg: 'I caught an error'

block里面的三个shell,只要有一个任务出错,那么就执行rescue。如果使用fail when的话需要搞三遍。

另外block 还有个always 也就是无论怎么样都执行,不管block的任务失败还是成功
(感觉像是程序里面的异常扑捉:try excep finally)

---
- hosts: test70
  remote_user: root
  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"

block里command: /bin/false任务失败,跳出执行rescue的任务,到command: /bin/false又执行失败,跳出执行always。

上一篇 下一篇

猜你喜欢

热点阅读