# Variable disappears...

**URL:** <https://forum.ansible.com/t/variable-disappears/27484>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [May 8, 2018, 3:04pm UTC](https://forum.ansible.com/t/variable-disappears/27484 "2018-05-08T15:04:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 8, 2018, 3:04pm UTC](https://forum.ansible.com/t/variable-disappears/27484/1 "2018-05-08T15:04:03Z")

</div>

Hello all,

I’ve got a playbook that does:

- name: Get db cluster endpoint  
shell: “aws --region=us-east-1 rds describe-db-clusters --db-cluster-identifier {{ cluster }}-kam-db”  
register: aws\_cluster\_res

- set\_fact:  
myvar: “{{ aws\_cluster\_res.stdout | from\_json }}”

- set\_fact:  
endpoint: “{{ myvar.DBClusters[0].Endpoint }}”

and much later on:

- replace:  
dest: /home/admin/myconfig.cfg  
regexp: ‘^(._)!{{ DB\_HOST }}(._)$’  
replace: ‘\1!{{ endpoint }}\2’

But ansible complaints that it is undefined, eve though it _has_ beed defined:

output:

`  
TASK [set\_fact] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/admin/create\_instances.yaml:107  
ok: [127.0.0.1] =\> {  
“ansible\_facts”: {  
“endpoint”: “[something.amazonaws.com](http://something.amazonaws.com)”  
},  
“changed”: false  
}  
Read vars\_file ‘vars/sts.yml’  
Read vars\_file ‘vars/default.yml’

TASK [Print endpoint] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/admin/create\_instances.yaml:110  
ok: [127.0.0.1] =\> {  
“msg”: “[sonmething.amazonaws.com](http://sonmething.amazonaws.com)”  
}

`

…

`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?

Thanks!

David

---

<div class="post-metadata">

**Author:** ![Fatima\_Elfakih](https://avatars.discourse-cdn.com/v4/letter/f/eada6e/32.png) [@Fatima\_Elfakih](https://forum.ansible.com/u/Fatima_Elfakih)\
**Post date:** [May 8, 2018, 3:14pm UTC](https://forum.ansible.com/t/variable-disappears/27484/2 "2018-05-08T15:14:36Z")

</div>

Hi

> 1- which version of ansible you use?

2- which include module you use? import\ __/include\__ /include?

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 8, 2018, 3:52pm UTC](https://forum.ansible.com/t/variable-disappears/27484/3 "2018-05-08T15:52:43Z")

</div>

Thanks for replying:  
`

$ ansible-playbook --version  
ansible-playbook 2.5.2  
config file = /etc/ansible/ansible.cfg  
configured module search path = [u’/home/admin/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/dist-packages/ansible  
executable location = /usr/bin/ansible-playbook  
python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]  
`

I don’t “include” any modules, i just call them as i use them? I’m not sure i understand that quesiton, i’m pretty new to ansible.

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [May 8, 2018, 4:46pm UTC](https://forum.ansible.com/t/variable-disappears/27484/4 "2018-05-08T16:46:07Z")

</div>

Is the “replace” task in the same play as the “set\_fact” play? It’s best if you could provide a reproducible full playbook.

You can add debug task between where it’s defined and the “replace” task to see when it starts to “disappears”

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 8, 2018, 5:02pm UTC](https://forum.ansible.com/t/variable-disappears/27484/5 "2018-05-08T17:02:18Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 8, 2018, 7:37pm UTC](https://forum.ansible.com/t/variable-disappears/27484/6 "2018-05-08T19:37:40Z")

</div>

It’s confirmed, if you set a variable on a playbook, and add\_host and make new tasks, this variable is not “forwarded” to the next tasks

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 8, 2018, 8:00pm UTC](https://forum.ansible.com/t/variable-disappears/27484/7 "2018-05-08T20:00:25Z")

</div>

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.

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 8, 2018, 10:01pm UTC](https://forum.ansible.com/t/variable-disappears/27484/8 "2018-05-08T22:01:10Z")

</div>

Hello Jordan,

That’s exactly my problem! Now i understand why it’s not on the second play. I get the variable again on the second play and it’s working properly.

Thanks a lot for the exaplanation!  
David
