<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Ansible ansible.builtin.set_fact  to create variable as Cisco comm in Code Exchange</title>
    <link>https://community.cisco.com/t5/code-exchange/ansible-ansible-builtin-set-fact-to-create-variable-as-cisco/m-p/4966587#M46</link>
    <description>&lt;P&gt;Hi you can do it like this, in this case you don't need to handle any hypens or quotes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="c"&gt;- 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&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 27 Nov 2023 07:09:50 GMT</pubDate>
    <dc:creator>Marcel Zehnder</dc:creator>
    <dc:date>2023-11-27T07:09:50Z</dc:date>
    <item>
      <title>Ansible ansible.builtin.set_fact  to create variable as Cisco command</title>
      <link>https://community.cisco.com/t5/code-exchange/ansible-ansible-builtin-set-fact-to-create-variable-as-cisco/m-p/4966582#M45</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I would appreciate, if someone could please advise on the following.&lt;/P&gt;&lt;P&gt;From running config I want to extract "router bgp &amp;lt;as#&amp;gt;. And use it as variable as illustrated below.&lt;/P&gt;&lt;P&gt;The challenge is that the created variable does include hyphens and quotes which needs to be removed before using as Cisco command:&lt;/P&gt;&lt;P&gt;fatal: [10.0.0.1]: FAILED! =&amp;gt; {"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"}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- name: Task3.1 - show run | incl router bgp&lt;BR /&gt;ios_command:&lt;BR /&gt;commands:&lt;BR /&gt;- show run | incl router bgp&lt;/P&gt;&lt;P&gt;become: true&lt;BR /&gt;register: output3&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;- name: Task3.2 Create bgp_as Variable&lt;BR /&gt;ansible.builtin.set_fact:&lt;BR /&gt;bgp_as: "{{ output3.stdout }}"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- name: Task4 - BGP no redistribute eigrp 10 route-map EIGRP-2-BGP&lt;BR /&gt;ios_config:&lt;BR /&gt;lines:&lt;BR /&gt;- no redistribute eigrp 10 route-map EIGRP-2-BGP&lt;BR /&gt;parents:&lt;BR /&gt;- "{{bgp_as| replace(\"'\",'') }}"&lt;BR /&gt;- address-family ipv4&lt;/P&gt;&lt;P&gt;when: "output2.stdout | regex_search('.*redistribute eigrp 10 route-map RM-EIGRP-2-BGP.*') "&lt;BR /&gt;&lt;BR /&gt;become: true&lt;BR /&gt;register: output4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I apprecite any advice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2023 06:45:47 GMT</pubDate>
      <guid>https://community.cisco.com/t5/code-exchange/ansible-ansible-builtin-set-fact-to-create-variable-as-cisco/m-p/4966582#M45</guid>
      <dc:creator>Netmart</dc:creator>
      <dc:date>2023-11-27T06:45:47Z</dc:date>
    </item>
    <item>
      <title>Re: Ansible ansible.builtin.set_fact  to create variable as Cisco comm</title>
      <link>https://community.cisco.com/t5/code-exchange/ansible-ansible-builtin-set-fact-to-create-variable-as-cisco/m-p/4966587#M46</link>
      <description>&lt;P&gt;Hi you can do it like this, in this case you don't need to handle any hypens or quotes:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="c"&gt;- 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&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2023 07:09:50 GMT</pubDate>
      <guid>https://community.cisco.com/t5/code-exchange/ansible-ansible-builtin-set-fact-to-create-variable-as-cisco/m-p/4966587#M46</guid>
      <dc:creator>Marcel Zehnder</dc:creator>
      <dc:date>2023-11-27T07:09:50Z</dc:date>
    </item>
  </channel>
</rss>

