我将使用模块 ec2_facts ( https://docs.ansible.com/ansible/2.3/ec2_facts_module.html ) 收集 ec2 事实,该模块将从 AWS EC2 实例元数据 ( http://docs.aws.amazon.com/收集数据AWSEC2/latest/UserGuide/ec2-instance-metadata.html)。
然后我会使用事实ansible_ec2_public_ipv4来获取公共 IP。
以下是模块输出的示例:
[centos@ip-10-0-0-172 ~]$ ansible localhost -m ec2_facts
[WARNING]: provided hosts list is empty, only localhost is available
localhost | SUCCESS => {
"ansible_facts": {
"ansible_ec2_ami_id": "ami-e1dd0398",
"ansible_ec2_ami_launch_index": "0",
"ansible_ec2_ami_manifest_path": "(unknown)",
"ansible_ec2_block_device_mapping_ami": "/dev/sda1",
"ansible_ec2_block_device_mapping_root": "/dev/sda1",
"ansible_ec2_hostname": "ip-10-0-0-172.eu-west-1.compute.internal",
"ansible_ec2_instance_action": "none",
"ansible_ec2_instance_id": "i-0f6499c2ea3019f72",
"ansible_ec2_instance_type": "t2.medium",
"ansible_ec2_local_hostname": "ip-10-0-0-172.eu-west-1.compute.internal",
"ansible_ec2_local_ipv4": "10.0.0.172",
"ansible_ec2_mac": "06:24:83:24:c2:22",
"ansible_ec2_metrics_vhostmd": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_device_number": "0",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_interface_id": "eni-f22ad9c3",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_ipv4_associations_52.17.160.237": "10.0.0.172",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_local_hostname": "ip-10-0-0-172.eu-west-1.compute.internal",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_local_ipv4s": "10.0.0.172",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_mac": "06:24:83:24:c2:22",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_owner_id": "382756349351",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_public_hostname": "ec2-52-17-160-237.eu-west-1.compute.amazonaws.com",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_public_ipv4s": "52.17.160.237",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_security_group_ids": "sg-52937729",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_security_groups": "allin",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_subnet_id": "subnet-6399db2a",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_subnet_ipv4_cidr_block": "10.0.0.0/24",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_vpc_id": "vpc-9c4906fb",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_vpc_ipv4_cidr_block": "10.0.0.0/16",
"ansible_ec2_network_interfaces_macs_06_24_83_24_c2_22_vpc_ipv4_cidr_blocks": "10.0.0.0/16",
"ansible_ec2_placement_availability_zone": "eu-west-1a",
"ansible_ec2_placement_region": "eu-west-1",
"ansible_ec2_product_codes": "aw0evgkw8e5c1q413zgy5pjce",
"ansible_ec2_profile": "default-hvm",
"ansible_ec2_public_hostname": "ec2-52-17-160-237.eu-west-1.compute.amazonaws.com",
"ansible_ec2_public_ipv4": "52.17.160.237",
"ansible_ec2_public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQChJakGTWO5BFSTbpsM7moXy6kwoMPMT6WokuMtYhtJhjpjDxs5XzOBs5SxqwLVOVR+TIWJjZhxWGBtTE+FNx6sXT51pGB9NSJzR+uOVCHrruWMdbPtDTZxXHm+cyNnmi2KGM0Rs4EWWgqW4ROAp940RJSoIqtFTCgMnpXrWX+z7t9FyZdu78Ug5/fnrMQC4d1v8pmOFnYmarbd/gipZaX3izcOfbxPNN8jor0gRBpKEgSBTApqeFiaj63XPv42p2TeumO6NHbdU6nAN/NFDz56Mtv2T4W5yTTSBx0OjV9XIdXo8FXpkqwhv1WNvq1ksNQAi7IKbSpeizn62yQswNNj curso-itnow\n",
"ansible_ec2_reservation_id": "r-053a78e239d501ec7",
"ansible_ec2_security_groups": "allin",
"ansible_ec2_services_domain": "amazonaws.com",
"ansible_ec2_services_partition": "aws",
"ansible_ec2_user_data": null
},
"changed": false
}
问候