Register variable using {{ item }}

78 views
Skip to first unread message

R.B. Kumar

unread,
May 28, 2021, 6:53:10 AM5/28/21
to Ansible Project
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

R.B. Kumar

unread,
May 28, 2021, 9:05:19 AM5/28/21
to Ansible Project
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"
                    ]
                ]
            }
        ]
    }
}

Vladimir Botka

unread,
May 28, 2021, 10:13:21 AM5/28/21
to R.B. Kumar, ansible...@googlegroups.com
For example

- set_fact:
interfaces: "{{ interfaces|default({})|
combine({_arr.0: _arr.1}) }}"
loop: "{{ var_ip_addr.results|
json_query('[].stdout_lines') }}"
vars:
_arr: "{{ item.0.1.split() }}"

should give

interfaces:
GigabitEthernet0/0/0: 42.151.182.42
Loopback0: 172.31.246.246


--
Vladimir Botka

R.B. Kumar

unread,
May 28, 2021, 11:21:43 AM5/28/21
to Ansible Project
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 

Vladimir Botka

unread,
May 28, 2021, 11:44:35 AM5/28/21
to R.B. Kumar, ansible...@googlegroups.com
On Fri, 28 May 2021 08:21:43 -0700 (PDT)
"R.B. Kumar" <seeku...@gmail.com> wrote:

> ... 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

- set_fact:
interfaces: "{{ interfaces|default({})|
combine({_arr.0: _arr.1}) }}"
loop: "{{ var_ip_addr.results|
json_query('[].stdout_lines') }}"
vars:
_arr: "{{ item.0.1.split() }}"

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

gives

msg: 42.151.182.42
msg: 172.31.246.246

--
Vladimir Botka

R.B. Kumar

unread,
May 28, 2021, 12:10:55 PM5/28/21
to Ansible Project

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

pradnya waghmare

unread,
May 29, 2021, 2:13:57 PM5/29/21
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/2d47c8ec-abf0-4b1a-8bce-0cebd8ba8090n%40googlegroups.com.

manoj kumar

unread,
May 29, 2021, 11:41:46 PM5/29/21
to ansible...@googlegroups.com
Please install pywinrm module on your ansible node 

Pip install pywinrm 

--
Thanks and Regards
Manoj Kumar MS

pradnya waghmare

unread,
May 30, 2021, 4:19:20 AM5/30/21
to ansible...@googlegroups.com
yes..I did same but after win_ping command I am getting no winrm module found error.

manoj kumar

unread,
May 30, 2021, 8:19:19 AM5/30/21
to ansible...@googlegroups.com
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.

Thanks and Regards
Manoj Kumar MS

The Vandyy Vines

unread,
May 30, 2021, 8:52:07 AM5/30/21
to ansible...@googlegroups.com
Check if winrm is installed on window server….

Test-wsman 



pradnya waghmare

unread,
May 30, 2021, 1:13:29 PM5/30/21
to ansible...@googlegroups.com
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 

image001.png

pradnya waghmare

unread,
May 30, 2021, 1:19:58 PM5/30/21
to ansible...@googlegroups.com
typed Test-wsman command on powersell .got below response.


IMG_20210530_224744.jpg
Reply all
Reply to author
Forward
0 new messages