在Ansible

时间:2016-12-08 16:17:06

标签: ansible ansible-playbook

我正在尝试使用ansible shell模块使用不同的参数多次运行shell脚本(script1)。但是如果任何命令失败并且返回代码不是0,则任务应该失败并退出。这是我到目前为止。

- name: Run scripts
  shell: "{{ item}}"
  register: rslt
  until: rslt.rc != 0
  with_items:
    - "./script1 -f add1"
    - "./script1 -f add2"
    - "./script1 -f add3"

此任务总是运行脚本3次,即使第一个脚本失败并返回代码(rslt.rc)而不是0.我希望任务失败并退出而不运行with_items中的后续项目如果当前执行脚本返回的返回码不是0.例如,如果第一个项目(" ./ script1 -f add1")失败,我不想要第二个和第三个项目运行和ansible任务应该失败。

我非常感谢有关如何解决这个问题的任何建议。

1 个答案:

答案 0 :(得分:0)

不幸的是,推荐的1.9解决方案是将任务分成单独的呼叫。

Github上有一些past discussion

您可以使用when子句而不是until来实现2.0+。

在找到非零返回码后,这将跳过剩余的任务:

- name: Run scripts
  shell: "{{ item }}"
  register: rslt
  when: rslt is undefined or rslt.rc == 0
  with_items:
     ...

示例输出:

TASK [Run scripts] *************************************************************
changed: [localhost] => (item=exit 0)
changed: [localhost] => (item=exit 0)
failed: [localhost] (item=exit 1) => {"changed": true, "cmd": "exit 1",  "delta": "0:00:00.004414", "end": "2016-12-08 13:14:06.365437", "failed": true, "item": "exit 1", "rc": 1, "start": "2016-12-08 13:14:06.361023", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}
skipping: [localhost] => (item=exit 0)
skipping: [localhost] => (item=exit 0)
相关问题