How to access a variable of the first host in a group?

devquora
devquora

Posted On: Feb 22, 2018

 

The trick lies in this command:

{{ hostvars[groups['webservers'][0]]['ansible_eth0']['ipv4']['address'] }}

Here, we’re pulling out the hostname of the first machine of the webservers group. If you’re doing this in a template, use the Jinja2 ‘#set’ or in a playbook, you can also use set_fact:

- set_fact: headnode={{ groups[['webservers'][0]] }}
- debug: msg={{ hostvars[headnode].ansible_eth0.ipv4.address }}

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Ansible Interview Questions

    What is Ansible?

    Ansible is an open source automation platform which can help you with configuration management, task automation and appl..

    Ansible Interview Questions

    Talk about Ansible architecture.

    Ansible works on ‘agentless architecture’. It works by connecting to your nodes and pushes out Ansible modules to th..

    Ansible Interview Questions

    List some advantages of using Ansible.

    Unlike other configuration management system, Ansible is the most sought after software applications these days. it off..