Skip to content

Example 4

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
      unique_hash: "NDRhOTBlZmU3OTc2"
    centos1:
      ansible_host: '192.168.77.22'
      ansible_port: 2345
      unique_hash: "MzI0ZTU2ZWMxNTkz"
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'
      unique_hash: "YjlkYTdlMjYyYTU3"

Tip

The template module enables the user to use Jinja2 templates and copy them to the remote hosts. It is very useful when copying multiple configuration files that depend on a set of variables. Check out the module's official documentation or run

ansible-doc template

Execute the following command

echo "{{ unique_hash }}" > hash.j2
ansible all -m template -a 'src="hash.j2" dest="/home/vagrant/.unique"'

Bonus round

Knowing that the variable inventory_hostname stores the host's name as described in Ansible's inventory, change the MOTD on centos1 to read Hello from centos1!.