ansible - 如何在 Ansible 中打破 `with_lines` 循环?

标签 ansible ansible-playbook

我想在 Ansible 中使用以下处理程序:

- name: force ntp update
  shell: ntpdate {{item}}
  with_lines: /etc/ntpd.serverlist

但我希望它在第一次成功执行后结束执行(列表包含您可以尝试同步的 ntpd 服务器。一个就足够了)。我该怎么做?

最佳答案

至少在 Ansible 2.2.1.0 上 when语句可以打破循环,以下工作在第一次成功后跳过所有评估:

---
# test.yml

# run this playbook with: ansible-playbook -i localhost, test.yml

- hosts: localhost
connection: local
gather_facts: no
tasks:
  - name: check
    shell: "[ {{item}} -ne 2 ]"
    register: checks
    ignore_errors: yes
    changed_when: false
    when: checks is not defined or checks.rc != 0
    with_items: [2,3,2,4]
  - name: set
    set_fact: first_working={{ item.item }}
    when: "'rc' in item and item.rc == 0"
    with_items: "{{ checks.results }}"
  - debug: var=first_working
  # Note that first_working will be undefined if no check succeeded

这是输出:
PLAY [localhost] ***************************************************************

TASK [check] *******************************************************************
failed: [localhost] (item=2) => {"changed": false, "cmd": "[ 2 -ne 2 ]", "delta": "0:00:00.001735", "end": "2017-03-13 16:14:00.515372", "failed": true, "item": 2, "rc": 1, "start": "2017-03-13 16:14:00.513637", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}
ok: [localhost] => (item=3)
skipping: [localhost] => (item=2)
skipping: [localhost] => (item=4)
...ignoring

TASK [set] *********************************************************************
skipping: [localhost] => (item={'_ansible_parsed': True, u'cmd': u'[ 2 -ne 2 ]', u'end': u'2017-03-13 16:14:00.515372', '_ansible_no_log': False, u'stdout': u'', '_ansible_item_result': True, u'changed': False, 'item': 2, u'delta': u'0:00:00.001735', u'stderr': u'', u'rc': 1, 'invocation': {'module_name': u'command', u'module_args': {u'creates': None, u'executable': None, u'_uses_shell': True, u'_raw_params': u'[ 2 -ne 2 ]', u'removes': None, u'warn': True, u'chdir': None}}, 'stdout_lines': [], u'start': u'2017-03-13 16:14:00.513637', u'warnings': [], 'failed': True})
ok: [localhost] => (item={'_ansible_parsed': True, u'changed': False, u'stdout': u'', '_ansible_no_log': False, u'warnings': [], '_ansible_item_result': True, u'rc': 0, u'end': u'2017-03-13 16:14:00.615658', u'start': u'2017-03-13 16:14:00.613978', u'cmd': u'[ 3 -ne 2 ]', 'item': 3, u'delta': u'0:00:00.001680', 'invocation': {'module_name': u'command', u'module_args': {u'creates': None, u'executable': None, u'_uses_shell': True, u'_raw_params': u'[ 3 -ne 2 ]', u'removes': None, u'warn': True, u'chdir': None}}, 'stdout_lines': [], u'stderr': u''})
skipping: [localhost] => (item={'skipped': True, '_ansible_no_log': False, 'skip_reason': u'Conditional check failed', '_ansible_item_result': True, 'item': 2, 'changed': False})
skipping: [localhost] => (item={'skipped': True, '_ansible_no_log': False, 'skip_reason': u'Conditional check failed', '_ansible_item_result': True, 'item': 4, 'changed': False})

TASK [debug] *******************************************************************
ok: [localhost] => {
    "first_working": "3"
}

关于ansible - 如何在 Ansible 中打破 `with_lines` 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29993341/

相关文章:

linux - ansible 卡在 "sudo yum install"' 步骤

Ansible 在其处理程序中未检测到角色默认变量

ansible - 如何使用 root 用户运行 Ansible,然后更改为另一个用户?

ansible 如何将现有主机添加到组

Ansible - playbook 调用另一个带有变量、标签和限制的 playbook

git - 如何在ansible中转发ssh key 以从源头 checkout 目标机器上的git存储库?

SSH 进入私有(private)主机 Ansible

jenkins - 当jenkins重写其配置时,如何使这个ansible jenkins脚本具有幂等性?

git - 将本地 git 存储库与 Ansible Tower 结合使用

ansible - 运行 Ansible 角色而不运行角色元中定义的依赖项