显示主机的自定义名称

时间:2015-03-30 05:15:16

标签: ansible ansible-facts ansible-inventory

我有一个用于处理EC2实例的Ansible游戏。我使用动态广告资源(ec2.py)来获取我想要使用的实例组(hosts: tag_Service_Foo)。当我运行它时,它会产生如下输出:

GATHERING FACTS ***************************************************************
ok: [54.149.9.198]
ok: [52.11.22.29]
ok: [52.11.0.3]

但是,我可以获取"名称"来自亚马逊的特定实例的标记(我这样做并将其存储在变量中以用于剧本的几个部分)。

有没有办法让Ansible在显示进度时将此字符串用作主机名?我希望看到更具描述性的内容(因为我没有记住IP):

GATHERING FACTS ***************************************************************
ok: [main-server]
ok: [extra-server]
ok: [my-cool-server]

ec2.py广告资源脚本的输出看起来像这样(截断;它很长)。

{
  "_meta": {
    "hostvars": {
      "54.149.9.198": {
        "ec2__in_monitoring_element": false,
        "ec2_ami_launch_index": "0",
        "ec2_architecture": "x86_64",
        "ec2_client_token": "xxx",
        "ec2_dns_name": "xxx",
        "ec2_ebs_optimized": false,
        "ec2_eventsSet": "",
        "ec2_group_name": "",
        "ec2_hypervisor": "xen",
        "ec2_id": "i-xxx",
        "ec2_image_id": "ami-xxx",
        "ec2_instance_type": "xxx",
        "ec2_ip_address": "xxx",
        "ec2_item": "",
        "ec2_kernel": "",
        "ec2_key_name": "xxx",
        "ec2_launch_time": "xxx",
        "ec2_monitored": xxx,
        "ec2_monitoring": "",
        "ec2_monitoring_state": "xxx",
        "ec2_persistent": false,
        "ec2_placement": "xxx",
        "ec2_platform": "",
        "ec2_previous_state": "",
        "ec2_previous_state_code": 0,
        "ec2_private_dns_name": "xxx",
        "ec2_private_ip_address": "xxx",
        "ec2_public_dns_name": "xxx",
        "ec2_ramdisk": "",
        "ec2_reason": "",
        "ec2_region": "xxx",
        "ec2_requester_id": "",
        "ec2_root_device_name": "/dev/xvda",
        "ec2_root_device_type": "ebs",
        "ec2_security_group_ids": "xxx",
        "ec2_security_group_names": "xxx",
        "ec2_sourceDestCheck": "true",
        "ec2_spot_instance_request_id": "",
        "ec2_state": "running",
        "ec2_state_code": 16,
        "ec2_state_reason": "",
        "ec2_subnet_id": "subnet-xxx",
        "ec2_tag_Name": "main-server",
        "ec2_tag_aws_autoscaling_groupName": "xxx",
        "ec2_virtualization_type": "hvm",
        "ec2_vpc_id": "vpc-xxx"
      }
    }
  }
  "tag_Service_Foo": [
    "54.149.9.198",
    "52.11.22.29",
    "52.11.0.3"
  ],
}

2 个答案:

答案 0 :(得分:2)

您需要做的是在后处理输出的my_ec2.py上创建自己的包装器(比如ec2.py)。想法是使用behavioral hostvar ansible_ssh_host。你不仅可以使用任何语言python。只要它在stdout上打印有效的json就可以了。 Reference if needed

这将是一点点工作。但希望sudo代码能有所帮助:

output_json_map = new map
for each group in <ec2_output>: # e.g. tag_Service_Foo, I think there would be another 
                                # key in the output that contains list of group names.
  for each ip_address in group:
    hname = ec2_output._meta.hostvars.find(ip_address).find(ec2_tag_Name)

    # Add new host to the group member list
    output_json_map.add(key=group, value=hname)
    copy all vars from ec2_output._meta.hostvars.<ip_address>
                  to output_json_map._meta.hostvars.<hname>
    # Assign the IP address of this host to the ansible_ssh_host
    # in hostvars for this host
    output_json_map.add(key=_meta.hostvars.<hname>.ansible_ssh_host,
                        value=ip_address)
    output_json_map.add(key=_meta.hostvars.find(ip_address).ansible_ssh_host,
                        value=ip_address)

print output_json_map to stdout

<强> E.g。对于您的示例,my_ec2.py的输出应为:

{
  "_meta": {
    "hostvars": {
      "main-server": {
        "ansible_ssh_host": "54.149.9.198"
        --- snip ---
        "ec2_tag_Name": "main-server",
        --- snip ---
      },
      "extra-server": {
        "ansible_ssh_host": "52.11.22.29"
        --- snip ---
        "ec2_tag_Name": "extra-server",
        --- snip ---
      },
      <other hosts from all groups>
    }
  }
  "tag_Service_Foo": [
    "main-server",
    "extra-server",
    <other hosts in this group>
  ],
  "some other group": [
    <hosts in this group>,
    ...
  ],
}

显然,使用此my_ec2.py代替ec2.py作为广告资源文件。 : - )

- 编辑 -

  

1)在小组中,我只能用一个名字来引用东西吗? 2)有   没有别名的概念? 3)我想知道我是否可以使用IP地址   这些组只是修改_meta部分或者我是否需要全部修改?

是*,否和否。


*技术上首先肯定是否定的。让我解释一下。

我们在这里所做的事情可以通过以下静态库存文件来完成:

原始ec2.py返回的json等效于以下广告资源文件:

[tag_Service_Foo]
54.149.9.198 ec2_tag_Name="main-server"  ec2_previous_state_code="0" ...
52.11.22.29  ec2_tag_Name="extra-server" ec2_previous_state_code="0" ...

我们的新my_ec2.py会返回此信息:

[tag_Service_Foo]
main-server  ansible_ssh_host="54.149.9.198" ec2_tag_Name="main-server"  ec2_previous_state_code="0" ...
extra-server ansible_ssh_host="52.11.22.29"  ec2_tag_Name="extra-server" ec2_previous_state_code="0" ...

# Technically it's possible to create "an alias" for main-server like this:
main-server-alias  ansible_ssh_host="54.149.9.198" ec2_tag_Name="main-server"  ec2_previous_state_code="0" ...

现在,您可以在主机列表中使用main-server-alias运行游戏,并且ansible将在54.149.9.198上执行该游戏。

但是,这是一个很大但是当你用'all'运行游戏作为主机模式时,ansible会在main-server-alias以及{{1}上运行任务}。所以你创建的是一个上下文中的别名和另一个上下文中的新主机。我没有测试过这个 BUT部分所以如果你发现其他情况,请回来纠正我。

HTH

答案 1 :(得分:1)

如果你把

vpc_destination_variable = Name

在您的ec2.ini文件中,也应该有用。