本文共 3840 字,大约阅读时间需要 12 分钟。
今天碰到一个需求,当使用ansible-playbook在执行一个脚本后,根据脚本返回的内容判断是否继续往下执行还是中断执行,查询官网发现使用register寄存器可以实现记录脚本输出,使用when+fail模块来判断是否往下继续执行或者中断,以下是一个简单例子:
ansible服务在172.16.133.128上,先执行一个脚本,脚本返回success时在172.16.133.129上创建一个目录/tmp/test,如果返回failed则中断执行
先在172.16.133.129上配置一个简单脚本/tmp/test.sh
#!/bin/bash
echo "failed"配置yml文件test.yml,先配置执行脚本部分
使用ansible-playbook运行
[root@localhost ansible]# ansible-playbook -v test.yml
PLAY [172.16.133.129] * GATHERING FACTS *** ok: [172.16.133.129]TASK: [command /tmp/test.sh] ** changed: [172.16.133.129] => {"changed": true, "cmd": ["/tmp/test.sh"], "delta": "0:00:00.002602", "end": "2016-04-11 17:00:57.227517", "rc": 0, "start": "2016-04-11 17:00:57.224915", "stderr": "", "stdout": "failed", "warnings": []}PLAY RECAP **** 172.16.133.129 : ok=2 changed=1 unreachable=0 failed=0register保存的信息就时“=>”后面的字典信息,信息保存在result变量中可以看到"stdout"就是脚本的标准输出信息,这时可以使用"when"来判断是否执行或者跳过
修改test.yml
hosts: 172.16.133.129
remote_user: roottasks:command: /tmp/test.sh
register: result执行yml后输出如下:
[root@localhost ansible]# ansible-playbook -v test.yml
PLAY [172.16.133.129] *****
GATHERING FACTS ***
ok: [172.16.133.129]TASK: [command /tmp/test.sh] **
changed: [172.16.133.129] => {"changed": true, "cmd": ["/tmp/test.sh"], "delta": "0:00:00.002618", "end": "2016-04-11 17:11:07.957097", "rc": 0, "start": "2016-04-11 17:11:07.954479", "stderr": "", "stdout": "failed", "warnings": []}TASK: [file path=/tmp/test state=directory] ***
changed: [172.16.133.129] => {"changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/test", "size": 4096, "state": "directory", "uid": 0}PLAY RECAP ****
172.16.133.129 : ok=3 changed=2 unreachable=0 failed=0修改test.sh,输出‘success‘,然后执行test.yml
[root@localhost ansible]# ansible-playbook -v test.yml
PLAY [172.16.133.129] *****
GATHERING FACTS ***
ok: [172.16.133.129]TASK: [command /tmp/test.sh] **
changed: [172.16.133.129] => {"changed": true, "cmd": ["/tmp/test.sh"], "delta": "0:00:00.002611", "end": "2016-04-11 17:14:08.456293", "rc": 0, "start": "2016-04-11 17:14:08.453682", "stderr": "", "stdout": "success", "warnings": []}TASK: [file path=/tmp/test state=directory] ***
skipping: [172.16.133.129]PLAY RECAP ****
172.16.133.129 : ok=2 changed=1 unreachable=0 failed=0可以看到当stdout为success时,创建目录的操作直接跳过了现在回到最终要达到的目的,当脚本输出为‘failed‘时,我们要中断ansible-playbook执行,而不是跳过,这时需要配合fail模块来实现,并且可以抛出自定义信息
修改test.sh文件,输出‘failed‘
修改test.yml
hosts: 172.16.133.129
remote_user: roottasks:command: /tmp/test.sh
register: resultname: if stdout ‘failed‘ ,Interrupt execution
fail: msg="check failed"when: result.stdout == ‘failed‘[root@localhost ansible]# ansible-playbook -v test.yml
PLAY [172.16.133.129] *****
GATHERING FACTS ***
ok: [172.16.133.129]TASK: [command /tmp/test.sh] **
changed: [172.16.133.129] => {"changed": true, "cmd": ["/tmp/test.sh"], "delta": "0:00:00.002632", "end": "2016-04-11 17:27:47.248996", "rc": 0, "start": "2016-04-11 17:27:47.246364", "stderr": "", "stdout": "failed", "warnings": []}TASK: [check ,Interrupt execution] ****
failed: [172.16.133.129] => {"failed": true}msg: check failedFATAL: all hosts have already failed -- aborting
PLAY RECAP ****
to retry, use: --limit @/root/test.retry172.16.133.129 : ok=2 changed=1 unreachable=0 failed=1
playbook运行到第二个task时就会报错并抛出msgansible官网文档:
注:寄存器中的数据都可以拿来使用,如"rc","stderr"等,当然也有很多种方法,文中的‘failed‘是严格匹配,也可以使用模糊查找,如result.stdout.find(‘failed‘) != -1也可以达到相同的效果
本文出自 “漂泊的鱼” 博客,请务必保留此出处http://faded.blog.51cto.com/6375932/1762688
转载于:https://blog.51cto.com/lookingdream/2054763