gpt4 book ai didi

amazon-ec2 - 如何通过 ansible playbook 获取 ec2 实例公共(public) DNS 名称

转载 作者:行者123 更新时间:2023-12-05 04:12:55 25 4
gpt4 key购买 nike

我正在使用 ansible 来自动化 ec2 实例。我创建了 ec2 实例,然后尝试通过 ec2_remote_facts 模块获取其公共(public) dns 名称。但它给出了输出中 undefined variable 。

playbook:

- hosts: localhost
connection: local

tasks:
- name: ec2 instance facts
ec2_remote_facts:
region: ap-southeast-2
filters:
instance-state-name: running
register: ec2

- debug: var=ec2.instances.public_name


Output:PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [create ec2 instance] *****************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
"ec2.instances.public_name": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0



Output for ec2.instances.

ok: [localhost] => {
"ec2.instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"client_token": "",
"ebs_optimized": false,
"groups": [
{
"id": "sg-6c016a08",
"name": "default"
}
],
"hypervisor": "xen",
"id": "i-915b1813",
"image_id": "ami-fedafc9d",
"instance_profile": null,
"interfaces": [
{
"id": "eni-96de4acf",
"mac_address": "0a:14:ac:64:c4:13"
}
],
"kernel": null,
"key_name": "ansible.key",
"launch_time": "2016-08-29T07:32:10.000Z",
"monitoring_state": "disabled",
"persistent": false,
"placement": {
"tenancy": "default",
"zone": "ap-southeast-2c"
},
"private_dns_name": "ip-xx-xx-xx-107.ap-southeast-2.compute.internal",
"private_ip_address": "xx.xx.xx.107",
"public_dns_name": "ec2-xx-xxx-xx-80.ap-southeast-2.compute.amazonaws.com",
"ramdisk": null,
"region": "ap-southeast-2",
"requester_id": null,
"root_device_type": "ebs",
"source_destination_check": "true",
"spot_instance_request_id": null,
"state": "running",
"tags": {
"Name": "Demo"
},
"virtualization_type": "hvm",
"vpc_id": "vpc-abcaf4ce"
}
]
}

我在这里缺少什么?

谢谢

本卓

最佳答案

如果您仔细查看 ec2.instances,您可能会注意到:

  • 它是一个列表,因此您可以通过索引 ec2.instances[0] 访问项目或使用 with_items: 结构对其进行迭代。
  • 没有 public_name 属性,只有 public_dns_name

关于amazon-ec2 - 如何通过 ansible playbook 获取 ec2 实例公共(public) DNS 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39200197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com