Interpolating concatenated variables

55 views
Skip to first unread message

James Morgan

unread,
Nov 5, 2014, 11:06:41 AM11/5/14
to ansible...@googlegroups.com
Hi,

I'd like to be able to build a variable name programatically in a similar fashion to


Instead of accessing a dict I would like to build up the variable name. For example

- hosts: localhost
  connection: local
  vars:
    test_app: admin
    admin_port: 1234
  tasks:
    - debug:
        msg: "Admin port = {{ test_app + '_port' }}"

This outputs

TASK: [debug ] **************************************************************** 
ok: [localhost] => {
    "msg": "Admin port = admin_port"
}

I would like to get the value of admin_port. So would need to do some kind of double substitution. 

I need to get the information from a var file just given the app_name. The above is just to demonstrate.

Many thanks

James


rek...@voodoowarez.com

unread,
Nov 5, 2014, 11:31:52 AM11/5/14
to ansible...@googlegroups.com
On Wed, Nov 05, 2014 at 08:06:41AM -0800, James Morgan wrote:
> Hi,
>
> I'd like to be able to build a variable name programatically in a similar
> fashion to
>
> http://docs.ansible.com/faq.html#how-do-i-access-a-variable-name-programmatically
>
> Instead of accessing a dict I would like to build up the variable name. For
> example
>
> - hosts: localhost
> connection: local
> vars:
> test_app: admin
> admin_port: 1234
> tasks:
> - debug:
> msg: "Admin port = {{ test_app + '_port' }}"
>
> This outputs
>
> TASK: [debug ]
> ****************************************************************
> ok: [localhost] => {
> "msg": "Admin port = admin_port"
> }
>
> I would like to get the value of *admin_port*. So would need to do some
> kind of double substitution.

{{ hostvars[inventory_hostname][test_app + '_port'] }} i think

James Morgan

unread,
Nov 5, 2014, 11:56:59 AM11/5/14
to ansible...@googlegroups.com
Ok that doesn't work in my example as the variables aren't in the inventory

TASK: [debug ] **************************************************************** 
fatal: [localhost] => One or more undefined variables: 'dict' object has no attribute 'admin_port'

Though If use include_vars which I would be doing its does work

- hosts: localhost
  connection: local
  vars:
    test_app: admin
  tasks:
    - include_vars: concat_vars.yml
    - debug:
        msg: "admin port from inventory = {{ hostvars[inventory_hostname][test_app+'_port'] }}"

TASK: [debug ] **************************************************************** 
ok: [localhost] => {
    "msg": "admin port from inventory = 1234"
}

Many thanks for your help. This forum is fantastic.

James

rek...@voodoowarez.com

unread,
Nov 5, 2014, 4:04:59 PM11/5/14
to ansible...@googlegroups.com
On Wed, Nov 05, 2014 at 08:56:59AM -0800, James Morgan wrote:
> Ok that doesn't work in my example as the variables aren't in the inventory

The other cache for facts: vars[test_app + '_port']

(PITA, this)

James Morgan

unread,
Nov 5, 2014, 5:01:32 PM11/5/14
to ansible...@googlegroups.com
Ah thanks for the info.


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/iuljtzy0Gc4/unsubscribe.
To unsubscribe from this group and all its topics, 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/20141105210453.GL17066%40voodoowarez.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages