Concatenate text and variable for a key in a dict

12 views
Skip to first unread message

Guillaume Abrioux

unread,
Nov 28, 2016, 7:05:36 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 behaviour I was expected.
I was more thinking about 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
Reply all
Reply to author
Forward
0 new messages