# Register variable using {{ item }}

**URL:** <https://forum.ansible.com/t/register-variable-using-item/35246>\
**Category:** Ansible Project\
**Created:** [May 28, 2021, 10:53am UTC](https://forum.ansible.com/t/register-variable-using-item/35246 "2021-05-28T10:53:10Z")\
**Posts on this page:** 13\
**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, 10:53am UTC](https://forum.ansible.com/t/register-variable-using-item/35246/1 "2021-05-28T10:53:10Z")

</div>

Hello Everyone again - Trying to get the IP address of the multiple Interfaces from a Cisco Router and register it. Using {{ item }} loop method to feed the Multiple Interfaces (eg: GigabitEthernet 0/0/0) in the command.

How to get the unique variables for each interface? I will pick those variables and going to use it in the same script.

For example i want to pint the IP address of GigabitEthernet 0/0/0, GigabitEthernet 0/0/1, GigabitEthernet 0/0/2,

**Script:**

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

with\_items:

- gigabitEthernet 0/0/0

- gigabitEthernet 0/0/1

- gigabitEthernet 0/0/2

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, 1:05pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/2 "2021-05-28T13:05:18Z")

</div>

All - I realised it is not possible or good idea to use {{ item }} in register. So i modified the code as below. So, now for all the three interface (GigabitEthernet 0/0/0,  
Loopback 0) are captured in the same output and registered in **var\_ip\_addr**

The challenge now is, i have to grep IP address value from the output **var\_ip\_addr**. The IP address located under **stdout.** There are multiple **stdout** for each interface. How to grep the corresponding IP address from the output. **debug: var=“var\_ip\_addr.results[0].stdout[0].split(‘\n’)[1].split()[1]”** picks only the first interface. The control is not moving the subsequent Interfaces as the records are in the common hierarchy under **result.**

**Script**

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

with\_items:

- gigabitEthernet0/0/0
- Loopback 0

# - debug: var=var\_ip\_addr

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

**Output of var\_ip\_addr**

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [labrtr2] =\> {  
“var\_ip\_addr”: {  
“changed”: false,  
“msg”: “All items completed”,  
**“results”: [**  
{  
“\_ansible\_ignore\_errors”: null,  
“\_ansible\_item\_label”: “gigabitEthernet0/0/0”,  
“\_ansible\_item\_result”: true,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“auth\_pass”: null,  
“commands”: [  
“show ip interface brief gigabitEthernet0/0/0”  
],  
“host”: null,  
“provider”: {  
“auth\_pass”: 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”  
]  
]  
},  
{  
“\_ansible\_ignore\_errors”: null,  
“\_ansible\_item\_label”: “Loopback 0”,  
“invocation”: {  
“module\_args”: {  
“auth\_pass”: null,  
“commands”: [  
“show ip interface brief Loopback 0”  
],  
“host”: null,  
“provider”: {  
“auth\_pass”: null,  
},  
}  
},  
“item”: “Loopback 0”,  
**“stdout”: [**  
“Interface IP-Address OK? Method Status Protocol\nLoopback0 172.31.246.246 YES NVRAM up up”  
],  
“stdout\_lines”: [  
[  
“Interface IP-Address OK? Method Status Protocol”,  
“Loopback0 172.31.246.246 YES NVRAM up up”  
]  
]  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 28, 2021, 2:13pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/3 "2021-05-28T14:13:01Z")

</div>

For example

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interfaces: "{{ interfaces|default({})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({\_arr.0: \_arr.1}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ var\_ip\_addr.results|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('.stdout\_lines') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_arr: "{{ item.0.1.split() }}"

should give

&nbsp;&nbsp;interfaces:  
&nbsp;&nbsp;&nbsp;&nbsp;GigabitEthernet0/0/0: 42.151.182.42  
&nbsp;&nbsp;&nbsp;&nbsp;Loopback0: 172.31.246.246

---

<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, 3:21pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/4 "2021-05-28T15:21:43Z")

</div>

Thank you Vladimir - Much appreciated your response. Unfortunately, I am a ansible noob and trying to learn the things for a realtime network requirements

If you can help me where the lines you shown here will fit in my script. My script is below

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

with\_items:

- gigabitEthernet0/0/0
- Loopback 0  
I have to use var\_ip\_addr in other places in the script such as configuring ACL.

For example I would look for GigabitEthernet 0/0/0 IP address from the var\_ip\_addr and use it in ACL. Again in the same script i will look for Loopback 0 in var\_ip\_addr and use it to configure router ID… Just for example.

Can you help me explaining how the instruction you provided will help here? Thanks

regards,RB

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 28, 2021, 3:44pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/5 "2021-05-28T15:44:18Z")

