简体   繁体   English

Ansible在动态清单中返回错误的主机(私有IP冲突?)

[英]Ansible returns wrong hosts in dynamic inventory (private ip collision?)

I have two instances on different VPCs which have the same private address. 我在具有相同专用地址的不同 VPC上有两个实例。

ci-vpc: ci-vpc:

172.18.50.180:
    tags:
        Environment: ci
        Role: aRole

test-vpc: 测试VPC:

172.18.50.180:
    tags:
        Environment: test
        Role: web

I am running the following playbook: 我正在运行以下剧本:

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

Since I am asking for both role web and environment ci, none of these instances should be picked, but nevertheless the result that I am getting is: 由于我同时要求角色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"
      }
  ]
}

Obviously this instance does not meet the requirements under hosts ... 显然,此实例不符合hosts下的要求...

It seems like ec2.py searched for the Environment tag, found ci for 172.18.50.180, then searched separately for the role tag, found another one under 172.18.50.180, and just marked that instance as ok, even though these are two different instances on different vpcs . 似乎ec2.py搜索了Environment标签,找到ci表示172.18.50.180,然后分别搜索了角色标签,在172.18.50.180下找到了另一个,然后将该实例标记为ok,即使这是两个不同的实例在不同的vpc上

I've tried changing vpc_destination_variable in ec2.ini to id but then I'm getting error when Ansible is trying to connect to these instances because it cannot connect to the id... 我尝试将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
}

Is there another option that will work under vpc_destination_variable ? vpc_destination_variable下还有其他选项vpc_destination_variable吗? Any known solution for such a collision? 对于这种碰撞有任何已知的解决方案吗?

tl;dr : This is exactly what hostname_variable in ec2.ini is for, as documented: tl; dr :这正是ec2.ini 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 I've missed it and found it after looking around in ec2.py 不幸的是,在ec2.py环顾四周后,我错过了它并找到了它

Longer answer with additional options to hostnames 主机名附加选项的答案更长

After finding out about hostname_variable , I had another problem that it can receive only one variable. 在找到关于hostname_variable ,我遇到了另一个问题,即它只能接收一个变量。 In my case I had some instances with the same private ip on one hand, and some with the same tags on the other (AWS autoscaling groups, same tags on all hosts), so I needed a way to differentiate between them. 在我的情况下,我有一些实例具有相同的专用ip,而另一些实例具有相同的标签(AWS自动缩放组,所有主机上具有相同的标签),因此我需要一种区分它们的方法。

I've created a gist with this option. 我使用此选项创建了要点 My change is in line 848. This allows you to use multiple comma separated variables in hostname_variable , eg: 我的更改在第848行中。这允许您在hostname_variable使用多个逗号分隔的变量,例如:

hostname_variable = tag_Name,private_ip_address

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM