DEV Community

Fabien Arcellier
Fabien Arcellier

Posted on • Edited on • Originally published at blog.farcellier.com

Inspect a dynamic Ansible AWS inventory

We will discover the power of ansible-inventory to inspect the structure of the inventory built by ansible when the inventory is generated from the metadata of a machine park. No more wasted time figuring out which groups and hosts Ansible referenced when loading a dynamic inventory.

Ansible is able to generate dynamic inventory from an AWS account. The aws_ec2 plugin builds the inventory from the metadata it finds on the account.

To use it, you must configure the aws_ec2 plugin in an inventory file.

ansible -i aws_ec2.yml --list-hosts all
Enter fullscreen mode Exit fullscreen mode
  hosts (2):
    ec2-52-XX-XX-25.eu-west-1.compute.amazonaws.com
    ec2-52-XX-XX-48.eu-west-1.compute.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

[WARNING]: Could not match supplied host pattern

It all starts with the error WARNING]: Could not match supplied host pattern. This error message is generated when ansible cannot find a match between groups and hosts referenced in the inventory.

ansible-playbook -i aws_ec2.yml deploy.yml
Enter fullscreen mode Exit fullscreen mode
[WARNING]: Could not match supplied host pattern, ignoring: farcellier.com

PLAY [farcellier.com] *******************************************************************************************
skipping: no hosts matched
Enter fullscreen mode Exit fullscreen mode

I wasted an hour trying to figure out why ansible couldn't find the farcellier.com group in the inventory when it should have been there. I would have saved time if I knew how to inspect the inventory.

Inspect inventory

The ansible-inventory command allows you to inspect the inventory as ansible sees it. It's a release to be able to see what ansible is interpreting instead of trying to guess.

ansible-inventory -i aws_ec2.yml --graph
Enter fullscreen mode Exit fullscreen mode
@all:
  |--@aws_ec2:
  |  |--ec2-52-XX-XX-25.eu-west-1.compute.amazonaws.com
  |  |--ec2-52-XX-XX-48.eu-west-1.compute.amazonaws.com
  |--@other_website:
  |  |--ec2-52-XX-XX-25.eu-west-1.compute.amazonaws.com
  |--@farcellier_com:
  |  |--ec2-52-XX-XX-48.eu-west-1.compute.amazonaws.com
  |--@ungrouped:
Enter fullscreen mode Exit fullscreen mode

The solution is before our eyes. Ansible renames the farcellier.com tag to farcellier_com.

Inspect inventory deeply

The ansible-inventory command allows you to see even more. It is able to trace all the variables captured at the host level. This is handy for improving the AWS inventory definition file.

ansible-inventory -i aws_ec2.yml --list
Enter fullscreen mode Exit fullscreen mode

I've posted a snippet of all the variables that are fetched.

{
  "_meta": {
    "hostvars": {
      "ec2-52-XX-XX-25.eu-west-1.compute.amazonaws.com": {
        "ami_launch_index": 0,
        "ansible_host": "ec2-52-XX-XX-25.eu-west-1.compute.amazonaws.com",
        "architecture": "x86_64"
      },
      "ec2-52-XX-XX-48.eu-west-1.compute.amazonaws.com": {
        "ami_launch_index": 0,
        "ansible_host": "ec2-52-XX-XX-48.eu-west-1.compute.amazonaws.com",
        "architecture": "x86_64"
      }
    }
  },
  "all": {
    "children": [
      "aws_ec2",
      "other_website",
      "farcellier_com",
      "ungrouped"
    ]
  },
  "aws_ec2": {
    "hosts": [
      "ec2-52-XX-XX-25.eu-west-1.compute.amazonaws.com",
      "ec2-52-XX-XX-48.eu-west-1.compute.amazonaws.com"
    ]
  },
  "other_website": {
    "hosts": [
      "ec2-52-XX-XX-48.eu-west-1.compute.amazonaws.com"
    ]
  },
  "farcellier_com": {
    "hosts": [
      "ec2-52-XX-XX-25.eu-west-1.compute.amazonaws.com"
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

References

Ansible AWS inventory used in this blog post

For reference, I share the inventory I used. It selects machines in the eu-west-1 region, groups them from the Name tag.

aws_ec2.yml

---
plugin: aws_ec2

regions:
  - eu-west-1

filters:
  # All instances with their state as `running`
  instance-state-name: running

keyed_groups:
 - key: tags.Name
   separator: ''
   prefix: ''

compose:
    ansible_host: public_dns_name
Enter fullscreen mode Exit fullscreen mode

Top comments (0)