在`with_items`任务上使用`failed_when`取决于返回码

时间:2014-09-22 19:35:59

标签: ansible ansible-playbook

我正在尝试编写一个运行ldapmodify语句列表的任务,并且如果任何返回代码不是0或68(对象已经存在),则只希望它失败:

- name: add needed LDAP infrastructure
  action: command ldapmodify -x -D '{{ ADMINDN }}' -w '{{ LDAPPW }}' -H {{ LDAPURI }} -c -f {{ item }}
  register: result
  failed_when: "result.results | rejectattr('rc', 'sameas', 0) | rejectattr('rc', 'sameas', 68) | list | length > 0"
  # ignore_errors: true
  with_items:
    - a.ldif
    - b.ldif

不起作用,产生错误:

error while evaluating conditional: result.results | rejectattr('rc', 'sameas', 0) | rejectattr('rc', 'sameas', 68) | list | length > 0

但是,如果我对failed_when发表评论并使用ignore_errors,则以下任务会产生正确的结果。虽然我可以使用此解决方法来解决我的问题,但我想了解为什么failed_when版本不起作用,因为我会发现它更优雅。

- debug: var="result.results | rejectattr('rc', 'sameas', 0) | rejectattr('rc', 'sameas', 68) | list | length > 0"
- fail: msg="failure during ldapmodify"
  when: "result.results | rejectattr('rc', 'sameas', 0) | rejectattr('rc', 'sameas', 68) | list | length > 0"

在其他版本的jinja2中,旁注sameas可能是equalto,以防您想知道。

1 个答案:

答案 0 :(得分:27)

嗯,事实证明我说它太复杂了。问题是:Ansible在循环的每次迭代后运行failed_when。因此,我只需要访问result.rc

- name: add needed LDAP infrastructure
  action: command ldapmodify -x -D '{{ ADMINDN }}' -w '{{ LDAPPW }}' -H {{ LDAPURI }} -c -f {{ item }}
  register: result
  # As per comment from user "ypid"
  failed_when: ( result.rc not in [ 0, 68 ] )
  # failed_when: ( result.rc != 0 ) and ( result.rc != 68 )
  with_items:
    - a.ldif
    - b.ldif

产生想要的结果。

在循环之后,变量result将填充一个摘要字典,其中包含results键中每个的详细信息。

但是由于我无法找到任何使用带有过滤器链的result.results的例子,我只想提出这个问题,希望其他人可能会发现它有用。 (我确定有一天我最终会想再查一下;))

感谢sansl on #ansible指出这一点。

相关问题