Cannot use add_host when using ec2.py?

Is it normal for add_host to not work when using ec2.py? I’m in a playbook that is using ec2.py as the inventory source and I’m trying to create a few groups dynamically using ec2 tag values.

When I run debug on the hostvars afterwards, the new group name isn’t there.

Inventory output:

jenkins@ip-172-31-35-124:~/jobs/JobName/workspace$ bin/devtools/ansible/ec2.py --list --refresh-cache

“tag_ec2id_test10”: [
“172.31.31.72”,
“172.31.45.13”
“tag_ec2type_mysql”: [
“172.31.25.13”
],
“tag_ec2type_apache”: [
“172.31.31.72”

Start of the play

  • name: Gather ec2 facts
    ec2_facts:
    when: provider == “ec2”

  • name: Add instance to iaas-provider-agnostic type group
    add_host: hostname={{ inventory_hostname }} groups=“{{ ec2_tag_ec2type }}”
    when: provider == “ec2”

A call to debug on one of these hosts, inserted after the above calls:

"ec2_tag_ec2id": "test10", 
"ec2_tag_ec2id_and_ec2type": "test10_apache2", 
"ec2_tag_ec2type": "apache2", 

...

"group_names": [
"***************************", 
"***************************", 
"i-********", 
"key_*******", 
"security_group_***************************", 
"security_group_***************************", 
"security_group_***************************", 
"***************************", 
"***************************", 
"tag_ec2id_and_ec2type_test10_apache2", 
"tag_ec2id_test10", 
"tag_ec2type_apache2", 
"tag_provisioned_True", 
"type_m1_medium", 
"us-west-2", 
"us-west-2b"
        ], 

TIA for any insights…hopefully I’ve just got an obvious typo.
-Mark

I can’t see any reason in the code for this to not work. Could you show us the -vvvv output from ansible-playbook? Maybe something isn’t evaulated the way you expect.

Yea, seems like it should work. I opened a github issue with a simpler localhost test-case.

https://github.com/ansible/ansible/issues/5791

A lot of these are obviously redacted :), but I’ve triple-checked and there is no ‘apache2’ or ‘mysql’ group there.

FWIW, add_host and ec2.py are unrelated.

There’s probably just a basic understanding issue here, and I don’t think we’re quite into “file a github ticket” area yet.

Bah! Obfuscation gone wrong…I never know how much is prudent to remove. :slight_smile:

In my last message, the group webnode==apache2 and db==mysql…I missed some of them when replacing.

Sorry!

Ok, saw the note on the github ticket (thanks, and sorry if filing it was premature). Not sure whether to continue here or there; here I guess…

So for my real use case of this on ec2, I posted more debug output…should that be working? More or less I’m attempting something like this from ansible-examples: https://github.com/ansible/ansible-examples/blob/master/openshift/roles/ec2/tasks/main.yml, except these instances already exist so I’m using ec2.py to pull in the tags to use for the provider-agnostic group names, which will then be used when calling deployment roles. This playbook I’m trying to do this grouping in is sort of an intermediary…I have existing plays for these roles and I didn’t want to go changing groups: db to groups: tag_ec2type_db for roles that otherwise have nothing to do with ec2.

Thank you,
Mark

I swapped the add_host tasks for some group_by’s and its working.

Thanks,
Mark

This may also be useful for anyone having this problem:

https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/ansible-project/cpnrBRxLy0E/W-nqNgYkR2sJ