Assume you are working with this inventory (hosts) file:

[bootstrap]
opihi.example.com ansible_host=192.168.30.254 ansible_user=you ansible_password='{{ vault_opihi_you_user_password }}' ansible_python_interpreter=/usr/bin/python3

then you can get the IP address with the expression {{ hostvars[groups['bootstrap'][0]].ansible_host }}.

Here is an example playbook:

---
- hosts: localhost

  tasks:
      - debug: msg={{ hostvars[groups['bootstrap'][0]].ansible_host }}

Which will output:

$#> ansible-playbook -i inventories/prod/ debug.yml

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

    TASK [Gathering Facts] **************************************************************************************************************************
    ok: [localhost]

    TASK [debug] ************************************************************************************************************************************
    ok: [localhost] => {}

    MSG:

    192.168.30.254

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

Enjoy.

Like this post? Share on: TwitterFacebookEmail


Jens Frey Avatar Jens Frey is the creator of the datapile blog.

Published

Category

Infrastructure

Tags

Stay in Touch