cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
407
Views
0
Helpful
1
Replies

Ansible ansible.builtin.set_fact to create variable as Cisco command

Netmart
Level 1
Level 1

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.

 

1 Accepted Solution

Accepted Solutions

Marcel Zehnder
Spotlight
Spotlight

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

 

View solution in original post

1 Reply 1

Marcel Zehnder
Spotlight
Spotlight

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