在Ansible中捕获EC2实例的公共IP

时间:2017-11-20 06:40:39

标签: ansible ansible-facts

我正在制作一个将我的网络应用程序部署到EC2实例的剧本。我很难找到创建实例的公共IP。

我正在尝试使用{{ hostvars[groups['aws'][0]]['ansible_eth0']['ipv4']['address'] }}捕获公共IP,但我得到了172.31.93.XXX这是私有IP。

以下任务的输出是:

- debug: var=ansible_all_ipv4_addresses
TASK [debug] *******************************************************************

ok: [54.208.31.XX] => {
    "ansible_all_ipv4_addresses": [
        "172.31.93.XXX"
    ]
}

有没有办法使用主机的公共IP?

2 个答案:

答案 0 :(得分:2)

我将使用模块ec2_facts(https://docs.ansible.com/ansible/2.3/ec2_facts_module.html)收集ec2事实,该模块将从AWS EC2 Instance MetaData(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
}

问候

答案 1 :(得分:1)

使用ipify_facts module

  

ipify_facts - 检索互联网网关的公共IP。

例如:

- name: Get my public IP
  ipify_facts:

- debug: var=ipify_public_ip