Managing Ansible Inventory: Static and Dynamic Inventories
Ansible inventories are a fundamental aspect of Ansible automation, allowing you to define and manage the hosts and groups that your playbooks will target. Inventories can be static, defined in simple text files, or dynamic, generated by scripts that query external sources like cloud providers or configuration management databases. In this blog post, we'll explore how to set up and manage both static and dynamic inventories in Ansible.
Setting Up and Managing Static Inventory Files
Static inventory files are the simplest way to define your infrastructure in Ansible. These files are typically written in YAML or INI format.
[webservers]
[dbservers]
[all:vars]
ansible_user=your_username
ansible_ssh_private_key_file=~/.ssh/id_rsa
2. Creating a Static Inventory File in YAML Format
YAML format provides more structure and is often preferred for its readability. Here’s the same inventory in YAML format:
all:
vars:
ansible_user: your_username
ansible_ssh_private_key_file: ~/.ssh/id_rsa
children:
webservers:
hosts:
dbservers:
hosts:
3. Using the Static Inventory File in Playbooks
To use the static inventory file in your playbooks, specify it with the -i option:
ansible-playbook -i hosts.ini site.yml
Or for the YAML format:
ansible-playbook -i hosts.yaml site.yml
4. Group Variables and Host Variables
You can define variables for groups and individual hosts directly within the inventory file or in separate group_vars and host_vars directories.
Example group_vars/webservers.yml:
http_port: 80
max_clients: 200
Example host_vars/web1.example.com.yml:
database_server: db1.example.com
Using Dynamic Inventories for Scalable Environments
Dynamic inventories are essential for environments where the infrastructure is constantly changing, such as cloud environments. Ansible supports dynamic inventories through inventory scripts or plugins.
Ansible provides a dynamic inventory script for AWS EC2 instances. First, install the boto3 library:
pip install boto3
Next, download the AWS EC2 inventory script from the Ansible GitHub repository:
curl -O https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py
curl -O https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.ini
chmod +x ec2.py
Configure your AWS credentials and region in the ec2.ini file, then use the script as your inventory source:
ansible-playbook -i ec2.py site.yml
2. Using Ansible Plugins for Dynamic Inventories
Ansible also supports inventory plugins for various platforms, including cloud providers, LDAP, and custom scripts.
Example for AWS EC2 using the aws_ec2 plugin:
plugin: aws_ec2
regions:
- us-east-1
filters:
instance-state-name: running
keyed_groups:
- key: tags.Name
prefix: tag
Save this configuration in a file, e.g., aws_ec2.yml, and use it as the inventory source:
ansible-playbook -i aws_ec2.yml site.yml
3. Writing Custom Dynamic Inventory Scripts
If your infrastructure requires custom logic, you can write your own dynamic inventory script. Here’s a basic example in Python:
#!/usr/bin/env python
import json
def main():
inventory = {
'webservers': {
'hosts': ['web1.example.com', 'web2.example.com'],
'vars': {
'http_port': 80
}
},
'dbservers': {
'hosts': ['db1.example.com', 'db2.example.com']
},
'_meta': {
'hostvars': {
'web1.example.com': {
'ansible_host': '192.168.1.10'
},
'web2.example.com': {
'ansible_host': '192.168.1.11'
}
}
}
}
print(json.dumps(inventory))
if name == '__main__':
main()
Save this script as dynamic_inventory.py and make it executable:
chmod +x dynamic_inventory.py
ansible-playbook -i dynamic_inventory.py site.yml
Conclusion
Ansible inventories, both static and dynamic, provide the flexibility needed to manage various infrastructure environments effectively. Static inventories are simple and ideal for stable environments, while dynamic inventories are crucial for scalable and constantly changing environments.
In this blog post, we covered:
By mastering these techniques, you can ensure that your Ansible playbooks are adaptable to any environment, enhancing your automation capabilities. Stay tuned for more advanced Ansible topics and best practices. Happy automating!
For more details click www.hawkstack.com