rybit
rybit

Reputation: 756

How do I use the external IP of an ec2 instance in an ansible template?

I am configuring nodes that are in rackspace/digitalocean/aws/google cloud and need to access the external IP of the ec2 instances in a template in ansible.

I want to define my machines in the host file like this:

taps_1 ansible_host=54.123.456.789  dc=aws-richmond provider=aws

and then in a template (like one for iptables):

-A FWR -ieth0 -p tcp -s {{ hostvars[host].ansible_host }} --dport 80   -j ACCEPT

but that value doesn't exist :/ if I use the ansible_fqdn or the IP provided on the ansible_default_ipv4 I only get the internal IP (e.g. 172.31.50.181)

We have been defining our infra like this:

54.123.456.789 name=taps_1 dc=aws-richmond provider=aws

and accessing by doing hostvars[host].inventory_name, but I would like to not do that.

Ideas?

Upvotes: 2

Views: 1395

Answers (1)

Arbab Nazar
Arbab Nazar

Reputation: 23791

In Ansible 1.9 you can use the ansible_ssh_host variable while on Ansible 2.0 you can use the ansible_host

In your template you just need to use this variable to get the public IP of your host and it will get it from your inventory file:

for example: Ansible 1.9

{{ ansible_ssh_host }}

or Ansible 2.0

{{ ansible_host }}

Hope it will help you.

Upvotes: 1

Related Questions