如何从AWS上的dynamic库存返回私有IP?

无论我尝试什么,出于某种原因,当我使用可靠的dynamic库存脚本(ec2.py + ec2.ini)运行一个可靠的ad-hoc模块时,将仅返回公共 IP用于标记查询,并尝试通过SSH连接到目标的公共 IP。 例如,如果我运行:

ansible -m ping tag_env_dev 

然后,它尝试通过公共IP连接,尽pipe私有IP是可取的(为了安全,复杂性和成本的原因)。 我试图在我的ec2.ini文件中调整以下选项:

 regions = us-east-1 # to restrict to us-east-1 region destination_variable = public_dns_name # I've also tried private_dns_name and private_ip_address, all of which still attempt to connect to the public IP of the destination instance(s) vpc_destination_variable = ip_address # also tried private_ip_address 

如果我运行./ec2.py --list --refresh-cache | grep -B 5 -A 5 "tag_env_dev" ./ec2.py --list --refresh-cache | grep -B 5 -A 5 "tag_env_dev" ,我得到一个只返回公共IP的结果:

 "tag_env_dev": [ "{{public ip here}}" ], 

每次后续尝试后,我运行./ec2.py --list --refresh-cache清除并重新生成caching。 然后,我将重新运行ansible -m ping tag_env_dev (或类似的),然后我将在SSH上获得连接超时到实例的公共IP地址。

我已经证实,我可以SSH直接进入“公共主机名”(在VPC内parsing为私有IP)和私有IP,而不是直接(如预期的)公共IP。 所以这不是一个关键的身份validation问题。

我也有一个IAMangular色,这个服务器分配给它,有足够的权限来执行这些操作(例如,它只有ec2和vpc只读)。

附加信息:

我在与testing目标相同的VPC中运行ec2实例。 目标上的安全组configuration为允许来自其内部CIDR块范围的SSH。 主持人还具有足以发现目标的私有IP的IAMangular色

任何帮助将不胜感激。

这里是一个ec2.ini的工作示例(使用2.1和2.3.1进行testing)

 [ec2] regions = us-east-1,us-west-2 regions_exclude = destination_variable = private_ip_address hostname_variable = peerio vpc_destination_variable = private_ip_address route53 = False rds = False elasticache = False all_instances = False #instance_states = pending, running, shutting-down, terminated, stopping, stopped all_rds_instances = False all_elasticache_replication_groups = False all_elasticache_clusters = False all_elasticache_nodes = False cache_path = ~/.ansible/tmp cache_max_age = 300 nested_groups = False replace_dash_in_groups = True expand_csv_tags = False group_by_instance_id = True group_by_region = True group_by_availability_zone = True group_by_ami_id = True group_by_instance_type = True group_by_key_pair = True group_by_vpc_id = True group_by_security_group = True group_by_tag_keys = True group_by_tag_none = True group_by_route53_names = True #pattern_include = staging-* #pattern_exclude = staging-* #instance_filters = instance-type=t1.micro,tag:env=staging #only process items we tagged instance_filters = tag:serviceclass=* boto_profile = ansible 

然后,应该使用他们的私有IP作为标识符列出实例:

 ./ec2.py --list { "_meta": { "hostvars": { "10.255.100.138": { "ansible_ssh_host": "10.255.100.138", "ec2__in_monitoring_element": false, "ec2_ami_launch_index": "0", ... "ec2_vpc_id": "vpc-57ed3733" }, "10.255.100.142": { "ansible_ssh_host": "10.255.100.142", "ec2__in_monitoring_element": false, ...