Host inventories

To provide a list of hosts, we need to provide an inventory list. This is in the form of a hosts file.

In its simplest form, our hosts file could contain a single line:

192.168.50.4.nip.io ansible_user=vagrant

What this is telling Ansible is that the host we want to contact is 192.168.50.4.nip.io and to use the username of vagrant. If we didn't provide the username, it would fall back to the user you are logged into your Ansible control host as, which in my case is simple—the user russ, which does not exist on the Vagrant box. There is a copy of the hosts file called hosts-simple in the Chapter02 folder of the repository alongside the Vagrantfile we used to launch the Vagrant box.

To run the setup command, we need to run the following command from within the same folder where hosts-simple is stored:

$ ansible -i hosts-simple 192.168.50.4.nip.io -m setup

You should see some output which looks like the following:

As you can see from the preceding screenshot, Ansible has quickly found out a lot of information on our Vagrant box. From the screenshot, you can see both of the IP addresses configured on the machine, along with the IPv6 addresses. It has recorded the time and date and, if you scroll through your own output, you will see that there is a lot of information returned detailing the host.

Going back to the command we ran:

$ ansible -i hosts-simple 192.168.50.4.nip.io -m setup

As you can see, we are loading the hosts-simple file using the -i flag. We could have also used --inventory=hosts-simple, which loads our inventory file. The next part of the command is the host to target. In our case, this is 192.168.50.4.nip.io. The final part of the command, -m, tells Ansible to use the setup module. We could have also used --module-name=setup.

This means that the full command, if we didn't use shorthand, would be:

$ ansible --inventory=hosts-simple 192.168.50.4.nip.io --module-name=setup

As already mentioned, the hosts-simple file is as basic as we can get it. The following is a more common host inventory file:

box ansible_host=192.168.50.4.nip.io

[boxes]
box

[boxes:vars]
ansible_connection=ssh
ansible_user=vagrant
ansible_private_key_file=~/.ssh/id_rsa
host_key_checking=False

There is a copy of the file, called just hosts, in the same folder as the hosts-simple file. As you can see, there is a lot more going on, so let's quickly work through it from top to bottom.

The first line defines our individual host. Unlike the simple example, we are going to be calling our hosts box and using ansible_host, so we are giving Ansible details of where it can SSH to. This means that we can now use the name box when referring to 192.168.50.4.nip.io. This means our command would now look something like this:

$ ansible -i hosts box -m setup

Next up in the file, we are creating a group of hosts called boxes and, in that group, we are adding our single host box. This means that we can also run:

$ ansible -i hosts boxes -m setup

If we had more than just a single host in the group, then the preceding command would have looped through all of them. The final section of the hosts file sets up some common configuration options for all of the hosts in the boxes group. In this case, we are telling Ansible that all of the hosts in the group are using SSH, the user is vagrant, the private key at ~/.ssh/id_rsa should be used, and also to not check the host key when connecting.

We will be revisiting the inventory host files in later chapters. From now on, we will be using the hosts file to target the boxes group.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.178.133