</div>

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;interfaces: "{{ interfaces|default({})|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;combine({\_arr.0: \_arr.1}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ var\_ip\_addr.results|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('.stdout\_lines') }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_arr: "{{ item.0.1.split() }}"

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ interfaces[item] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- GigabitEthernet0/0/0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Loopback0

gives

&nbsp;&nbsp;msg: 42.151.182.42  
&nbsp;&nbsp;msg: 172.31.246.246

---

<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, 4:10pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/6 "2021-05-28T16:10:55Z")

</div>

Thank you Vladimir. I will try in to add in my script and try. Thanks again a lot

---

<div class="post-metadata">

**Author:** ![pradnya\_waghmare](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@pradnya\_waghmare](https://forum.ansible.com/u/pradnya_waghmare)\
**Post date:** [May 29, 2021, 6:13pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/7 "2021-05-29T18:13:32Z")

</div>

hi there,

I am facing issue about winrm.

getting winrm or request is not installed no module name winrm msg.

can anyone help  
trying to connect to window machine.

---

<div class="post-metadata">

**Author:** ![manoj\_kumar](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@manoj\_kumar](https://forum.ansible.com/u/manoj_kumar)\
**Post date:** [May 30, 2021, 3:41am UTC](https://forum.ansible.com/t/register-variable-using-item/35246/8 "2021-05-30T03:41:24Z")

</div>

Please install pywinrm module on your ansible node

Pip install pywinrm

---

<div class="post-metadata">

**Author:** ![pradnya\_waghmare](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@pradnya\_waghmare](https://forum.ansible.com/u/pradnya_waghmare)\
**Post date:** [May 30, 2021, 8:18am UTC](https://forum.ansible.com/t/register-variable-using-item/35246/9 "2021-05-30T08:18:57Z")

</div>

yes…I did same but after win\_ping command I am getting no winrm module found error.

---

<div class="post-metadata">

**Author:** ![manoj\_kumar](https://avatars.discourse-cdn.com/v4/letter/m/ecd19e/32.png) [@manoj\_kumar](https://forum.ansible.com/u/manoj_kumar)\
**Post date:** [May 30, 2021, 12:18pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/10 "2021-05-30T12:18:53Z")

</div>

if you installed winrm module through yum, it wouldnt not recognise it. I suggest you remove it and install it using pip.

Also, sharing error screenshot, inventory would help.

---

<div class="post-metadata">

**Author:** ![The\_Vandyy\_Vines](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@The\_Vandyy\_Vines](https://forum.ansible.com/u/The_Vandyy_Vines)\
**Post date:** [May 30, 2021, 12:51pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/11 "2021-05-30T12:51:45Z")

</div>

Check if winrm is installed on window server….

Test-wsman

---

<div class="post-metadata">

**Author:** ![pradnya\_waghmare](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@pradnya\_waghmare](https://forum.ansible.com/u/pradnya_waghmare)\
**Post date:** [May 30, 2021, 5:13pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/12 "2021-05-30T17:13:02Z")

</div>

hi,

inventory file is as below:

[win]  
ansible\_host=10.128.28.140

[win:vars]  
ansible\_user=pradnya.waghmare  
ansible\_pass=\*\*\*\*\*  
anaible\_port=5986  
ansible\_connection=winrm  
ansible\_winrm\_server\_cert\_validation= ignore

and below is error after pip install pywinrm

thanks

> **(attachments)**
>
> ![image001.png](https://us1.discourse-cdn.com/ansible/original/2X/a/ac5cbea7933ccae8a13121c9b7bd9bd18b5bbfce.png)

---

<div class="post-metadata">

**Author:** ![pradnya\_waghmare](https://avatars.discourse-cdn.com/v4/letter/p/b5e925/32.png) [@pradnya\_waghmare](https://forum.ansible.com/u/pradnya_waghmare)\
**Post date:** [May 30, 2021, 5:19pm UTC](https://forum.ansible.com/t/register-variable-using-item/35246/13 "2021-05-30T17:19:20Z")

</div>

typed Test-wsman command on powersell .got below response.

> **(attachments)**
>
> ![IMG_20210530_224744.jpg](https://us1.discourse-cdn.com/ansible/original/2X/2/25cc6d09a943c8c42e74dd67a985b1801343183c.jpeg)
