How to assign inventory group alias to a variable

ansible

I have this Ansible inventory file:

[fileservers]
fs01.example.com   ansible_ssh_host=192.168.1.1    ip_addr=192.168.1.1

[dbservers]
db01.example.com   ansible_ssh_host=192.168.1.2    ip_addr=192.168.1.2

[webservers]
web01.example.com   ansible_ssh_host=192.168.1.3    ip_addr=192.168.1.3

[cmsservers]
cms01.example.com   ansible_ssh_host=192.168.1.4    ip_addr=192.168.1.4

What I want to do is be able to set some facts that contain the host aliases when I run this playbook on my web01.example.com server:

- name: create alias facts
  set_fact:
    file_server_alias: hostvars[{{ inventory_hostname }}]['groups']['fileservers'][0]  # should get string 'fs01.example.com'
    db_server_alias: hostvars[{{ inventory_hostname }}]['groups']['dbservers'][0]      # should get string 'db01.example.com'
    cms_server_alias: hostvars[{{ inventory_hostname }}]['groups']['cmsservers'][0]    # should get string 'cms01.example.com'

However, when I do the above, file_server_alias just gets set to 'hostvars[{{ inventory_hostname }}]['groups']['fileservers'][0]', and so on. Is there any way to assign these inventory group aliases to variables?

Best Answer

You need to put the whole assigned expression inside the moustaches. This example is working for me in ansible-playbook 2.5.1 and Python 2.7

---
- hosts: fileservers
  gather_facts: no
  tasks:
        - name: create alias facts
          set_fact:
            file_server_alias: "{{ hostvars[inventory_hostname]['groups']['fileservers'][0] }}" # should get string 'fs01.example.com'
            db_server_alias: "{{ hostvars[inventory_hostname]['groups']['dbservers'][0] }}"      # should get string 'db01.example.com'
            cms_server_alias: "{{ hostvars[inventory_hostname]['groups']['cmsservers'][0] }}"    # should get string 'cms01.example.com'
        - name: debug
          debug:
            msg: "{{ file_server_alias }} {{ db_server_alias }} {{ cms_server_alias }}"

Output:

$ ansible-playbook -i inventory playbook.yml 

PLAY [fileservers] *********************************************************************************************************************************************************************************************************************************************

TASK [create alias facts] **************************************************************************************************************************************************************************************************************************************
ok: [fs01.example.com]

TASK [debug] ***************************************************************************************************************************************************************************************************************************************************
ok: [fs01.example.com] => {
    "msg": "fs01.example.com db01.example.com cms01.example.com"
}

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************
fs01.example.com           : ok=2    changed=0    unreachable=0    failed=0