# "VARIABLE IS NOT DEFINED!" when use {{ item }}

**URL:** <https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244>\
**Category:** Ansible Project\
**Created:** [May 28, 2021, 8:08am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244 "2021-05-28T08:08:05Z")\
**Posts on this page:** 11\
**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 28, 2021, 8:08am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/1 "2021-05-28T08:08:05Z")

</div>

Hello All - I am using Ansible version 2.7.18

I am trying this code to get IP address of the Cisco Router interface. When i use {{ item }}, the IP address is not captured. Where as if i use a static value (eg: show ip interface brief gig 0/0/0), the IP address is captured and i can use it in the script.

Can you help me what is the mistake here? Thanks

- name: get\_ip  
ios\_command:  
provider: “{{ cli }}”  
commands: "show ip interface brief **{{ item }}"**  
register: var\_ip\_addr\_0

**with\_items:**  
**- gigabitEthernet0/0/0**

- debug: var=“var\_ip\_addr\_0.stdout[0].split(‘\n’)[1].split()[1]”

But, the output of the debug shows the value is not captured.

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [[labrtr2.allmerica.com](http://labrtr2.allmerica.com)] =\> {  
“var\_ip\_addr\_0.stdout[0].split(‘\n’)[1].split()[1]”: **“VARIABLE IS NOT DEFINED!”**  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[labrtr2.allmerica.com](http://labrtr2.allmerica.com) : ok=3 changed=0 unreachable=0 failed=0

---

<div class="post-metadata">

**Author:** ![Jatin\_Garg](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@Jatin\_Garg](https://forum.ansible.com/u/Jatin_Garg)\
**Post date:** [May 28, 2021, 8:11am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/2 "2021-05-28T08:11:26Z")

</div>

Please try to print stdout value first and see what does it contain. Stdout\_lines may have your data.

---

<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 28, 2021, 8:18am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/3 "2021-05-28T08:18:18Z")

</div>

Thank you… Just by printing stdout, it shows all the data perfectly. But when i try to grep it using split, it is showing “VARIABLE IS NOT FOUND”

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

ok: [[labrtr2.allmerica.com](http://labrtr2.allmerica.com)] =\> {  
“var\_ip\_addr\_0.stdout[0].split(‘\n’)[1].split()[1]”: “VARIABLE IS NOT DEFINED!”  
}

if i remove stdout[0].split(‘\n’)[1].split()[1]": I am getting the output showing the IP address in the output

---

<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 28, 2021, 8:26am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/4 "2021-05-28T08:26:57Z")

</div>

Hello - I attached the output of debug when i used {{ item }}. That is show ip interface brief {{ item }}. I am trying to capture the IP address in the output and reuse it in the script.

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [labrtr2] =\> {  
“var\_ip\_addr\_0”: {  
“changed”: false,  
“msg”: “All items completed”,  
“results”: [  
{  
“\_ansible\_ignore\_errors”: null,  
“\_ansible\_item\_label”: “gigabitEthernet0/0/0”,  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“auth\_pass”: null,  
“authorize”: null,  
“commands”: [  
“show ip interface brief gigabitEthernet0/0/0”  
],  
“host”: null,  
“interval”: 1,  
“match”: “all”,  
“password”: null,  
“port”: null,  
“provider”: {  
“auth\_pass”: null,  
“authorize”: null,  
“host”: null,  
“password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“port”: null,  
“ssh\_keyfile”: null,  
“timeout”: null,  
“username”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”  
},  
“retries”: 10,  
“ssh\_keyfile”: null,  
“timeout”: null,  
“username”: null,  
“wait\_for”: null  
}  
},  
“item”: “gigabitEthernet0/0/0”,  
“stdout”: [  
"Interface IP-Address OK? Method Status Protocol\nGigabitEthernet0/0/0

42.151.182.42 YES DHCP up up"  
],  
“stdout\_lines”: [  
[  
“Interface IP-Address OK? Method Status Protocol”,  
“GigabitEthernet0/0/0 **42.151.182.42** YES DHCP up up”  
]  
]  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![Jatin\_Garg](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@Jatin\_Garg](https://forum.ansible.com/u/Jatin_Garg)\
**Post date:** [May 28, 2021, 8:27am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/5 "2021-05-28T08:27:38Z")

</div>

So would it work if you just use stdout or is split required?

If split is required, pls show me the stdout output and what you need from this value?

---

<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 28, 2021, 8:40am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/6 "2021-05-28T08:40:49Z")

</div>

Hello - The difference between working and not working script is given below. Need to find out why we get “VARIABLE IS NOT DEFINED” in script-1

**NOT WORKING SCRIPT** **(SCRIPT-1)**

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

with\_items:

- gigabitEthernet0/0/0

- debug: var=“var\_ip\_addr\_0.stdout.line[0]”

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [labrtr2] =\> {  
“var\_ip\_addr\_0.stdout.line[0]”: **“VARIABLE IS NOT DEFINED!”**  
}

**WORKING SCRIPT (SCRIPT-2)**

- name: get\_ip  
ios\_command:  
provider: “{{ cli }}”  
commands: “show ip interface brief **Gigabitethernet 0/0/0** ”  
register: var\_ip\_addr\_0

- debug: var=“var\_ip\_addr\_0.stdout[0].split(‘\n’)[1].split()[1]”

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

---

<div class="post-metadata">

**Author:** ![Jatin\_Garg](https://avatars.discourse-cdn.com/v4/letter/j/f08c70/32.png) [@Jatin\_Garg](https://forum.ansible.com/u/Jatin_Garg)\
**Post date:** [May 28, 2021, 8:46am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/7 "2021-05-28T08:46:49Z")

</div>

Pls see the difference in stdout outputs in both scripts and in failing script. As you are using loop so register output becomes bit changed so you will have to use your logic as per loop output.

You can call me. No. shared seperately.

---

<div class="post-metadata">

**Author:** ![Antony\_Stone](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Antony\_Stone](https://forum.ansible.com/u/Antony_Stone)\
**Post date:** [May 28, 2021, 8:47am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/8 "2021-05-28T08:47:25Z")

</div>

I would suggest there is a meaningful difference between "gigabitEthernet0/0/0"  
and "Gigabitethernet 0/0/0".

Antony.

---

<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 28, 2021, 8:59am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/9 "2021-05-28T08:59:01Z")

</div>

Thank you. But gigabit and Gigbit case difference is not a matter here…

All i am trying to do is, using {{ item }} i a getting a output from a Cisco Router and want to grep only a IP address value in the in output and store it in a variable. so that i can use the variable in the script

regards, RB

---

<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 28, 2021, 10:25am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/10 "2021-05-28T10:25:14Z")

</div>

Hi Jatin / All Thank you

solved the problem. The issue is by using {{ item }} the output format is different. By using hierarchy I am able to get the IP address greped. Thanks again to all

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [May 28, 2021, 10:31am UTC](https://forum.ansible.com/t/variable-is-not-defined-when-use-item/35244/11 "2021-05-28T10:31:16Z")

</div>

Call me clueless but from

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"stdout\_lines": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Interface IP-Address OK?  
Method Status Protocol",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"GigabitEthernet0/0/0 42.151.182.42 YES  
DHCP up up"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]

You have two fields being returned. Pick the second one and print it  
using debug. Then split the SOB.
