[英]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.