Variables inside variables?

118 views
Skip to first unread message

Alejandro Ferrari

unread,
Aug 4, 2015, 10:28:08 AM8/4/15
to Ansible Project
Hi, I like to know if possible made reference of a variable inside other variable.

Example:

{{ansible_local.oracle.credentials."{{websphere_mb.odbc.schema_name}}"}}

with this I try to build the final variable name that if I put manually works, but like to don't hardcode Schame_name and read from vars file.

this can be possible?

Thanks
Ale

Brian Coca

unread,
Aug 4, 2015, 10:31:41 AM8/4/15
to Ansible Project
no moustache stacking

{{ansible_local.oracle.credentials[{{websphere_mb.odbc.schema_name]}}
> --
> 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.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/340275a2-6ba7-44ee-940b-d7d06c3495bb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Alejandro Ferrari

unread,
Aug 4, 2015, 12:11:20 PM8/4/15
to Ansible Project
Thanks Brian, 

THis is how work for me.

{{ansible_local.oracle.credentials[websphere_mb.odbc.schema_name]}}

And ansible_local really come from other node that I ask using

- name: Get sql master facts
  setup:
  delegate_to: "{{websphere_mb.odbc.host}}"
  register: db_facts
  tags: mbconfig

Some better way to do this?

THanks
Ale

Virendra Bhalothia

unread,
Aug 4, 2015, 1:08:36 PM8/4/15
to Ansible Project
Ale/Brian

I'm trying to use variables in variables as well...not sure how to do it. I'm using set facts

Below is the when condition:


hardcoded:

 when: "'{{ hostvars[groups['app-a1'][0]]['target_app_build'] }}' != '{{ hostvars[groups['app-a1'][0]]['actual_app_build'] }}'"


what I need:


 when: "'{{ hostvars[groups['app-a1'][{{ partition }}]]['target_app_build'] }}' != '{{ hostvars[groups['app-'][{{ partition }}]]['actual_app_build'] }}'"


Here {{ partition }} - is an extra variable



It doesn't seem to be working for me.

Thanks,
Virendra

Brian Coca

unread,
Aug 4, 2015, 1:10:46 PM8/4/15
to Ansible Project
same rule no nesting of moustaches:

when: "{{ hostvars[groups['app-a1'][partition]]['target_app_build']
!= hostvars[groups['app-'][partition]]['actual_app_build']}}"




--
Brian Coca

Rob White

unread,
Sep 11, 2015, 3:25:38 AM9/11/15
to Ansible Project
If there is no moustache stacking does that mean variables inside variables is impossible?

I.e.

environments:
  prod:
    vpc_id: 12345
  stg:
    vpc_id: 67890
env: prod

debug: msg={{ 'environments.' + env + '.vpc_id' }}

This just gives me a string of 'environments.prod.vpc_id' but what I want is '12345'

Stephan Hradek

unread,
Sep 11, 2015, 3:35:33 AM9/11/15
to Ansible Project
I'm completely new to all this, but looking at http://jinja.pocoo.org/docs/dev/templates/#variables

I'd try to do it like this:

environments:
 prod:
   vpc_id: 12345
 stg:
   vpc_id: 67890
env: prod

debug: msg={{ environments[env].vpc_id }}




Reply all
Reply to author
Forward
0 new messages