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

384 views
Skip to first unread message

R.B. Kumar

unread,
May 28, 2021, 4:08:05 AM5/28/21
to Ansible Project
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] ***************************************************************************************************************
    "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   

Jatin Garg

unread,
May 28, 2021, 4:11:47 AM5/28/21
to ansible...@googlegroups.com
Please try to print stdout value first and see what does it contain. Stdout_lines may have your data.

--
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/e11452df-60f8-4f11-afa8-fe1afb2749f1n%40googlegroups.com.

R.B. Kumar

unread,
May 28, 2021, 4:18:18 AM5/28/21
to Ansible Project
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] ***************************************************************************************************************
    "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

R.B. Kumar

unread,
May 28, 2021, 4:26:57 AM5/28/21
to Ansible Project
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"
                    ]
                ]
            }
        ]
    }
}

On Friday, May 28, 2021 at 1:41:47 PM UTC+5:30 jatin....@gmail.com wrote:

Jatin Garg

unread,
May 28, 2021, 4:27:59 AM5/28/21
to ansible...@googlegroups.com
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?

R.B. Kumar

unread,
May 28, 2021, 4:40:49 AM5/28/21
to Ansible Project
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"
}

Jatin Garg

unread,
May 28, 2021, 4:47:11 AM5/28/21
to ansible...@googlegroups.com
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.

Antony Stone

unread,
May 28, 2021, 4:47:56 AM5/28/21
to ansible...@googlegroups.com
I would suggest there is a meaningful difference between "gigabitEthernet0/0/0"
and "Gigabitethernet 0/0/0".


Antony.

--
It is also possible that putting the birds in a laboratory setting
inadvertently renders them relatively incompetent.

- Daniel C Dennett

Please reply to the list;
please *don't* CC me.

R.B. Kumar

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

R.B. Kumar

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

Mauricio Tavares

unread,
May 28, 2021, 6:31:40 AM5/28/21
to ansible...@googlegroups.com
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.
> --
> 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/41e0e726-c996-4f31-9468-2d1f18261401n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages