Search code examples
ansibleansible-inventory

Ansible playbook parameter from dynamic inventory


Given the following playbook, I would like to assign specific hostnames to every member of the hostgroup. Therefore, the name parameter needs to change for every instance in the hostgroup.

- name: friendly hostname
  hostname: name=clusterXmember.1

How do I access the machine names from the inventory file? Specifically the current machine's ip, or the hostname?

As the playbook runs, I'd like the name param to be pre-filled with data from inventory. (generated from terraform)


Solution

  • How do I access the machine names from the inventory file? Specifically the current machine's ip, or the hostname?

    The inventory name is accessible in different formats via the built-in variables inventory_hostname, inventory_hostname_short, and ansible_hostname. See the documentation I linked to for a description of each.

    The IP can be obtained via the facts that are gathered for each host. You will probably want to use ansible_default_ipv4['address'] to get what you are looking for.