Interpolating concatenated variables

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.

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

Many thanks

James

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

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

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

(PITA, this)

Ah thanks for the info.