Variable disappears...

38 views
Skip to first unread message

David Villasmil

unread,
May 8, 2018, 11:04:03 AM5/8/18
to Ansible Project
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"
   
},
   
"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"
}



....


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


Fatima Elfakih

unread,
May 8, 2018, 11:14:36 AM5/8/18
to Ansible Project
Hi 
1- which version of ansible you use?
    2- which include module you use? import_*/include_*/include? 
 

David Villasmil

unread,
May 8, 2018, 11:52:43 AM5/8/18
to Ansible Project
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.

Tony Chia

unread,
May 8, 2018, 12:46:07 PM5/8/18
to Ansible Project
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"

David Villasmil

unread,
May 8, 2018, 1:02:45 PM5/8/18
to ansible...@googlegroups.com

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?


--
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 post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/491d0f10-323c-4542-abb9-9101665c245a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Villasmil

unread,
May 8, 2018, 3:37:41 PM5/8/18
to Ansible Project
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

Jordan Borean

unread,
May 8, 2018, 4:00:25 PM5/8/18
to Ansible Project
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

David Villasmil

unread,
May 8, 2018, 6:01:34 PM5/8/18
to ansible...@googlegroups.com

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


--
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 post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages