简体   繁体   中英

Ansible AWS ec2 module with tags and dynamic inventory returns wrong hosts

I have two VPCs under the same account, each of which has several hosts with private IPs.

I have one host with a tag mobile equals to true and another host with a tag of Environment equals to ci , both with the same private IP, but they reside in different VPCs.

when I run the following tag search using Ansible:

- name: "install security service"
  hosts: "tag_Environment_{{ env }}:&tag_Service_{{ service }}_true"

With the arguments env="ci" and service="mobile" I get one of the hosts back, even though each of the hosts doesn't have both tags.

Since they have the same IP, it seems like the search is merging the results and thus returns a host which has only one of the tags.

Yes, ec2.py inventory script uses vpc_destination_variable parameter to name hosts.
For VPC it is usually set to private_ip_address in ec2.ini , so you can have only one host with same IP in your inventory.

To overcome your situation, you may try play with instance_filters to filter hosts before they are saved to inventory.

example from ec2.ini:

# Retrieve only instances with (key=value) env=staging tag
# instance_filters = tag:env=staging

# Retrieve only instances with role=webservers OR role=dbservers tag
# instance_filters = tag:role=webservers,tag:role=dbservers

# Retrieve only t1.micro instances OR instances with tag env=staging
# instance_filters = instance-type=t1.micro,tag:env=staging

# You can use wildcards in filter values also. Below will list instances which
# tag Name value matches webservers1*
# (ex. webservers15, webservers1a, webservers123 etc)
# instance_filters = tag:Name=webservers1*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM