ansible-playbook 有个一个failed_when,这个是什么时候会用到呢?
先看一个例子:
1 2 3 4 5 6 7 8 9 10 11 |
--- - hosts: localhost remote_user: root tasks: - name: command command: /bin/false ignore_errors: yes - name: another debug: msg: "this is the pring info" |
这个例子中,command 命令明显就会返回一个false , 这个时候ansible-playbook就知道这个task failed,但是,有些时候,我们是需要通过返回的字符串来判断是否failed, 例如:
1 2 3 4 |
- name: Fail task when the command error output prints FAILED command: /usr/bin/example-command -x -y -z register: command_result failed_when: "'FAILED' in command_result.stderr" |
也就是说,我们只有在返回结果中出现字符串‘FAILED’,我们才认为我们的task 失败了(因为command 命令即使执行成功,返回值才是我们要判断失败与否的关键)
类似的有changed_when
1 2 3 4 5 6 7 8 9 |
tasks: - shell: /usr/bin/billybass --mode="take me to the river" register: bass_result changed_when: "bass_result.rc != 2" # this will never report 'changed' status - shell: wall 'beep' changed_when: False |
在使用command /shell 模块的时候ansible playbook 会按照自己的判断来决定是否changed了,有时候我们仅仅是ls 了一下, ansible-playbook 也会认为是changed了,可能这并不是我们想要的,这个时候我们就要用例子中方法来修改task的状态了
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021
广州网站建设 2018/08/21 09:56
确实是这样的