| || || || |
An inventory is an important part of the Ansible tool chain. Without an inventory Ansible will have no idea what systems we want to manage, where they can be found (within the network or remote network), how-to connect to them, and more.
Ansible inventories come in different shapes and sizes. You're very likely to find both static and dynamic inventories in the wild.
Our inventory is a static inventory. We know what systems we have in our network and it doesn't change. We can therefore write out the inventory ahead of time and use it to manage our (static) systems.
A dynamic inventory is used in networks that have an unknown or dynamic range of systems. A good example of this is an AutoScaling Group in AWS. The number of EC2 Instances inside of the group at any one time can be anything. Therefore we use tags on resources and a dynamic inventory can be used to find what instances exist at that point in time and then execute Ansible against them.
Our Ansible inventory is written in YAML. In the wild you might see other inventories written in INI format. That format is a bit easier to read until an inventory gets complex. INI files are also harder to generate from other (automated) sources versus YAML. That's why I've opted for a YAML based inventory file.
Please ensure you're aware of how-to write a YAML file because as a format it's very particular about white space.
First we "create" a group called
all group technically already exists as it's a special group that basically means "everything". Still we're creating it for clarity.
Next we use the
hosts keyword to define what hosts exists inside this group:
1 2 3
And then we define the hosts themselves, each with a variable being set called
1 2 3 4 5 6
Note that the key used inside of the
hosts dictionary is the hostname of the remote system itself. This is the internal DNS hostname used to resolve the remote host's IP and connect to it.
ssh_username so that Ansible knows what username to use. There are many other options available that you should explore in the official Ansible documentation.
1 2 3 4 5 6
Committing the Code¶
- Set your working directory to the
- Save the file as
git add inventory.yamlto add it to the Git staging area
git commit -am 'our primary inventory'to commit the file to our repository
- Push the code to GitLab.com: