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"
                    ]
                ]
            }
        ]
    }
}

On Friday, May 28, 2021 at 4:23:10 PM UTC+5:30 R.B. Kumar wrote:

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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d350740e-748b-4ca8-84e3-e36dc8667bcbn%40googlegroups.com.

Reply via email to