Ansible 根据返回代码,在“带项目”任务上使用“失败时”
我正在尝试编写一个任务,该任务运行ldapmodify语句列表,并且只希望在任何返回代码不是0或68(object allready existed)时失败: 不工作,产生错误:Ansible 根据返回代码,在“带项目”任务上使用“失败时”,ansible,ansible-playbook,Ansible,Ansible Playbook,我正在尝试编写一个任务,该任务运行ldapmodify语句列表,并且只希望在任何返回代码不是0或68(object allready existed)时失败: 不工作,产生错误: error while evaluating conditional: result.results | rejectattr('rc', 'sameas', 0) | rejectattr('rc', 'sameas', 68) | list | length > 0 但是,如果我在时注释失败,\u并使用忽略
error while evaluating conditional: result.results | rejectattr('rc', 'sameas', 0) | rejectattr('rc', 'sameas', 68) | list | length > 0
但是,如果我在时注释失败,\u并使用忽略错误
,则以下任务会产生正确的结果。虽然我可以使用此解决方法来解决我的问题,但我想了解当
版本不工作时,失败的原因,因为我会发现这更优雅
- 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
。好吧,事实证明,我的操作太复杂了。问题是:Ansible在每次循环迭代后运行时失败。因此,我只需要访问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
的示例,我将把这个问题留给其他人,希望其他人会发现它有用。(我相信总有一天我会想再查一遍;)
感谢sivel on#ansible指出了这一点
- 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