Dynamic Inventory: Past, Present & Future

January 29, 2018 by Dylan Silva

In Red Hat Ansible Engine 2.4, we made some changes to how inventory works. We introduced a new cli tool, and added an inventory plugin type.

The goal of this plugin type, was well, to make Ansible Engine even more pluggable. All kidding aside, we wanted to provide Ansible Engine content authors and Ansible Engine users a new way to visualize their target inventory.  Using the ansible-inventory command,  the targeted inventory will be listed with the details of the hosts in that inventory, and the hosts groups.

For example:

[thaumos@ecb51a545078 /]# ansible-inventory -i ~/Development/playbooks/inventory/prod.aws_ec2.yml --list
{
    "_meta": {
        "hostvars": {
            "ec2-5x-xx-x-xxx.us-west-2.compute.amazonaws.com": {
                "AmiLaunchIndex": 2,
                "Architecture": "x86_64",
                "BlockDeviceMappings": [
                    {
                        "DeviceName": "/dev/sda1",
                        "Ebs": {
                            "AttachTime": "2017-12-13T15:40:19+00:00",
                            "DeleteOnTermination": false,
                            "Status": "attached",
                            "VolumeId": "vol-0514xxx"
                        }
                    }
                ],
                "ClientToken": "",
                "EbsOptimized": false,
                "Hypervisor": "xen",
                "ImageId": "ami-0c2aba6c",
                "InstanceId": "i-009xxxx3",
                "InstanceType": "t2.micro",
                "KeyName": "blogKey",
                "LaunchTime": "2017-12-13T15:40:18+00:00",
                "Monitoring": {
                    "State": "disabled"
                },
                "NetworkInterfaces": [
                    {
                        "Association": {
                            "IpOwnerId": "amazon",
                            "PublicDnsName": "ec2-5x-xx-x-xxx.us-west-2.compute.amazonaws.com",
                            "PublicIp": "5x.xx.x.xxx"
                        },
                        "Attachment": {
                            "AttachTime": "2017-12-13T15:40:18+00:00",
                            "AttachmentId": "eni-attach-97c4xxxx",
                            "DeleteOnTermination": true,
                            "DeviceIndex": 0,
                            "Status": "attached"
                        },
                        "Description": "",
                        "Groups": [
                            {
                                "GroupId": "sg-e63xxxxd",
                                "GroupName": "blogGroup"
                            }
                        ],
                        "Ipv6Addresses": [],
                        "MacAddress": "02:50:99:0b:e8:e8",
                        "NetworkInterfaceId": "eni-eaxxxxxx",
                        "OwnerId": "xxxxxxxx",
                        "PrivateDnsName": "ip-1xx-xx-xx-x.us-west-2.compute.internal",
                        "PrivateIpAddress": "1xx.xx.xx.x",
                        "PrivateIpAddresses": [
                            {
                                "Association": {
                                    "IpOwnerId": "amazon",
                                    "PublicDnsName": "ec2-5x-xx-x-xxx.us-west-2.compute.amazonaws.com",
                                    "PublicIp": "52.xx.x.xxx"
                                },
                                "Primary": true,
                                "PrivateDnsName": "ip-1xx-xx-xx-x.us-west-2.compute.internal",
                                "PrivateIpAddress": "1xx.xx.xx.x"
                            }
                        ],
                        "SourceDestCheck": true,
                        "Status": "in-use",
                        "SubnetId": "subnet-1xxx279",
                        "VpcId": "vpc-4cxxxx29"
                    }
                ],
                "Placement": {
                    "AvailabilityZone": "us-west-2a",
                    "GroupName": "",
                    "Tenancy": "default"
                },
                "PrivateDnsName": "ip-1xx-xx-xx-x.us-west-2.compute.internal",
                "PrivateIpAddress": "1xx.xx.x.x",
                "ProductCodes": [],
                "PublicDnsName": "ec2-5x-xx-x-xxx.us-west-2.compute.amazonaws.com",
                "PublicIpAddress": "5x.xx.x.xxx",
                "RootDeviceName": "/dev/sda1",
                "RootDeviceType": "ebs",
                "SecurityGroups": [
                    {
                        "GroupId": "sg-e6xxxxxx",
                        "GroupName": "blogGroup"
                    }
                ],
                "SourceDestCheck": true,
                "State": {
                    "Code": 16,
                    "Name": "running"
                },
                "StateTransitionReason": "",
                "SubnetId": "subnet-1cxxxxxx",
                "Tags": [
                    {
                        "Key": "env",
                        "Value": "Production"
                    },
                    {
                        "Key": "Name",
                        "Value": "blogKey"
                    }
                ],
                "VirtualizationType": "hvm",
                "VpcId": "vpc-4cexxxxx"
            }
        }
    },
    "all": {
        "children": [
            "aws_ec2_instance_type_t2_micro",
            "aws_ec2_tag_value_blogger",
            "ungrouped"
        ]
    },
    "aws_ec2_instance_type_t2_micro": {
        "hosts": [
            "ec2-5x-xx-x-xxx.us-west-2.compute.amazonaws.com"
        ]
    },
    "aws_ec2_tag_value_blogger": {
        "hosts": [
            "ec2-5x-xx-x-xxx.us-west-2.compute.amazonaws.com"
        ]
    },
    "ungrouped": {}
}
Another nifty thing the tool does is show a tree or graph of what the inventory looks like:
[thaumos@ecb51a545078 /]# ansible-inventory -i ~/Development/playbooks/inventory/prod.aws_ec2.yml --graph
@all:
  |--@aws_ec2_instance_type_t2_micro:
  |  |--ec2-5x-xx-x-xxx.us-west-2.compute.amazonaws.com
  |--@aws_ec2_tag_value_blogger:
  |  |--ec2-5x-xx-x-xxx.us-west-2.compute.amazonaws.com
  |--@ungrouped:

