TASK [replace] ************************************************************************************************************************************************************************** task path: /home/admin/voice-conf/playbooks/create_instances.yaml:335 fatal: [18.206.82.39]: FAILED! => { "msg": "The task includes an option with an undefined variable. The error was: 'endpoint' is undefined\n\nThe error appears to have been in '/home/admin/create_instances.yaml': line 335, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - replace:\n ^ here\n" } fatal: [18.206.108.97]: FAILED! => { "msg": "The task includes an option with an undefined variable. The error was: 'endpoint' is undefined\n\nThe error appears to have been in '/home/admin/create_instances.yaml': line 335, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - replace:\n ^ here\n" }
I can’t figure out why this is happening, can someone help?
My playbook has 2 “tasks”:
I first gather some info, including the hosts to work on, then add them with add_hosts, and then start again some new tasks, could this be it?
There are some vars set on the cli that are not lost, could those be global and the ones set in the playbook not be global?
Without seeing the full playbook it is hard to actually confirm what you are doing but it sounds like you have 2 plays in your playbook, 1 to get the host info and add it, the other to run tasks on that new host. When you run set_fact or register in a play, that variable/fact is only registered for the hosts it is running on. If you were to run a 2nd play on a different set of hosts those variables are no longer accessible like you would do it normally. What you need to do is access those variables from the hostvars dict and specify the original host, I’m going to guess the first play is run on localhost so it would be {{ hostvars[‘localhost’][‘endpoint’] }}. Here is an example of it in action
`
name: 1st play that runs on localhost and defines the var
hosts: localhost
tasks:
name: set variable/fact on the first play for localhost
set_fact:
my_var: abc
name: 2nd play that runs on another host
hosts: other-group
tasks:
name: output my_var registered on localhost
debug:
var: hostvars[‘localhost’][‘my_var’]
`
As you can see, the 2nd play runs on a different group and I use hostvars to lookup the variables defined on localhost and finally get the my_var variable.