Register variable using {{ item }}

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

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

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

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

- 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

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

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.

Please install pywinrm module on your ansible node

Pip install pywinrm

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

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.

Check if winrm is installed on window server….

Test-wsman

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)

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

(attachments)