如何在使用ansible更新选项之前确保dhcp池名称存在
How to ensure that the dhcp pool name exist before updating options using ansible
这是我的剧本。当我执行它时,命令 ip dhcp pool {{ item.name }} 不检查名称是否存在。我使用参数“匹配:精确”,但它不起作用。那么我可以在 ansible playbook 中使用 if 语句吗?或者有没有办法在执行命令之前检查池名称。
我在剧本中使用 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_DHCP\nip dhcp pool WIFI_ADM\nip dhcp pool VOICEC_DHCP\nip dhcp pool DATA\nip 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 }} 我不想在交换机中创建它" 不匹配。
如果我模拟你的 return 值:
- 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 }}"
这是我的剧本。当我执行它时,命令 ip dhcp pool {{ item.name }} 不检查名称是否存在。我使用参数“匹配:精确”,但它不起作用。那么我可以在 ansible playbook 中使用 if 语句吗?或者有没有办法在执行命令之前检查池名称。 我在剧本中使用 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_DHCP\nip dhcp pool WIFI_ADM\nip dhcp pool VOICEC_DHCP\nip dhcp pool DATA\nip 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 }} 我不想在交换机中创建它" 不匹配。
如果我模拟你的 return 值:
- 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 }}"