Dynamic and Static Inventory Setup
Beginner8 min
Configure static INI or YAML inventory files and dynamic inventory scripts for cloud providers to manage host groups.
Prerequisites
- -Ansible installed
- -SSH access to target hosts
Steps
1
Create a static INI inventory
Defines host groups with connection details and shared variables.
$ cat > inventory.ini << 'EOF'
[webservers]
web01 ansible_host=192.168.1.10
web02 ansible_host=192.168.1.11
[dbservers]
db01 ansible_host=192.168.1.20
[all:vars]
ansible_user=deploy
ansible_python_interpreter=/usr/bin/python3
EOF
2
Test connectivity to all hosts
Sends a ping module to every host in the inventory to verify SSH connectivity.
$ ansible all -i inventory.ini -m ping
3
List hosts in a specific group
Shows which hosts belong to the webservers group.
$ ansible webservers -i inventory.ini --list-hosts
4
Set up dynamic inventory for AWS
Uses the aws_ec2 inventory plugin to auto-discover EC2 instances grouped by tags, regions, or instance types.
$ pip install boto3 && ansible-inventory -i aws_ec2.yml --graph
Create an aws_ec2.yml with 'plugin: amazon.aws.aws_ec2' and configure keyed_groups to organize instances by tags.
5
View the complete inventory graph
Displays the full inventory tree with all groups, hosts, and their variables.
$ ansible-inventory -i inventory.ini --graph --vars
Full Script
FAQ
Discussion
Loading comments...