11-26-2023 10:45 PM
Hello,
I would appreciate, if someone could please advise on the following.
From running config I want to extract "router bgp <as#>. And use it as variable as illustrated below.
The challenge is that the created variable does include hyphens and quotes which needs to be removed before using as Cisco command:
fatal: [10.0.0.1]: FAILED! => {"changed": false, "module_stderr": "[router bgp 65411]\r\n[router bgp 65411]\r\n^\r\n% Invalid input detected at '^' marker.\r\n\r\c4500(config)#", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error"}
- name: Task3.1 - show run | incl router bgp
ios_command:
commands:
- show run | incl router bgp
become: true
register: output3
- name: Task3.2 Create bgp_as Variable
ansible.builtin.set_fact:
bgp_as: "{{ output3.stdout }}"
- name: Task4 - BGP no redistribute eigrp 10 route-map EIGRP-2-BGP
ios_config:
lines:
- no redistribute eigrp 10 route-map EIGRP-2-BGP
parents:
- "{{bgp_as| replace(\"'\",'') }}"
- address-family ipv4
when: "output2.stdout | regex_search('.*redistribute eigrp 10 route-map RM-EIGRP-2-BGP.*') "
become: true
register: output4
I apprecite any advice.
Thanks.
Solved! Go to Solution.
11-26-2023 11:09 PM - edited 11-26-2023 11:09 PM
Hi you can do it like this, in this case you don't need to handle any hypens or quotes:
- name: Task3.1 - show run | incl router bgp
ios_command:
commands:
- show run | incl router bgp
register: output3
# use the first line (output3.stdout_lines[0]) and search for the digits,
# assign the digits to your var
- name: Task3.2 Create bgp_as Variable
ansible.builtin.set_fact:
bgp_as: "{{ output3.stdout_lines[0] | regex_search('[0-9]+') }}"
# use the var with the digits as part of your parents:
# "router bgp {{ bgp_as }}"
- name: Task4 - BGP no redistribute eigrp 10 route-map EIGRP-2-BGP
ios_config:
lines:
- no redistribute eigrp 10 route-map EIGRP-2-BGP
parents:
- "router bgp {{ bgp_as }}"
- address-family ipv4
11-26-2023 11:09 PM - edited 11-26-2023 11:09 PM
Hi you can do it like this, in this case you don't need to handle any hypens or quotes:
- name: Task3.1 - show run | incl router bgp
ios_command:
commands:
- show run | incl router bgp
register: output3
# use the first line (output3.stdout_lines[0]) and search for the digits,
# assign the digits to your var
- name: Task3.2 Create bgp_as Variable
ansible.builtin.set_fact:
bgp_as: "{{ output3.stdout_lines[0] | regex_search('[0-9]+') }}"
# use the var with the digits as part of your parents:
# "router bgp {{ bgp_as }}"
- name: Task4 - BGP no redistribute eigrp 10 route-map EIGRP-2-BGP
ios_config:
lines:
- no redistribute eigrp 10 route-map EIGRP-2-BGP
parents:
- "router bgp {{ bgp_as }}"
- address-family ipv4
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide