Ansible returns 动态清单中的错误主机(私有 ip 冲突?)
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 }}"
因为我要求 both role web 和 environment ci,这些实例的 none 应该被选中,但我得到的结果是:
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
标签,找到了ci172.18.50.180,然后单独搜索角色标签,在172.18.50.180下找到了另一个,并且只是将该实例标记为正常,即使它们是 不同 vpc 上的两个不同实例。
我尝试将 ec2.ini
中的 vpc_destination_variable
更改为 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
下使用吗?任何已知的此类碰撞解决方案?
tl;dr:这正是 ec2.ini
中的 hostname_variable
的用途,如文档所述:
# 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 自动缩放组,所有主机上的标签相同),所以我需要一种方法来区分它们。
我用这个选项创建了一个 gist。我的更改是在第 848 行。这允许您在 hostname_variable
中使用多个逗号分隔的变量,例如:
hostname_variable = tag_Name,private_ip_address
我在 不同的 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 }}"
因为我要求 both role web 和 environment ci,这些实例的 none 应该被选中,但我得到的结果是:
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
标签,找到了ci172.18.50.180,然后单独搜索角色标签,在172.18.50.180下找到了另一个,并且只是将该实例标记为正常,即使它们是 不同 vpc 上的两个不同实例。
我尝试将 ec2.ini
中的 vpc_destination_variable
更改为 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
下使用吗?任何已知的此类碰撞解决方案?
tl;dr:这正是 ec2.ini
中的 hostname_variable
的用途,如文档所述:
# 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 自动缩放组,所有主机上的标签相同),所以我需要一种方法来区分它们。
我用这个选项创建了一个 gist。我的更改是在第 848 行。这允许您在 hostname_variable
中使用多个逗号分隔的变量,例如:
hostname_variable = tag_Name,private_ip_address