# Passing hash as an argument of an include, possible ?

**URL:** <https://forum.ansible.com/t/passing-hash-as-an-argument-of-an-include-possible/12186>\
**Category:** Ansible Project\
**Created:** [January 7, 2013, 3:12pm UTC](https://forum.ansible.com/t/passing-hash-as-an-argument-of-an-include-possible/12186 "2013-01-07T15:12:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmaurice](https://avatars.discourse-cdn.com/v4/letter/g/f04885/32.png) [@gmaurice](https://forum.ansible.com/u/gmaurice)\
**Post date:** [January 7, 2013, 3:12pm UTC](https://forum.ansible.com/t/passing-hash-as-an-argument-of-an-include-possible/12186/1 "2013-01-07T15:12:25Z")

</div>

Hello everybody,

I’m running ansible version 0.9, and i would like to pass a hash as an argument of an include.

I’m trying to do that in the top level playbook :

- hosts: $hosts  
vars\_files:
- projects/$project/$level.yml  
tasks:
- include: tasks/elasticsearch.yml elasticsearch=$hash\_elasticsearch

$project and $level are get from group\_vars, and no problem to get vars from the file.

The vars\_file used is projects/proj1/prod.yml that is look like :

main\_elasticsearch:  
version: 0.19.11-1  
cluster\_name: main\_app

specific\_elasticsearch:  
version: 0.19.11-2  
cluster\_name: specific\_app

The included playbook looks like :

---

<div class="post-metadata">

**Author:** ![gmaurice](https://avatars.discourse-cdn.com/v4/letter/g/f04885/32.png) [@gmaurice](https://forum.ansible.com/u/gmaurice)\
**Post date:** [January 9, 2013, 12:13pm UTC](https://forum.ansible.com/t/passing-hash-as-an-argument-of-an-include-possible/12186/2 "2013-01-09T12:13:03Z")

</div>

Checkouting ansible from version to another without erase clone directory will lead to a non working ansible installation.  
I had to clone ansible and checkout the good version in order to make it working again.  
The way I use vars, vars\_files and group\_vars is incompatible with version 0.9, so, I have to stay at version 0.7.

To do what I wanted is to load another vars\_file in order to override the variables given in the projects/$project/$level.yml file.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 9, 2013, 12:49pm UTC](https://forum.ansible.com/t/passing-hash-as-an-argument-of-an-include-possible/12186/3 "2013-01-09T12:49:42Z")

</div>

Parameterized includes are there, and I don't see why you say you  
can't move off 0.7, as we've kept things compatible and have only  
fixed a few bugs along the way. Though if you had a specific example  
you could share, perhaps I would understand better what you say is not  
possible in 0.9?

You had typed this:

${$elasticsearch.version}

If you want to do lookups inside a hash based on another variable, you  
totally can. But what you are trying to do is like a Perl-style  
dereference by name, and isn't possible. Instead what you want, if  
the top variable is looked up by another variable, is this:

$hostvars.$ansible\_hostname.$elasticsearch\_type.version

If it were at a known depth you could have just done:

$some\_root\_variable.$elasticsearch\_type.version

So yes, you can do that...

Hope that helps!

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 9, 2013, 1:25pm UTC](https://forum.ansible.com/t/passing-hash-as-an-argument-of-an-include-possible/12186/4 "2013-01-09T13:25:47Z")

</div>

gmaurice@linkfluence.net wrote:

> Hello everybody,
> 
> I'm running ansible version 0.9, and i would like to pass a hash as an  
> argument of an include.

devel should have part of this fixed, where it will resolve ${x.foo} even  
though $x is just '$y' but ${y.foo} exists.

> I'm trying to do that in the top level playbook :  
> - hosts: $hosts  
> &nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;&nbsp;&nbsp;- projects/$project/$level.yml  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- include: tasks/elasticsearch.yml elasticsearch=$hash\_elasticsearch

Note, hash\_elasticsearch isn't defined below.

> $project and $level are get from group\_vars, and no problem to get vars  
> from the file.
> 
> The vars\_file used is projects/proj1/prod.yml that is look like :  
> main\_elasticsearch:  
> &nbsp;&nbsp;version: 0.19.11-1  
> &nbsp;&nbsp;cluster\_name: main\_app  
> specific\_elasticsearch:  
> &nbsp;&nbsp;version: 0.19.11-2  
> &nbsp;&nbsp;cluster\_name: specific\_app
> 
> The included playbook looks like :  
> ---  
> - name: Install Elasticsearch  
> &nbsp;&nbsp;tags: install  
> &nbsp;&nbsp;action: apt pkg=elasticsearch=$elasticsearch.version update-cache=yes

To access sub-variables you need ${elasticsearch.version}.

> And i have the following error :  
> TASK: [Install Elasticsearch] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> failed: [\*\*\*\*.\*\*\*.\*\*] =\> {"failed": true, "item": ""}  
> msg: 'apt-get install 'elasticsearch=$elasticsearch.version' ' failed: E:  
> Version '$hash\_elasticsearch.version" for elasticsearch' was not found
> 
> This form does not work too :  
> ---  
> - name: Install Elasticsearch  
> &nbsp;&nbsp;tags: install  
> &nbsp;&nbsp;action: apt pkg=elasticsearch=${$elasticsearch.version} update-cache=yes

This is a different thing entirely, attempting to access a variable by the  
name of whatever is in $elasticsearch (in this case '$hash\_elasticsearch'),  
and then version within that.

> Is it possible to do that ?

I'm not entirely sure what it is that you want to do. You can use variables.  
You can use variables within variables. Variables can refer to eachother.  
If you tell us what is that you want, and how you are telling ansible to do  
it, we might be of more help.

Daniel

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 9, 2013, 2:39pm UTC](https://forum.ansible.com/t/passing-hash-as-an-argument-of-an-include-possible/12186/5 "2013-01-09T14:39:49Z")

</div>

> I'm not entirely sure what it is that you want to do. You can use variables.  
> You can use variables within variables. Variables can refer to eachother.  
> If you tell us what is that you want, and how you are telling ansible to do  
> it, we might be of more help.

It seems he's trying to do a perl style $$var like

my $foo = "bar";  
my $bar = 42;  
my $baz = 43;

print $$foo;

# prints42;

Hence my suggestion of looking up the variable in hostvars above...
