EC2动态库存 - 按VPC ID过滤实例

时间:2018-01-17 18:56:10

标签: python amazon-ec2 ansible

我在Ubuntu 16.04内核4.4.0上的Python 3.5.2下使用Ansible 2.4.2.0。

我已下载EC2 inventory Python script并使用ec2.ini进行配置,如下所示:

[ec2]
regions = all
regions_exclude = us-gov-west-1,cn-north-1
destination_variable = private_dns_name
vpc_destination_variable = private_ip_address
route53 = True
rds = False
elasticache = False
all_instances = False
all_rds_instances = False
include_rds_clusters = False
all_elasticache_replication_groups = False
all_elasticache_clusters = False
all_elasticache_nodes = False
cache_path = ~/.ansible/tmp
cache_max_age = 300
nested_groups = True
replace_dash_in_groups = True
expand_csv_tags = True
group_by_instance_id = True
group_by_region = True
group_by_availability_zone = True
group_by_aws_account = False
group_by_ami_id = True
group_by_instance_type = True
group_by_instance_state = False
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
group_by_rds_engine = True
group_by_rds_parameter_group = True
group_by_elasticache_engine = True
group_by_elasticache_cluster = True
group_by_elasticache_parameter_group = True
group_by_elasticache_replication_group = True
instance_filters = vpc-id=vpc-deadbeef

[credentials]

我的广告资源目录inventory/development包含以下内容:

inventory/development/
├── ec2.ini
├── ec2.py
└── hosts

我的主机文件如下所示:

[tag_atlas_project_manager]

[manager:children]
tag_atlas_project_manager

我的ansible.cfg

[defaults]
retry_files_enabled = false
roles_path = galaxy_roles:roles
inventory = inventory/
timeout = 120

[ssh_connection]
pipelining = True
ssh_args = -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ControlMaster=auto -o ControlPath=~/.ssh/mux-%r@%h:%p -o ControlPersist=8h

好的,所以我的VPC vpc-deadbeef配置如下:

$ aws ec2 describe-vpcs | \
    jq -r '.Vpcs[] | { Id: .VpcId, CidrBlock: .CidrBlock } | select(.CidrBlock == "10.50.0.0/16")'
{
  "CidrBlock": "10.50.0.0/16",
  "Id": "vpc-deadbeef"
}

当我运行库存脚本时,我没有获得此范围之外的任何IP地址:

$ inventory/development/ec2.py --list | \
    grep -ioP '10\.\d{1,3}\.\d{1,3}.\d{1,3}' | sort -u
10.50.10.10
...

但是,当我针对all运行一个剧本时,我会看到vpc-deadbeef以外的主持人:

$ ansible -i inventory/development/ -f 30 all -m command -a true
...

10.1.30.110 | UNREACHABLE! => {
    "changed": false,
    "msg": "SSH Error: data could not be sent to remote host \"10.1.30.110\". Make sure this host can be reached over ssh",
    "unreachable": true
}
10.1.30.250 | UNREACHABLE! => {
    "changed": false,
    "msg": "SSH Error: data could not be sent to remote host \"10.1.30.250\". Make sure this host can be reached over ssh",
    "unreachable": true
}

是什么给出的?我做错了什么?

1 个答案:

答案 0 :(得分:0)

事实证明inventory/development中有另一个子目录也有ec2.ini,而且这个库存文件在运行期间被Ansible 合并

$ ansible -vvv -i inventory/development/ -u grindr all -m command -a 'true'
ansible 2.4.2.0
  config file = ansible.cfg
  configured module search path = ['/home/naftuli/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.5/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
Using ansible.cfg as config file
Parsed inventory/development/ec2.py inventory source with script plugin
Parsed inventory/development/hosts inventory source with ini plugin
Parsed inventory/development/preprod/ec2.py inventory source with script plugin
Parsed inventory/development/preprod/hosts inventory source with ini plugin

这是问题,Ansible递归地爬下来,我认为这是有道理的。