Variable of variable doesn't work in ansible 1.4

5,139 views
Skip to first unread message

Giorgio Crivellari

unread,
Dec 3, 2013, 5:17:59 AM12/3/13
to ansible...@googlegroups.com

I used to use "hosts" property, in ansible playbook, dinamically for example:

hosts: ${$jobprofile.hosts}

where $jobprofile was an external variable that allow me to change hosts based on profile.

Since last 1.4 version, my previous playbook doesn't works any more.
Have you some suggestion to do an EVAL into a playbook?

Thanks
Giorgio

Michael DeHaan

unread,
Dec 3, 2013, 8:26:54 PM12/3/13
to ansible...@googlegroups.com
Yeah this is a very outdated usage of legacy variables, and a bit of an anti-pattern whenever you want to define a variable by a variable name.  (There's a reason even Perl tries to discourage this, and that's Perl).

I don't know where you are defining "jobprofile", but can we step back and talk about the use case?

Public service announcement, variables look like this:

foo: "{{ bar }}"

And if you want to get at a variable by name that is scoped to a host, you can do so if you want -- which you won't need often -- like so:

{{ hostvars[inventory_hostname][fact_name] }}

This doesn't of course apply to the host specifier -- we haven't even got to figure out what host we've talked to yet, hence I'm wanting to understand more.

This seems like a case better suited to seperate inventory files, or the case of a dynamic inventory script, the dynamic inventory script responding to an environment variable.




--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

Giorgio Crivellari

unread,
Dec 5, 2013, 4:56:29 AM12/5/13
to ansible...@googlegroups.com
Hi Michael,
thanks for you response and for this awesome tool!

We're using it for deployment jobs, as post-build of jenkins jobs, and we're trying to do something like this:

-------------- job playbook ---------------
- vars:
   devel:
     host: "host02"
   prod:
     host: "host01"

  hosts: $vars[$prof][host]

  tasks:
  - debug: msg="Hello"
---------------------------------------------------------

ansible playbook is called passing extra var called prof (that is profile) and, according with that, we want to use proper host group.

We would like to use a single job playbook for each jenkins job to avoid multiple file to maintain.

Thanks for support!

Giorgio

Dmitry Horbach

unread,
Dec 5, 2013, 4:28:56 PM12/5/13
to ansible...@googlegroups.com
Hi Michael,

We are also using double evaluation in many places - mostly to not repeat definitions of the same variable

Example 1

user.yml - user uid values

userA: 800
userB: 801

main.yml - role variables

user: userA
user_uid: ${${user}} # will be resolved to 800

Example 2

type1_data:
  var1: value1
  var2: value1
type2_data:
  var1: value2
  var2: value2

type_data: ${${type}_data}
type_var1: ${type_data.var1} 
type_var2: ${type_data.var2}

By overriding single variable we get different plays (all var blocks can be defined in role and block selection can go to single var in vars_files)
This also allows command line override with -e option by passing just one key=value. 

What will be the appropriate way to use those after old syntax removed?

Michael DeHaan

unread,
Dec 5, 2013, 4:30:50 PM12/5/13
to ansible...@googlegroups.com
I feel like I've answered this a lot in the last 2 days, see the archives and we can add an entry in the FAQ.

-- Michael

Dmitry Horbach

unread,
Dec 5, 2013, 6:03:59 PM12/5/13
to ansible...@googlegroups.com
Though I couldn't find anything related within past days - I just found solution to my problem using following vars :)

users:
    userA: 800
user: userA
user_uid: "{{ users[user] }}"
    
data:
    type1:
        key: value1
    type2:
        key: value2
type: type1
type_key: "{{ data[type].key }}"

Thanks

Giorgio Crivellari

unread,
Dec 6, 2013, 4:46:13 AM12/6/13
to ansible...@googlegroups.com

Dmitry you're a f***** genius!!

Michael, please add Dimitry example in variables website documentation page... many users will appreciate!

Thanks guys!
Giorgio

Sankalp Khare

unread,
Oct 29, 2014, 4:09:02 AM10/29/14
to ansible...@googlegroups.com
I second Giorgio. Dmitry, you're a lifesaver :D

senorsmile

unread,
May 11, 2015, 2:28:55 PM5/11/15
to ansible...@googlegroups.com
Dmitry, this is the solution to the ultimate problem I've been having.  Thanks!!!!

Iliya Strakovich

unread,
Jul 16, 2016, 5:02:47 AM7/16/16
to Ansible Project
Dmitry,

You really saved my day !!!
Reply all
Reply to author
Forward
0 new messages