Ansible-当条件不与元任务一起工作时

Ansible-当条件不与元任务一起工作时,ansible,ansible-2.x,Ansible,Ansible 2.x,在元标记中使用when条件似乎有问题。我尝试了一些变化,也读了一些书,仍然不确定,所以我想我也应该在这里问一下 任务 - name: "This is my task" command: "{{ path to conda }} {{ script }}" register: check until: check is succeeded retries: 5 no_log: false - name: "End this play if the script ran su

在元标记中使用when条件似乎有问题。我尝试了一些变化,也读了一些书,仍然不确定,所以我想我也应该在这里问一下

任务

- name: "This is my task"
  command: "{{ path to conda }} {{ script }}"
  register: check
  until: check is succeeded
  retries: 5
  no_log: false

- name: "End this play if the script ran successfully on 1 remote target"
  meta: end_play
  when: 
    - check is succeeded
错误

错误!条件检查“检查成功”失败。错误 was:失败的测试需要出现错误的字典 在“/path/to/roles/tasks/main.yml”中:第9行第3列,但可能是 文件中的其他位置,具体取决于语法问题。这个 令人不快的一行似乎是: -name:“如果脚本在1个远程目标上成功运行,则结束此播放”^here

更新(为了完成而共享)

共享指定的解决方法。在我的用例中,我基本上希望在整个清单上“尝试”一项任务,直到成功

最终使用了主机变量。如果任务是资源清册中的第一台主机,则它将始终尝试该任务,并在决定在下一台inv主机上再次执行该任务之前“回顾”上一台主机的尝试

# my task
- name: "Attempt this task on each host if unsuccessful"
  raw: "raw_command_here_as_remote_host_is_using_rbash"
  register: status
  when: >
    inventory_hostname == ansible_play_hosts_all[0] or
    hostvars [ ansible_play_hosts_all [ groups ['my_host_group'].index(inventory_hostname) | int - 1 ] ] ['stop_it'] == 'false'
  ignore_errors: yes
  until: status is succeeded
  retries: 1

- set_fact:
    stop_it: true
    cacheable: yes
  when: status is succeeded

- set_fact:
    stop_it: false
    cacheable: yes
  when: status is not succeeded
# my task
- name: "Attempt this task on each host if unsuccessful"
  raw: "raw_command_here_as_remote_host_is_using_rbash"
  register: status
  when: >
    inventory_hostname == ansible_play_hosts_all[0] or
    hostvars [ ansible_play_hosts_all [ groups ['my_host_group'].index(inventory_hostname) | int - 1 ] ] ['stop_it'] == 'false'
  ignore_errors: yes
  until: status is succeeded
  retries: 1

- set_fact:
    stop_it: true
    cacheable: yes
  when: status is succeeded

- set_fact:
    stop_it: false
    cacheable: yes
  when: status is not succeeded
更新(为了完成而共享答案)

共享指定的解决方法。在我的用例中,我基本上希望在整个清单上“尝试”一项任务,直到成功

最终使用了主机变量。如果任务是资源清册中的第一台主机,则它将始终尝试该任务,并在决定在下一台inv主机上再次执行该任务之前“回顾”上一台主机的尝试

# my task
- name: "Attempt this task on each host if unsuccessful"
  raw: "raw_command_here_as_remote_host_is_using_rbash"
  register: status
  when: >
    inventory_hostname == ansible_play_hosts_all[0] or
    hostvars [ ansible_play_hosts_all [ groups ['my_host_group'].index(inventory_hostname) | int - 1 ] ] ['stop_it'] == 'false'
  ignore_errors: yes
  until: status is succeeded
  retries: 1

- set_fact:
    stop_it: true
    cacheable: yes
  when: status is succeeded

- set_fact:
    stop_it: false
    cacheable: yes
  when: status is not succeeded
# my task
- name: "Attempt this task on each host if unsuccessful"
  raw: "raw_command_here_as_remote_host_is_using_rbash"
  register: status
  when: >
    inventory_hostname == ansible_play_hosts_all[0] or
    hostvars [ ansible_play_hosts_all [ groups ['my_host_group'].index(inventory_hostname) | int - 1 ] ] ['stop_it'] == 'false'
  ignore_errors: yes
  until: status is succeeded
  retries: 1

- set_fact:
    stop_it: true
    cacheable: yes
  when: status is succeeded

- set_fact:
    stop_it: false
    cacheable: yes
  when: status is not succeeded

Meta确实与when一起工作。我认为“检查成功”是错误的。尝试在:1==1时运行,看看是否有效。谢谢您的建议!我选择了另一条路线我在更新中分享了解决方法