Ansible变量未在不匹配的条件下从更新
我有下面的剧本,它实际上比较了两个路由器(基本上是成对的)的运行配置,如果有匹配和不匹配,它应该报告出来Ansible变量未在不匹配的条件下从更新,ansible,Ansible,我有下面的剧本,它实际上比较了两个路由器(基本上是成对的)的运行配置,如果有匹配和不匹配,它应该报告出来 --- - hosts: '{{d1}}' gather_facts: no connection: network_cli tasks: - name: Task1 - Fetching running configuration of device A cli_command: command: show run
---
-
hosts: '{{d1}}'
gather_facts: no
connection: network_cli
tasks:
- name: Task1 - Fetching running configuration of device A
cli_command:
command: show run
#output: json
register: runA
#- debug:
# msg: "{{runA.stdout_lines}}"
-
hosts: '{{d2}}'
gather_facts: no
connection: network_cli
tasks:
- name: Task2 - Fetching running configuraiton of device B
cli_command:
command: show run
#output: json
register: runB
#- debug:
# msg: "{{runB.stdout_lines}}"
- hosts: localhost
gather_facts: no
roles:
- ansible-network.network-engine
vars:
runconfA: "{{hostvars[d1]['runA']['stdout']}}"
runconfB: "{{hostvars[d2]['runB']['stdout']}}"
atomic_update_mismatch: false
tasks:
#- debug:
# msg: "{{hostvars[d1]['runA']['stdout']}}"
- name: Task3 - Parse Atomic Update from Running config of device A
command_parser:
file: "parsers/atomic_update.yaml"
content: "{{ runconfA }}"
- name: Task4 - Save Atomic Update output
set_fact:
atomic_update_deviceA: "{{output}}"
- name: Task5 - Parse Atomic Update from Running config of device B
command_parser:
file: "parsers/atomic_update.yaml"
content: "{{ runconfB }}"
- name: Task6 - Save Atomic Update output
set_fact:
atomic_update_deviceB: "{{output}}"
- debug:
msg:
- "atomic update from device a: {{atomic_update_deviceA}}"
- "atomic update from device b: {{atomic_update_deviceB}}"
- name: Task7 - Compare output from both devices
set_fact:
atomic_update_mismatch: true
when: atomic_update_deviceA != atomic_update_deviceB
- debug:
msg: "does running config has atomic update mismatch: {{atomic_update_mismatch}}"
#msg: "check if {{atomic_update_deviceA}} == {{atomic_update_deviceB}}"
#- "Atomic Update of device B: {{atomic_update_deviceB}}"
还有另一个包含regex的playbook,我使用regex
,在匹配时将结果放入变量并将变量返回到第一个playbook
---
#- debug: var=runconfA
- name: Match atomic update from running-config
export: yes
pattern_match:
match_all: yes
#match_greedy: yes
#regex: ".*\\b(atomic)\\b.*"
regex: "^.*\\batomic\\b.*$"
register: output
使用以下命令执行:
ansible-playbook config_mfg_ce.yml -i inventory.txt --extra-vars "d1=A d2=B"
其中d1为路由器,d2为B路由器
当我运行这个程序时,如果在路由器A中有原子匹配,但在路由器B中没有,它仍然会更新为“没有不匹配”,这是错误的,这是因为输出变量也会将路由器B的值更新回路由器A,并且不会刷新以获取路由器B的值
非常感谢您提供的任何帮助。请粘贴调试输出请清理您的示例,使其最小化,否则您的问题可能无法得到解答,甚至因质量低下而关闭。