这是我的剧本。执行ip dhcp pool {{item.name}}命令时,不检查名称是否存在。我使用参数"match: exact"但这行不通。因此,我可以在可视剧本中使用if语句,或者有一种方法可以在执行命令之前检查池名称。我在playbook中使用when来检查是否定义了item.name,但它也不起作用。
---
- name: "UPDATE DHCP OPTIONS FOR FNAC-FRANCHISE SWITCHES"
hosts: all
gather_facts: false
vars:
xx: ["ip dhcp pool VOICEC_DHCP","ip dhcp pool DATA","ip dhcp pool VIDEO_DHCP ","ip dhcp pool WIFI_USER"," ip dhcp pool WIFI_ADM", ]
tasks:
- name: "CHECK"
ios_command:
commands:
- show run | include ip dhcp pool
register: output
- name: DISPLAY THE COMMAND OUTPUT
debug:
var: output.stdout_lines
- name: transform output
set_fact:
pools: "{{ item | regex_replace('ip dhcp pool ', '') }}"
loop: "{{ output.stdout_lines }}"
- name: "UPDATE DHCP OPTIONS IN POOL DATA & WIFI_USER"
ios_config:
lines:
- dns-server 10.0.0.1
- netbios-name-server 10.0.0.1
- netbios-node-type h-node
parents: ip dhcp pool {{ item.name }}
match: exact
loop: "{{ pools }}"
这是我得到的输出
ok: [ITG] => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "stdout": ["ip dhcp pool VIDEO_DHCPnip dhcp pool WIFI_ADMnip dhcp pool VOICEC_DHCPnip dhcp pool DATAnip dhcp pool WIFI_USER"], "stdout_lines": [["ip dhcp pool VIDEO_DHCP", "ip dhcp pool WIFI_ADM", "ip dhcp pool VOICEC_DHCP", "ip dhcp pool DATA", "ip dhcp pool WIFI_USER"]]}
TASK [DISPLAY THE COMMAND OUTPUT] *********************************************************************************************************************************************
ok: [ITG] => {
"output.stdout_lines": [
[
"ip dhcp pool VIDEO_DHCP",
"ip dhcp pool WIFI_ADM",
"ip dhcp pool VOICEC_DHCP",
"ip dhcp pool DATA",
"ip dhcp pool WIFI_USER"
]
]
}
TASK [transform output] *******************************************************************************************************************************************************
ok: [ITG] => (item=[u'ip dhcp pool VIDEO_DHCP', u'ip dhcp pool WIFI_ADM', u'ip dhcp pool VOICEC_DHCP', u'ip dhcp pool DATA', u'ip dhcp pool WIFI_USER']) => {"ansible_facts": {"pools": ["VIDEO_DHCP", "WIFI_ADM", "VOICEC_DHCP", "DATA", "WIFI_USER"]}, "ansible_loop_var": "item", "changed": false, "item": ["ip dhcp pool VIDEO_DHCP", "ip dhcp pool WIFI_ADM", "ip dhcp pool VOICEC_DHCP", "ip dhcp pool DATA", "ip dhcp pool WIFI_USER"]}
有交换机池名称WIFI_USER不存在,我不想在交换机中创建它,如果行"parents: ip dhcp pool {{item.name}}"不匹配
如果我模拟返回值:
- name: vartest
hosts: localhost
vars: #d use just to test
xx: ["ip dhcp pool VOICEC_DHCP","ip dhcp pool DATA","ip dhcp pool VIDEO_DHCP ","ip dhcp pool WIFI_USER"," ip dhcp pool WIFI_ADM", ]
tasks:
- name: trap output
ios_config:
parents: show ip dhcp pool
register: output
- name: transform output
set_fact:
pools: "{{ pools | default([]) + [item | regex_replace('ip dhcp pool ', '')] }}"
loop: "{{ output.stdout_lines[0] }}" #you adapt following the result output.stdout or something else
- name: "UPDATE DHCP OPTIONS IN POOL DATA & WIFI_USER"
ios_config:
commands:
- dns-server <ip-addr>
- netbios-name-server <ip-addr>
- netbios-node-type h-node
parents: ip dhcp pool {{ item }}
loop: "{{ pools }}"