[英]Ansible AWS ec2 module with tags and dynamic inventory returns wrong hosts
[英]Ansible returns wrong hosts in dynamic inventory (private ip collision?)
我在具有相同专用地址的不同 VPC上有两个实例。
ci-vpc:
172.18.50.180:
tags:
Environment: ci
Role: aRole
测试VPC:
172.18.50.180:
tags:
Environment: test
Role: web
我正在运行以下剧本:
- name: "print account specific variables"
hosts: "tag_Environment_ci:&tag_Role_web"
tasks:
- name: "print account specific variables for account {{ account }}"
debug:
msg:
- 'ec2_tag_Name': "{{ ec2_tag_Name }}"
'ec2_tag_Role': "{{ ec2_tag_Role }}"
'ec2_private_ip_address': "{{ ec2_private_ip_address }}"
'ec2_tag_Environment': "{{ ec2_tag_Environment }}"
由于我同时要求角色Web和环境ci,因此不应选择这些实例,但无论如何,我得到的结果是:
ok: [172.18.50.180] => {
"changed": false,
"msg": [
{
"ec2_private_ip_address": "172.18.50.180",
"ec2_tag_Environment": "test",
"ec2_tag_Name": "test-web-1",
"ec2_tag_Role": "web"
}
]
}
显然,此实例不符合hosts
下的要求...
似乎ec2.py
搜索了Environment
标签,找到ci表示172.18.50.180,然后分别搜索了角色标签,在172.18.50.180下找到了另一个,然后将该实例标记为ok,即使这是两个不同的实例在不同的vpc上 。
我尝试将vpc_destination_variable
中的ec2.ini
更改为id
但是当Ansible由于无法连接到id而试图连接到这些实例时出现错误……
fatal: [i-XXX]: UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname i-XXX: Name or service not known\r\n", "unreachable": true
}
在vpc_destination_variable
下还有其他选项vpc_destination_variable
吗? 对于这种碰撞有任何已知的解决方案吗?
tl; dr :这正是ec2.ini
hostname_variable
的ec2.ini
,如记录所示:
# This allows you to override the inventory_name with an ec2 variable, instead
# of using the destination_variable above. Addressing (aka ansible_ssh_host)
# will still use destination_variable. Tags should be written as 'tag_TAGNAME'.
不幸的是,在ec2.py
环顾四周后,我错过了它并找到了它
主机名附加选项的答案更长
在找到关于hostname_variable
,我遇到了另一个问题,即它只能接收一个变量。 在我的情况下,我有一些实例具有相同的专用ip,而另一些实例具有相同的标签(AWS自动缩放组,所有主机上具有相同的标签),因此我需要一种区分它们的方法。
我使用此选项创建了要点 。 我的更改在第848行中。这允许您在hostname_variable
使用多个逗号分隔的变量,例如:
hostname_variable = tag_Name,private_ip_address
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.