Reputation: 11
I am on working on a playbook that deploys my web-application to an EC2 instance. I am stuck with finding the public IP of a created instance.
I am trying to capture the public IP using {{ hostvars[groups['aws'][0]]['ansible_eth0']['ipv4']['address'] }}
, but I am getting the 172.31.93.XXX which is the private IP.
Output of the below task is:
- debug: var=ansible_all_ipv4_addresses
TASK [debug] *******************************************************************
ok: [54.208.31.XX] => {
"ansible_all_ipv4_addresses": [
"172.31.93.XXX"
]
}
Is there a way to use public IP of the host?
Upvotes: 0
Views: 7243
Reputation: 146
I would gather ec2 facts with module ec2_facts (https://docs.ansible.com/ansible/2.3/ec2_facts_module.html) which would gather the data from AWS EC2 Instance MetaData (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html).
Then i would use the fact ansible_ec2_public_ipv4 to get the public IP.
Here is an example of the output of the module:
[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
}
regards
Upvotes: 3
Reputation: 68489
Use ipify_facts
module.
ipify_facts - Retrieve the public IP of your internet gateway.
For example:
- name: Get my public IP
ipify_facts:
- debug: var=ipify_public_ip
Upvotes: 1