2017-08-25 61 views
0

我有兩個不同的實例具有相同專用地址的VPC。Ansible在動態庫存中返回錯誤的主機(私有IP衝突?)

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 }}" 

因爲我要求角色網絡和環境ci,no這些實例的NE應採摘,但儘管如此,我得到的結果是:

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下要求...

好像關注Environmentec2.py標籤,發現ci爲172.18.50.180,然後單獨搜索角色標籤,在172.18.50.180下找到另一個,並將該實例標記爲ok,即使這些是兩個不同vpcs上的不同實例。

我試圖在ec2.ini改變vpc_destination_variableid但後來我發現了,當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下工作?這種碰撞的任何已知解決方案?

回答

0

TL;博士:這正是hostname_variableec2.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'. 

Unfortunetely我已經錯過了它,發現它在ec2.py

更長環顧四周後,回答主機名的附加選項

找到約後,我有另一個問題,它可以收到只有一個變量。在我的情況下,我有一些具有相同私有IP的實例,另一些具有相同的標籤(AWS自動縮放組,所有主機上的相同標籤),所以我需要一種方法來區分它們。

我用這個選項創建了一個gist。我的更改是在第848行。這允許您在hostname_variable中使用多個逗號分隔變量,例如:

hostname_variable = tag_Name,private_ip_address