# Referencing Ansible Variables

**URL:** <https://forum.ansible.com/t/referencing-ansible-variables/17216>\
**Category:** Ansible Project\
**Created:** [January 8, 2015, 7:05am UTC](https://forum.ansible.com/t/referencing-ansible-variables/17216 "2015-01-08T07:05:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 8, 2015, 7:05am UTC](https://forum.ansible.com/t/referencing-ansible-variables/17216/1 "2015-01-08T07:05:42Z")

</div>

Hi there,

I am fairly new with ansible and I am having some with variables :-).

Here is my 1st problem, I have a playbook I do have a variable:

`

- name: Create instance(s)  
hosts: local  
gather\_facts: no  
connection: local  
tags: provisioning  
vars:  
…  
groupname: launched  
port: 8080  
`

Later I use it in the add\_host line:

`

- name: Add host to groupname  
add\_host: hostname={{ item.public\_ip }} ansible\_ssh\_private\_key\_file=PATH\_TO\_KEYFILE groupname={{ groupname }}  
with\_items: gce.instance\_data

`

and then I use it again at:

---

<div class="post-metadata">

**Author:** ![Fred\_Badel](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@Fred\_Badel](https://forum.ansible.com/u/Fred_Badel)\
**Post date:** [January 8, 2015, 9:00am UTC](https://forum.ansible.com/t/referencing-ansible-variables/17216/2 "2015-01-08T09:00:19Z")

</div>

Hi Peter,

IMHO it is because your variables are defined for the host “local” within your 1st play, the hosts group “launched” cannot know about variables defined for another group

Hope this help.

Fred

---

<div class="post-metadata">

**Author:** ![Tom\_Bamford](https://avatars.discourse-cdn.com/v4/letter/t/c2a13f/32.png) [@Tom\_Bamford](https://forum.ansible.com/u/Tom_Bamford)\
**Post date:** [January 8, 2015, 9:12am UTC](https://forum.ansible.com/t/referencing-ansible-variables/17216/3 "2015-01-08T09:12:07Z")

</div>

Hi Peter

As Fred says, the variables are not in scope for both plays. You will need to declare your variables such that both plays can see them. You could put them into a separate file and reference it with vars\_files in both plays, or you could pass them in as extra vars on the command line. You will not be able to put them into group\_vars/ _or host\_vars/_, as they are not able to be resolved at the time the hosts are matched.

Also, you can simplify your role call to

```auto
roles:
  - { role: webserver, port: port }

```

(i.e. without interpolating the port var into a string)

Although the port variable will be in scope within the role just by declaring it in your play, so you can drop it altogether as a role parameter

```auto
roles:
  - webserver

```

Regards  
Tom

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 8, 2015, 9:27am UTC](https://forum.ansible.com/t/referencing-ansible-variables/17216/4 "2015-01-08T09:27:09Z")

</div>

Thank you guys!!!

Peter
