# Using Variable inside  IOS\_CONFIG

**URL:** <https://forum.ansible.com/t/using-variable-inside-ios-config/35252>\
**Category:** Ansible Project\
**Created:** [May 29, 2021, 6:55pm UTC](https://forum.ansible.com/t/using-variable-inside-ios-config/35252 "2021-05-29T18:55:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![R.B\_Kumar](https://avatars.discourse-cdn.com/v4/letter/r/d07c76/32.png) [@R.B\_Kumar](https://forum.ansible.com/u/R.B_Kumar)\
**Post date:** [May 29, 2021, 6:55pm UTC](https://forum.ansible.com/t/using-variable-inside-ios-config/35252/1 "2021-05-29T18:55:08Z")

</div>

Hello Everyone - Need your help

When I use a variable inside **ios\_config** I am getting an error. The error message is below.

**Goal**

1. Caputre an IP addresses from “show ip interface brief” from Cisco Router

2. Register the Output in **var\_ip\_address**

3. Grep one of the IP address using **var\_ip\_address.stdout[0].split(‘\n’)[1].split()[1]**

4. using **ios\_config** configure **ntp server** **var\_ip\_address.stdout[0].split(‘\n’)[1].split()[1]**

**Problem**  
The output is as expected in var\_ip\_address.stdout[0].split(‘\n’)[1].split()[1]. I am good till this part

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [labrtr2] =\> {  
“var\_ip\_address.stdout[0].split(‘\n’)[1].split()[1]”: “44.151.182.42”  
}

When i apply the variable in to configure NTP Server using IOS\_CISCO, i am getting the error the below error

TASK [NTP\_HOSTNAME] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [labrtr2]: FAILED! =\> {\*\*“msg”: “The task includes an option with an undefined variable. The error was: list object has no element 1\*\*n\nThe error appears to have been in ‘/software/RB/NAT\_1.yml’: line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - debug: var=var\_ip\_address.stdout[0]\n - name: NTP\_HOSTNAME\n ^ here\n”}  
to retry, use: --limit @/software/RB/NAT\_1.retry

**Full Script**

tasks:

- name: get\_ip  
ios\_command:  
provider: “{{ cli }}”  
commands: “show ip interface brief”  
register: var\_ip\_address

- debug: var=var\_ip\_address.stdout[0].split(‘\n’)[1].split()[1]

- name: NTP\_HOSTNAME  
ios\_config:  
provider: “{{ cli }}”  
lines:

- ntp server **“{{ var\_ip\_address.stdout[0].split(‘\n’)[1].split()[1] }}”**  
register: NTP

- debug: var=NTP

regards, RB

---

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [May 30, 2021, 1:30am UTC](https://forum.ansible.com/t/using-variable-inside-ios-config/35252/2 "2021-05-30T01:30:47Z")

</div>

Try – set\_fact the grep output and try to call the variable in ios\_config

set\_fact:  
desired\_ntp: {{ var\_ip\_address.stdout[0].split(‘\n’)[1].split()[1] }}

---

<div class="post-metadata">

**Author:** ![R.B\_Kumar](https://avatars.discourse-cdn.com/v4/letter/r/d07c76/32.png) [@R.B\_Kumar](https://forum.ansible.com/u/R.B_Kumar)\
**Post date:** [May 30, 2021, 8:56am UTC](https://forum.ansible.com/t/using-variable-inside-ios-config/35252/3 "2021-05-30T08:56:39Z")

</div>

Thanks Pushparaj for your help. **set\_fact** is the savior here. It works by using the variable captured under set\_fact.

Thanks again for mind share and your comment solved my requirement. Have a nice time ahead. 🙂
