# Passing register variable to a role called by playbook?

**URL:** <https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015>\
**Category:** Ansible Project\
**Created:** [December 10, 2014, 8:11pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015 "2014-12-10T20:11:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 10, 2014, 8:11pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/1 "2014-12-10T20:11:41Z")

</div>

I am launching one-off ec2 instance for development purposes and in the main playbook I am launching an EC2 instance then assigning an elastic IP. Once all of that is done I am configuring the server via a role. I am able to capture the elastic IP via register but how do I call it from the role? Or more specifically how can I call it in a configuration template from within the role that is applied?

Here’s an example:

launch-server.yml:

- name: Launch ec2 instance  
hosts: localhost  
gather\_facts: False  
vars:  
key\_name: ssh-key  
instance\_type: t2.medium  
group\_id: sg-f834234  
image: ami-b234234  
region: us-east-1  
vpc\_subnet\_id: subnet-65wer234  
tasks:

- name: Launch instance  
local\_action: ec2 key\_name={{ key\_name }} group\_id={{ group\_id }} instance\_type={{ instance\_type }} image={{ image }} wait=true region={{ region }} vpc\_subnet\_id={{ vpc\_subnet\_id }} instance\_tags=‘{“Name”:“ec2-instance”}’  
register: ec2

- name: allocate a new elastic IP without associating it to anything  
action: ec2\_eip region=us-east-1  
register: eiptest

- name: output the IP  
debug: msg=“Allocated IP is {{ eiptest.public\_ip }}” \<— this prints the elastic IP to the screen.

- name: Add new instance to host group  
local\_action: add\_host hostname={{ item.private\_ip }} groupname=launched  
with\_items: ec2.instances

- name: Wait for SSH to come up  
local\_action: wait\_for host={{ item.private\_ip }} port=22 delay=60 timeout=320 state=started  
with\_items: ec2.instances

- name: Configure instance  
hosts: launched  
sudo: True  
gather\_facts: True  
roles:

- myrole

In the template from within the role it doesn’t know about eiptest.public\_ip so it fails and reading the docs it seems that var will be destroyed at the end of the play that calls the role. If I run ansible hostname -m setup I don’t see any entries for the elastic IP assigned to the instance.

I’ve tried a number of things and read through the docs but not having much luck. I would like to keep the roles separate from the plays that launch the instances if at all possible.

Thanks!

---

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 10, 2014, 8:18pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/2 "2014-12-10T20:18:53Z")

</div>

Quick correction on this. I pasted the wrong data where it says “name: allocate a new elastic IP without associating it to anything” The play actually looks like this:

- name: Associate a new Elastic IP to instance  
ec2\_eip: region=us-east-1 in\_vpc=yes “instance\_id={{ item }}”  
with\_items: ec2.instance\_ids  
register: eiptest

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [December 12, 2014, 12:25am UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/3 "2014-12-12T00:25:39Z")

</div>

Perhaps I’m not completely following the question:

" I am able to capture the elastic IP via register but how do I call it from the role?"

Basically any variable already registered can be used like “{{ foo }}” in subsequent roles/tasks/whatever – so maybe you could help me understand where the point of confusion is?

Want to help, just a bit unclear on it. Thanks!

---

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 12, 2014, 3:29pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/4 "2014-12-12T15:29:51Z")

</div>

Hi Michael,

Thanks for the reply.

I have a playbook that launches an EC2 instance and associates an elastic IP to that instance I then use register to capture that IP:

- name: Associate a new Elastic IP to instance  
ec2\_eip: region=us-east-1 in\_vpc=yes “instance\_id={{ item }}”  
with\_items: ec2.instance\_ids  
register: eiptest

I used the debug technique to print out the IP just to make sure it was working:

- name: Output the IP  
debug: msg=“The elastic IP is {{ eiptest }}”

Here’s the output:

