Fetch hosts without domain from inventory file

Viewed 105

I have following inventory file

$ cat hosts
[web]
server1.example.com
server2.example.com

I would like to fetch the hostname, without the part of domain (.example.com). I tried with the following playbook, however, it is still fetching with the entire hostname..

$ playbook.yaml
- hosts: localhost
  tasks:
  - debug:
      msg: "{{ groups['web'] }}"

Output

PLAY [localhost] *************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************

ok: [localhost]

TASK [debug] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "server1.example.com"
        "server2.example.com"
    ]
}

PLAY RECAP *******************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Expected output

PLAY [localhost] *************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************

ok: [localhost]

TASK [debug] *****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "server1"
        "server2"
    ]
}

PLAY RECAP *******************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
1 Answers

You can get what you want from a magic variable called inventory_hostname_short which basically returns anything before the first . found in the inventory_hostname.

To get this in a normal play host loop, it's as easy as:

- hosts: all

  tasks:
    - name: show target short name
      debug:
        var: inventory_hostname_short

If you need to get that for hosts not in the host loop, you will have to go through hostvars. Here is an example to get all those names in a list for a given group running from localhost:

- hosts: localhost
  gather_facts: false

  tasks:
    - name: show list of shortnames for group 'toto'
      debug:
        msg: "{{ groups['toto'] | map('extract', hostvars, 'inventory_hostname_short') }}"

An other example to get that name only for the first server in group 'toto'

- hosts: localhost
  gather_facts: false

  tasks:
    - name: show shortnames for first server in group 'toto'
      vars:
        server_name: "{{ groups['toto'][0] }}"
      debug:
        msg: "{{ hostvars[server_name].inventory_hostname_short }}"
Related