Concatenate text and variable for a key inside a dict

82 views
Skip to first unread message

Guillaume Abrioux

unread,
Nov 28, 2016, 7:06:46 PM11/28/16
to Ansible Project
Hi,

I would like to define a dict in group_vars/all.yml where a key inside this dict would consist of a fixed string concatened to the result of : "{{ hostvars[inventory_hostname]['ansible_hostname'] }}".

This should looks like following:

✘ root@lab:~/playbook# cat group_vars/all.yml
---
my_dict:
  "something_{{ hostvars[inventory_hostname]['ansible_hostname'] }}":
    "key1": "value1"
    "key2": "value2"



✔ root@lab:~/playbook# cat roles/test/tasks/test.yml
---
- name: print my_dict
  debug:
    msg: "{{ my_dict }}"



✔ root@lab:~/playbook# ansible-playbook -i hosts site.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [127.0.0.1]

TASK [test : print my_dict] ****************************************************
ok: [127.0.0.1] => {
    "msg": {
        "something_{{ hostvars[inventory_hostname]['ansible_hostname'] }}": {
            "key1": "value1",
            "key2": "value2"
        }
    }
}

But as you can see, when I print it with a simple debug task, it hasn't the expected behaviour.
I was more thinking to get something as following :

TASK [test : print my_dict] ****************************************************
ok: [127.0.0.1] => {
    "msg": {
        "something_mygreathostname": {
            "key1": "value1",
            "key2": "value2"
        }
    }
}

Do you have an idea how could I do it ?

Thanks,

Guillaume

sh...@redhat.com

unread,
Dec 15, 2016, 12:19:25 PM12/15/16
to Ansible Project
No one for this?
I'm also getting this problem, it'd be nice if we could get some clarification.

Thanks!
Reply all
Reply to author
Forward
0 new messages