Summary

A summary of the content covered in the chapter.

This chapter introduced dynamic inventories, inventory plugins, and how you can use them to configure your own dynamic inventory of hosts deployed on Azure and AWS.

Dynamic inventory

We started with introducing dynamic inventory and the need for it. Infrastructure is rarely static, and as it grows, you need mechanisms to manage it efficiently.

A dynamic inventory uses an external source to generate the Ansible inventory.

Create a dynamic inventory - AWS

Next, we looked into how you can create an inventory of the hosts deployed on AWS. You made use of the aws_ec2 plugin.

The aws_ec2 plugin queries the EC2 instances, their details and constructs an inventory from that information. You created a hosts_aws_ec2.yml file and defined the aws_ec2 settings. You queried based on the following:

  • regions, us-east-1
  • tags, ansible

Next, we looked into groups to categorize the Linux and Windows hosts. You used tags names to add hosts to their corresponding groups. You used the ansible_inventory command to confirm the hostnames.

We looked at the limitations of groups and introduced a better method, keyed_groups.

Instead of specifying the group name, keyed groups base the group name on the variable’s value, and the existence of the variable determines group membership.

You used the tag.os host variable to group the hosts.

Create a dynamic inventory - Azure

We looked into how you can create an inventory of the hosts deployed on Azure. You used the azure_rm plugin.

The azure_rm plugin queries Azure Resource Manager for the VM details and constructs an Ansible inventory from that information.

Next, we looked into the conditional_groups parameter to categorize the Linux and Windows hosts. You used name to group Linux hosts and image.offer to group Windows hosts.

We looked into os_profile.system hostvar for better grouping. You used the ansible_inventory command to confirm the hostnames and looked into the following options:

  • --graph
  • --lists

We introduced an alternate method, keyed_groups.

Instead of specifying the group name, keyed groups base the group name on the variable’s value, and the existence of the variable determines group membership.

You used the os_profile.system host variable to group the hosts deployed on Azure. A new group would be dynamically created and populated based on the os_profile.system host variable if a new system type were deployed.

Delete the environments

In case you are going to visit the rest of the course later, you can delete the infrastructure to avoid any unexpected bills.

Disclaimer: Run the playbook at your own risk!
It is highly recommended you use a development AWS account and Azure subscription before executing these playbooks.

You can review the playbooks in the sections for Deploy to Azure and Deploy to AWS to delete the infrastructure.

Get hands-on with 1200+ tech skills courses.