Ansible - 当条件不适用于元任务时
Ansible - When condition not working with meta task
我似乎无法在元标记中使用 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
错误
ERROR! The conditional check 'check is succeeded' failed. The error
was: The failed test expects a dictionary The error appears to have
been in '/path/to/roles/tasks/main.yml': line 9, column 3, but may be
elsewhere in the file depending on the exact syntax problem. The
offending line appears to be:
- name: "End this play if the script ran successfully on 1 remote target" ^ here
更新(分享完成)
共享指定的解决方法。在我的用例中,我基本上想 "try" 对整个清单执行一项任务 直到 成功
最终使用主机变量。如果它是清单中的第一台主机,这将始终尝试该任务,并且将 "look-back" 在决定在下一个 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
更新(为了完成分享答案)
共享指定的解决方法。在我的用例中,我基本上想 "try" 对整个库存执行任务,直到成功
最终使用主机变量。如果它是清单中的第一台主机,这将始终尝试该任务,并且将 "look-back" 在决定在下一个 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
我似乎无法在元标记中使用 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
错误
ERROR! The conditional check 'check is succeeded' failed. The error was: The failed test expects a dictionary The error appears to have been in '/path/to/roles/tasks/main.yml': line 9, column 3, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - name: "End this play if the script ran successfully on 1 remote target" ^ here
更新(分享完成)
共享指定的解决方法。在我的用例中,我基本上想 "try" 对整个清单执行一项任务 直到 成功
最终使用主机变量。如果它是清单中的第一台主机,这将始终尝试该任务,并且将 "look-back" 在决定在下一个 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
更新(为了完成分享答案)
共享指定的解决方法。在我的用例中,我基本上想 "try" 对整个库存执行任务,直到成功
最终使用主机变量。如果它是清单中的第一台主机,这将始终尝试该任务,并且将 "look-back" 在决定在下一个 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