Using Variable inside IOS_CONFIG

22 views
Skip to first unread message

R.B. Kumar

unread,
May 29, 2021, 2:55:09 PM5/29/21
to Ansible Project
Hello Everyone - Need your help

When I use a variable inside ios_config I am getting an error. The error message is below.

Goal
1. Caputre an IP addresses from "show ip interface brief" from Cisco Router
2.  Register the Output in var_ip_address
3.  Grep one of the IP address using var_ip_address.stdout[0].split('\n')[1].split()[1]
4. using ios_config configure ntp server var_ip_address.stdout[0].split('\n')[1].split()[1]

Problem
The output is as expected in var_ip_address.stdout[0].split('\n')[1].split()[1]. I am good till this part

TASK [debug] *****************************************************************
ok: [labrtr2] => {
    "var_ip_address.stdout[0].split('\n')[1].split()[1]": "44.151.182.42"
}

When i apply the variable in to configure NTP Server using IOS_CISCO, i am getting the error the below error
TASK [NTP_HOSTNAME] ************************************************************************************************************************************************
fatal: [labrtr2]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: list object has no element 1\n\nThe error appears to have been in '/software/RB/NAT_1.yml': line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    - debug: var=var_ip_address.stdout[0]\n    - name: NTP_HOSTNAME\n      ^ here\n"}
        to retry, use: --limit @/software/RB/NAT_1.retry

Full Script
  tasks:
    - name: get_ip
      ios_command:
        provider: "{{ cli }}"
        commands: "show ip interface brief"
      register: var_ip_address

    - debug: var=var_ip_address.stdout[0].split('\n')[1].split()[1]

    - name: NTP_HOSTNAME
      ios_config:
        provider: "{{ cli }}"
        lines:
          - ntp server "{{ var_ip_address.stdout[0].split('\n')[1].split()[1] }}"
      register: NTP
    - debug: var=NTP

regards, RB

Rajthecomputerguy

unread,
May 29, 2021, 9:31:34 PM5/29/21
to ansible...@googlegroups.com
Try -- set_fact the grep output and try to call the variable in ios_config

set_fact:
     desired_ntp: {{ var_ip_address.stdout[0].split('\n')[1].split()[1] }}

--
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/a1d5111a-198d-4054-87e8-a57a11535122n%40googlegroups.com.


--

Thanks,

Pushparaj G


R.B. Kumar

unread,
May 30, 2021, 4:56:39 AM5/30/21
to Ansible Project
Thanks Pushparaj for your help. set_fact is the savior here. It works by using the variable captured under set_fact.

Thanks again for mind share and  your comment solved my requirement. Have a nice time ahead. :)  

Reply all
Reply to author
Forward
0 new messages