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

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] => {
“var_ip_addr_0.stdout[0].split(‘\n’)[1].split()[1]”: “VARIABLE IS NOT DEFINED!”
}

PLAY RECAP *****************************************************************************************************************
labrtr2.allmerica.com : ok=3 changed=0 unreachable=0 failed=0

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

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] => {
“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

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

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?

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

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.

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

Antony.

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

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

Call me clueless but from

                "stdout_lines": [
                    [
                        "Interface IP-Address OK?
Method Status Protocol",
                        "GigabitEthernet0/0/0 42.151.182.42 YES
DHCP up up"
                    ]
                ]

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