【问题标题】:Ansible - When condition not working with meta taskAnsible - 当条件不适用于元任务时
【发布时间】:2020-02-11 22:41:03
【问题描述】:

我似乎在元标记中使用 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

错误

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

更新(为完成而分享)

共享规定的解决方法。在我的用例中,我基本上想在整个库存上“尝试”一项任务直到成功

最终使用主机变量。如果它是清单中的第一个主机,这将始终尝试该任务,并且将“回顾”前一个主机的尝试,然后再决定在下一个 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

【问题讨论】:

  • Meta 确实适用于 when。我认为“检查成功”是错误的。尝试运行时:1==1 看看是否有效。
  • 感谢您的建议!我选择走另一条路
  • 我已经在我的更新中分享了解决方法

标签: ansible ansible-2.x


【解决方案1】:

更新(分享答案以完成)

共享规定的解决方法。在我的用例中,我基本上想在整个库存上“尝试”一项任务,直到成功

最终使用主机变量。如果它是清单中的第一个主机,这将始终尝试该任务,并且将“回顾”前一个主机的尝试,然后再决定在下一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多