需要帮助使用带有Ansible循环的when条件吗
我无法使用Ansible中带有循环的when条件跳过任务。我在playbook任务中没有看到任何跳过输出,但在play recap中显示跳过 我的剧本任务:需要帮助使用带有Ansible循环的when条件吗,ansible,Ansible,我无法使用Ansible中带有循环的when条件跳过任务。我在playbook任务中没有看到任何跳过输出,但在play recap中显示跳过 我的剧本任务: - name: Capture the Existing DHCP Config set_fact: existing_dhcp_config: "{{ output['stdout'][0] | regex_findall('forwarding-options dhcp-relay serv
- name: Capture the Existing DHCP Config
set_fact:
existing_dhcp_config: "{{ output['stdout'][0] | regex_findall('forwarding-options dhcp-relay server-group site-dhcp \\S+') }}"
- name: View the existing DHCP Config
debug:
var: existing_dhcp_config
- name: Desired DHCP Config Based on Site Standards
set_fact:
desired_dhcp_config: "{{ lookup('file', './DHCP-Config/{{ inventory_hostname }}-dhcp.cfg') | regex_findall('forwarding-options dhcp-relay server-group site-dhcp \\S+') }}"
- name: View Desired DHCP Config
debug:
var: desired_dhcp_config
- name: Compare Existing and Desired DHCP Configs to Remove Stale Config
set_fact:
dhcp_config_to_remove: "{{ existing_dhcp_config | difference(desired_dhcp_config) }}"
- name: View the Difference Against Existing and Desired DHCP Configs
debug:
var: dhcp_config_to_remove
when: dhcp_config_to_remove != ""
- name: Delete Non-Site Standards DHCP Config
junos_config:
commands: delete {{ item }}
comment: "{{ chg_ticket }}"
confirm_commit: yes
loop: "{{ dhcp_config_to_remove }}"
when: dhcp_config_to_remove != ""
- name: Verify the Final DHCP Config on JUNOS Devices
junos_command:
commands: show configuration | display set
register: final_output
- name: View the Final DHCP Config
debug:
msg: "{{ final_output['stdout'][0] | regex_findall('forwarding-options dhcp-relay server-group site-dhcp \\S+') }}"
剧本的输出:
TASK [Capture the Existing DHCP Config] ******************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1]
TASK [View the existing DHCP Config] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1] => {
"existing_dhcp_config": [
"forwarding-options dhcp-relay server-group site-dhcp 10.1.1.2"
]
}
TASK [Desired DHCP Config Based on Site Standards] *******************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1]
TASK [View Desired DHCP Config] **************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1] => {
"desired_dhcp_config": [
"forwarding-options dhcp-relay server-group site-dhcp 10.1.1.2"
]
}
TASK [Compare Existing and Desired DHCP Configs to Remove Stale Config] **********************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1]
TASK [View the Difference Against Existing and Desired DHCP Configs] *************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1] => {
"dhcp_config_to_remove": []
}
TASK [Delete Non-Site Standards DHCP Config] *************************************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Verify the Final DHCP Config on JUNOS Devices] *****************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1]
TASK [View the Final DHCP Config] ************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [junos-1] => {
"msg": [
"forwarding-options dhcp-relay server-group site-dhcp 10.1.1.2"
]
}
PLAY RECAP ***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
junos-1 : ok=23 changed=3 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
在以下两个任务中,我没有看到任何跳过的描述,即使我为它们定义了when语句:
TASK [View the Difference Against Existing and Desired DHCP Configs]
TASK [Delete Non-Site Standards DHCP Config]
这两个语句没有跳过语句,因为它们没有显式跳过 在
when:dhcp\u config\u to\u remove!=""
您正在检查此变量是否为空的字符串,而不是空的列表:
ok:[junos-1]=>{
“dhcp配置到删除”:[]
}
因此,你的条件确实不符合,而跳过实际上是来自其他方面
如前所述,主机实际上有两种可能不显示某些跳过的任务:
)进行显式跳过,但在配置中有display\u skipped\u hosts=no
display\u skipped\u hosts=yes
when:dhcp_config_to_remove | length
因为此测试将为空列表生成0
,并且0
的计算结果为False
;当任何其他数字的计算结果为True
时,这将起到作用
但是,当您在空列表上循环时,
循环
的跳过将发生在当
时的跳过之前
例如:
-主机:所有
收集事实:不
任务:
-调试:
何时:假
循环:{{undefined_list}默认([])}
总结如下:
播放[全部]*******************************************************************************************************
任务[调试]*****************************************************************************************************
重演*******************************************************************************************************
本地主机:确定=0更改=0无法访问=0失败=0跳过=1获救=0忽略=0
但这是预期的行为。根据您的ansible版本,您应该在ansible.cfg中检查两件事:对于旧版本,您可能已经设置了
stdout\u callback=skippy
(已弃用),您可以切换回默认值。对于较新的版本,您可能需要将display\u skipped\u hosts=no
返回到yes。在Ansible文档中,它说默认行为是display\u skipped\u hosts=yes
,实际上它并没有显示仅带循环的任务已跳过任务[删除非站点标准DHCP配置]
对于其他任务,它就在那里。跳过对第一个任务有效,但对第二个任务保持不变,在任务上显示一个空提示,但在播放回放中提到跳过。如果列表为空,可能是因为循环
是在之前解释的,什么时候
有办法解决这个问题吗?