Working with Ansible Static and Dynamic Inventory
Static Inventory:
Static Inventory allows to specify the hosts IP address. Ansible check for the inventory file by default at /etc/ansible/hosts based on the hosts defined in the file it perform the function. The static inventory are useful in case when you have fix hosts, whose IP addresses do not change.
Below is the defined INI format you can use to create a ansible static host file.
demo1.example.com [group1] demo2.example.com [group2] demo3.example.com demo4.example.com demo5.example.com [group3] demo1.example.com demo6.example.com
The group1 have only one classified host & group2 contain three hosts specified. So the ansible will execute it's task on the all hosts defined in the group. You can define a host in multiple hosts as the demo1.example.com is in group1 & group3. You can also give the IP Address instead of the domain name.
Nested Grouping of Hosts:
In INI Format:
[group1] host1 host2 [group2:children] group1 [group3:children] group2 host3 host4
In YAML Format:
group3: children: group2: children: group1: hosts: host1: host2: host3: host4:
In the above the nested grouping is defined in both the formats as INI/YAML.
Dynamic Inventory:
The dynamic inventory are useful for the scenario when the target hosts are keep on changing due to changing IP's, autoscaling etc so it is difficult to use the static inventory in this case. Dynamic inventory provides features to get the hosts address based on the tags, assumed role, instance types etc.
The plugin amazon.aws.aws_ec2 makes a API call to AWS which helps to get the list of instances at the run time.
The dynamic inventory name must always end with aws_ec2.yml for e.g: mydemo.aws_ec2.yml.
Below is the demo dynamic inventory file which use the aws_ec2 plugin and the Access & Secret key to interact with the AWS to get the list of instances.
plugin: amazon.aws.aws_ec2 aws_access_key: <AWS-ACCESS-KEY> aws_secret_key: <AWS-SECRET-KEY>
For executing the ansible playbook with the dynamic inventory you need to specify the path to your inventory file as below.
ansible-playbook -i file/path/demo.aws_ec2.yml
plugin: amazon.aws.aws_ec2 iam_role_arn: <ARN_OF_ASSUMED_ROLE>
The above use case is really helpful in the scenario when you want to get the instance which are located in other AWS account. Using the assumed role the aws_ec2 plugin is able to fetch the instance IP's cross account.
keyed_groups & filters: The dynamic host grouped can be created based on the keyed_group feature where you can define the prefix & key.
Using Filters you can fetch the instance based on the tag given to them. For e.g you can get all the instances having the QA tag. The demo file for this use case given below.plugin: amazon.aws.aws_ec2 keyed_groups: - prefix: instance_type key: <type of instance> filters: tag:Env: qa instance-state-name: running tag:Name: - 'demo1' - 'demo2'
Conclusion:
- Ansible's inventory management plays a crucial role in orchestrating and automating IT infrastructure. Static inventories are ideal for stable environments with fixed hosts, while dynamic inventories cater to dynamic infrastructures, such as those found in cloud environments with changing IP addresses and auto-scaling. The flexibility of Ansible allows users to define host groups and even nest them, providing a structured and organized approach to managing various sets of hosts. With dynamic inventories, plugins like aws_ec2 enable seamless integration with cloud platforms, making it possible to adapt to ever-changing instances, even across different AWS accounts.