Managing Ansible inventories can be challenging, especially when dealing with multiple servers and playbooks. In this guide, we’ll explore how to create a properly formatted Ansible inventory file using a simple bash script. This approach is particularly useful when you need to convert server lists from CSV or other formats into Ansible’s inventory format.
Why Structured Inventory Files Matter
When working with Ansible, it’s crucial to maintain an organized and up-to-date inventory file. A well-structured inventory allows you to:
- Run different playbooks against specific server groups
- Maintain a single source of truth for your infrastructure
- Easily update and manage server classifications
- Scale your automation across multiple environments
Consider this common scenario: You have multiple playbooks - one for managing web services, another for database operations. Instead of creating separate inventory files for each playbook, you can maintain a single, well-organized inventory file.
Example of a Well-Structured Ansible Inventory
Here’s how a properly formatted Ansible inventory looks:
[app]
app.server.com
app2.server.com
[web]
web1.server.com
web2.server.com
[databases]
db1.server.com
db2.server.com
With this structure, you can target specific server groups in your playbooks by simply referencing the group name:
- hosts: app
tasks:
# Your tasks here
Converting Server Lists to Ansible Format
Often, you’ll receive server lists in different formats (like CSV) that need to be converted into Ansible’s inventory format. Manual conversion can be time-consuming and error-prone. Let’s solve this with a bash script.
Step 1: Prepare Your Raw Server List
Create a file named raw_inventory.txt
with your server list:
app.server.com
app2.server.com
web1.server.com
web2.server.com
Step 2: Create the Conversion Script
Create a file named script.sh
with the following content:
#!/bin/bash
# Script to format list of servers into ansible format
file=raw_inventory.txt
last=''
while read nameX
do
label="${nameX%%[.0-9]*}"
if [[ "$label" != "$last" ]]
then
echo "[$label]"
last="$label"
fi
echo "$nameX"
done < $file > Inventory
Step 3: Execute the Script
Make the script executable and run it:
chmod +x script.sh
./script.sh
The script will generate an Inventory
file with the properly formatted Ansible inventory:
[app]
app.server.com
app2.server.com
[web]
web1.server.com
web2.server.com
How the Script Works
The script performs the following operations:
- Reads the server list from
raw_inventory.txt
- Extracts the server type (label) by removing everything after the first dot or number
- Creates a new group header whenever it encounters a different server type
- Places each server under its appropriate group
Best Practices
When using this approach, keep in mind:
- Maintain consistent naming conventions for your servers
- Regularly update your raw inventory list
- Version control both your raw list and the conversion script
- Validate the generated inventory file before using it with Ansible
Next Steps
After creating your formatted inventory, you might want to explore:
Conclusion
This simple bash script can save significant time when managing Ansible inventories, especially in larger environments. By automating the inventory formatting process, you can focus more on writing effective playbooks and less on inventory management.
Remember to adjust the script according to your specific naming conventions and requirements. The key is to maintain consistency in your server naming pattern for the script to work effectively.