Using Variable inside IOS_CONFIG

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

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

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. :slight_smile: