在Ansible中多次运行shell模块

在Ansible中多次运行shell模块,ansible,ansible-playbook,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" - "./sc

我正在尝试使用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。如果脚本的当前执行返回的返回代码不是0,则我希望任务失败并退出,而不使用_items运行中的后续项。例如,如果第一项(“./script1-f add1”)失败,我不希望第二项和第三项运行,并且ansible任务应该失败


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

不幸的是,1.9推荐的解决方案是将任务分成单独的调用

Github中有一些关于这方面的内容

您可以在2.0+中通过使用when子句而不是until来实现这一点

找到非零返回码后,将跳过其余任务:

- 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)

我尝试了“when”,但它不会跳过非零返回代码之后的剩余任务。这和ansible版本有关吗?我有ansible 1.9.Hey@hmdb,只是在1.9.4上运行了它,可以验证我的解决方案不适用于这个旧版本。让我看看能不能为1.9做点什么谢谢@Rob Wagner