Hello Everyone - Need your help
When I use a variable inside ios_config I am getting an error. The error message is below.
Goal
-
Caputre an IP addresses from “show ip interface brief” from Cisco Router
-
Register the Output in var_ip_address
-
Grep one of the IP address using var_ip_address.stdout[0].split(‘\n’)[1].split()[1]
-
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