Passing hash as an argument of an include, possible ?

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 :

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.

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!

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
  vars_files:
    - projects/$project/$level.yml
  tasks:
  - 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:
  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 :
---
- name: Install Elasticsearch
  tags: install
  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
  tags: install
  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

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...