Ansible aws_ec2 inventory never matching host group

1.1k Views Asked by At

So firstly, I probably missing something really simple here and I just can't see it. I am trying to use ansible pull with aws_ec2 inventory pluggin to run on a EC2 instance in AWS using packer. I have the following inventory file:

plugin: aws_ec2
regions:
  - eu-west-1
  - eu-west-2
keyed_groups:
  - key: tags['role']
    prefix: enc
filters:
  instance-state-name: running
  tag:ami: "true

and when I run ansible-inventory -i /tmp/ansible/inventories/dev/ami_inventory_aws_ec2.yml --list I get the following groups:

"all": {
        "children": [
            "aws_ec2",
            "enc_web",
            "ungrouped"
        ]
    },
    "aws_ec2": {
        "hosts": [
            "ip-10-93-66-209.eu-west-1.compute.internal"
        ]
    },
    "enc_web": {
        "hosts": [
            "ip-10-93-66-209.eu-west-1.compute.internal"
        ]
    }

which is what I expect to see. Then in my playbook I have the following:

---
- import_playbook: bootstrap.yml

- hosts: enc_web
  gather_facts: True
  roles:
    - name: enc.base
....

At this point I would of thought that running ansible-pull or ansible-playblook with or without --limit enc_web ansible would of matched the group name and run, but instead I am getting

[WARNING]: Could not match supplied host pattern, ignoring: ami-aws-web-01
ERROR! Specified hosts and/or --limit does not match any hosts

I am I missing something really basic here? Should it run against all hosts in that group name?

My full ansible command is:

ansible-pull [email protected]:XXXXX/ansible.git --accept-host-key --directory=/tmp/ansible --full --checkout=web --private-key=/home/ubuntu/.ssh/ansible/ansible -e ami=true -e region=eu-west-1 --vault-password-file=~/.ssh/ansible/dev --inventory-file=/tmp/ansible/inventories/dev/ami_inventory_aws_ec2.yml playbooks/web.yml --limit enc_web
1

There are 1 best solutions below

0
On

Changed the inventory file to:

plugin: aws_ec2
regions:
 - eu-west-1
 - eu-west-2
keyed_groups:
 - key: tags['role']
    prefix: enc
filters:
  instance-state-name: running
  tag:ami: "true"
hostnames:
 - tag:Name

When then make the host name what was needed to match.