TASK: [Output the IP] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “The elastic IP is {‘msg’: ‘All items completed’, ‘changed’: True, ‘results’: [{u’public\_ip’: u’255.100.104.57’, ‘item’: u’i-32d230d2’, u’changed’: True, ‘invocation’: {‘module\_name’: u’ec2\_eip’, ‘module\_args’: u’region=us-east-1 in\_vpc=yes "instance\_id=asdfadf"'}}]}”  
}

I tried a number of things and for some reason I can’t get it to print “The elastic IP is 255.100.104.57” using “The elastic IP is {{ eiptest.public\_ip }}”. I had it working before but now I can only get it to dump the full dict above.

In any case at the end of the play I call the role which attempts to use a j2 template to put a config in place. From the configuration file:

external-ip=“{{ eiptest.public\_ip }}”

And the error message:

ASK: [boom | Copy boom configuration] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [10.10.10.10] =\> {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘eiptest’ is undefined”, ‘failed’: True}

Hope all of this makes sense. I am using version 1.7.2 currently.

Thanks!

---

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 12, 2014, 8:09pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/5 "2014-12-12T20:09:05Z")

</div>

Okay I fixed the first issue in my previous post so:

- name: Output the IP  
debug: msg=“The elastic IP is {{ eiptest.results[0].public\_ip }}”

Now displays:

TASK: [Output the IP] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: “The elastic IP is 255.10.10.10”  
}

But it doesnt carry over when the role is run. I tried turning off gather\_facts before the role is called thinking maybe it was being overwritten or discarded but that didn’t work either.

I am also having a similar issue where we run a script that generates an API key and I save the output to a register: but I can’t get the nested value of “key”. But I’ll save that for another post 🙂

Once I wrap my head around the best way to access data in lists / dicts properly via Ansible I’ll be off and running.

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [December 12, 2014, 9:29pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/6 "2014-12-12T21:29:59Z")

</div>

I tried to simplify this down and replicate it but wasn't able to.  
Here's my playbooks and role, maybe you can spot an important  
difference:

$ cat var.yml

---

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 14, 2014, 8:01pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/7 "2014-12-14T20:01:26Z")

</div>

Looks good to me. But I am still fighting through this on my end.

It seems like it should be pretty straightforward.

- Launch the instance
- Allocate elastic IP then use the register keyword to save it to a var. (I even tried set\_fact with no luck)
- In the template add {{ var.public\_ip }}.

It will print the correct IP during a playbook run using debug but when it gets to the point of building the config from the template it seems the variable no longer exists.

I am going to try some other things or just put everything in the same one-off play.

Thanks for the replies.

---

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 15, 2014, 9:48pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/8 "2014-12-15T21:48:39Z")

</div>

Alright I have no idea what I am missing here but this is not working.

I added everything from the role into the same playbook that launches the server and it still has no clue what the variable is when I try to use it in a j2 template.

Keep in mind here there are two stages to this playbook. The local actions that LAUNCH the ec2 instance and then I configure the remote instance that was launched. All in the same play. Whereas before I was just launching then calling a role to run on it. I create groupname=launched then when it comes time to configure I use hosts: launched and everything else works but my vars don’t carry over.

Here are the relevant bits:

- name: Associate a new Elastic IP to instance  
ec2\_eip: region=us-east-1 in\_vpc=yes “instance\_id={{ item }}”  
with\_items: ec2.instance\_ids  
register: eiptest

- name: Set elastic IP fact  
set\_fact: elasticIP=“{{ eiptest.results[0].public\_ip }}”

- name: Output the IP  
debug: msg=“The elastic IP is {{ elasticIP }}” \<— This prints out the IP properly. No problem. Example: “msg”: “The elastic IP is 104.104.104.104”

- name: Add new instance to host group  
local\_action: add\_host hostname={{ item.private\_ip }} groupname=launched  
with\_items: ec2.instances

- name: Wait for SSH to come up  
local\_action: wait\_for host={{ item.private\_ip }} port=22 delay=60 timeout=320 state=started  
with\_items: ec2.instances

After SSH comes up the play continues with the following:

- name: Install and configure server  
hosts: launched  
remote\_user: ec2-user  
sudo: yes  
tasks:

- name: Copy rpm packages for installation  
copy: src=packages/{{ item }} dest=/tmp/{{ item }}  
with\_items:

- a-linux-rpm-el6.x86\_64.rpm

- another-rpm-el6.x86\_64.rpm

- name: Copy configuration file  
template: src=config.conf.j2 dest=/etc/linuxstuff/config.conf  
notify:

- restart server

And finally IN the config file itself is this line:

external-ip=“{{ elasticIP }}” NOTE: I have tried many different quote styles here

And I get this error:

TASK: [Copy server configuration] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [10.10.10.10 =\> {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘elasticIP’ is undefined”, ‘failed’: True}  
fatal: [10.10.10.10] =\> {‘msg’: “AnsibleUndefinedVariable: One or more undefined variables: ‘elasticIP’ is undefined”, ‘failed’: True}

---

<div class="post-metadata">

**Author:** ![jimmy](https://avatars.discourse-cdn.com/v4/letter/j/ee59a6/32.png) [@jimmy](https://forum.ansible.com/u/jimmy)\
**Post date:** [December 16, 2014, 3:06pm UTC](https://forum.ansible.com/t/passing-register-variable-to-a-role-called-by-playbook/17015/9 "2014-12-16T15:06:13Z")

</div>

I resolved this by doing the following:

Instance launch stage:

- Setting the fact for the elastic IP
- Writing the IP to a file using shell:

After calling the role when SSH is available:

- cat the same file and register result to a new variable (delegate\_to: localhost)
- Set a new fact
- great success
