Introduction:
Inventory files lie at the core of Ansible. They are a crucial component that organizes and categorizes the target hosts to which Ansible will apply configurations. In this post, I'll walk you through the essentials of an Ansible inventory file, covering its basic structure and syntax. We'll end by demonstrating how to create one. By the end, you'll have the foundation needed to efficiently manage your servers using Ansible.
Note: This post builds on what we covered in the "Getting Started With Ansible: Installing the Control Node" and "Getting Started With Ansible: Setting Up a Windows Managed Node" posts. We set up the Ansible control node there, installed Visual Studio Code, and installed the recommended extensions. If you're following along in the demos and haven't created the control node or a Windows managed node, I'd recommend starting with those posts.
What is an Ansible Inventory File?
An Ansible inventory file is a configuration file that lists and categorizes the hosts Ansible will manage. It provides a structured way to define hosts and groups, enabling efficient targeting and execution of tasks on specific hosts or groups of hosts. They typically include hostnames or IP addresses and host groups, allowing you to target specific managed nodes or categories of managed nodes for your automation. Inventory files may also include variables to change their default behavior when connecting and executing commands on managed nodes.
Basic Syntax and Structure of an Inventory File:
Let's break down the fundamental elements that make up an inventory file. An inventory file can use a simple INI format or YAML format. We'll stick with the INI format here, but to learn more about the YAML format, see the "Working with Inventory" link in the Additional Resources section below. Hosts and their details are organized under different group names, encapsulated in square brackets ([group_name]). Each host or group is defined with key-value pairs, specifying attributes like IP addresses, SSH port, and more.
[group_name]
hostname ansible_host=IP_address ansible_ssh_port=SSH_port
[another_group]
host1 ansible_host=192.168.1.100 ansible_ssh_port=22
host2 ansible_host=192.168.1.101 ansible_ssh_port=2222
Creating Your First Inventory File:
Let's create an inventory file that we'll use in the demo. This file will consist of two Windows machines and one Red Hat Enterprise Linux machine with two groups.
- Open a text editor and create a new file, hosts.ini.
- Define host groups using square brackets. We'll name the first group [windowsservers]. The second group can be named [linuxservers].
- Add hostnames or IP addresses to each group.
- windowsservers
- SRV2 ansible_host=10.0.0.155
- SRV3 ansible_host=10.0.0.159
- linuxservers
- RHEL1 ansible_host=10.0.0.157
- windowsservers
- Let's add the group variables to define which protocols Ansible should use when connecting to machines within these groups. Define two variable sections: [windowsservers:vars] and [linuxserver:vars].
- Add the following variables within each section.
- windowsservers:vars
- ansible_connection=winrm
- ansible_winrm_server_cert_validation=ignore
- ansible_port=5986
- ansible_winrm_transport=ntlm
- ansible_winrm_operation_timeout_sec=60
- ansible_winrm_read_timeout_sec=90
- linuxservers:vars
- ansible_connection=ssh
- windowsservers:vars
- Save the file. It should match the screenshot below.
Advanced Inventory File Options:
As you become more proficient with Ansible, you'll discover additional capabilities and features that enhance your inventory file. We used a few of those advanced options in our example above, such as defining variables for hosts or groups and creating aliases for hosts (SRV2 ansible_hosts=.....). There are many more options available. For more information, see the "Working with Inventory" link in the Additional Resources section.
Best Practices for Organizing Inventory Files:
Efficiently organizing your inventory file is essential, especially as your infrastructure grows. Let's explore some best practices to ensure your inventory file remains manageable and scalable.
- Divide hosts into logical groups based on their roles or functions (e.g., web servers, databases, windows managed nodes, linux managed nodes).
- Use descriptive names for groups and hosts to enhance readability.
- Separate sensitive data, like passwords, into separate files (e.g., Ansible Vault) for added security.
Validating and Testing the Inventory File:
Before using your inventory file, it's crucial to validate its syntax and test connectivity to the specified hosts.
- Use the ansible-inventory command with the --list option to validate the syntax and structure.
- Utilize the ansible command with the simple ping module or win_ping module to test connectivity to hosts, ensuring your inventory file is correctly configured.
Demo:
In the following demo, we'll cover building the inventory file, validating the structure with ansible-inventory, and ending with using the ping and win_ping modules to verify connectivity to the managed nodes.
Conclusion:
Congratulations! You've taken the first step in leveraging Ansible by creating your first inventory file. By understanding the fundamental structure and syntax, you can now efficiently manage and automate your infrastructure. Stay tuned for more Ansible guides as I continue the "Getting Started With Ansible" series with Ansible Playbooks up next.
Happy automating!
Additional Resources