All of this data is being provided by our new AWS EC2 inventory plugin!  Inventory plugins are a much simpler interface for Ansible Engine content providers to interact with the underlying inventory components. This inventory plugin will be provided as a tech-preview. Please provide feedback on it!

Most inventory plugins utilize simple yaml based configuration files to activate the plugin[1] and fine tune the data collected.  Here's an example:

    plugin: aws_ec2
    boto_profile: default
    regions: # populate inventory with instances in these regions
      - us-west-2
    group_by:
    # makes a group for instances that have the tag values 'thaumos' and 't2.micro' instance type.
      - tag-value=thaumos
      - instance-type=t2.micro
    filters: 
      tag:Name: blogger
      tag:env: Production
      instance-state-name: running
    strict_permissions: False

These plugins also have access to the inventory cache that is being built in Ansible Engine 2.5, also in tech preview.  This inventory cache allows for users to combine the powers, so to speak, of other inventory plugins.  For instance, imagine using any of the cloud inventory plugins to pull data about the instances and the constructed inventory plugin and jinja to build groups as decided by the user!  This approach is much more flexible than before; where a content developer would have to build grouping into the dynamic inventory script itself.

I hope the information in this blog post is a great teaser to up and coming features.  I plan on doing little teasers like these more often as we have new stuff coming in Ansible Engine.  These posts will also coincide with a webinar.  The webinar for this blog post can be found here!

Continue Automating all the things!

[1]: Default behaviour in configuration.  Remove 'auto' from INVENTORY_ENABLED in ansible config to stop auto-loading of inventory plugins.

END OF LINE.

Share:

Topics:
AWS, Ansible, Plugins, Ansible Engine, Dynamic Inventory, Inventory


 

Dylan Silva

Dylan is a Principal Product Manager, Ansible, Red Hat. Starting as an early Core community member in Ansible's early days, Dylan now manages product roadmap for Ansible Core. He’s a self-proclaimed Linux and OSS diehard, Internet geek, and father to #Ansipup Honey. You can follow him on twitter and GitHub at @Thaumos.


rss-icon  RSS Feed

Ansible Tower by Red Hat
Ansible Fest Austin 2018
Learn About Ansible Tower