2

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?

rybit
  • 716
  • 3
  • 17
  • 25
  • 1
    You're using static inventory files instead of [the provided dynamic inventory scripts](https://docs.ansible.com/ansible/intro_dynamic_inventory.html)? The Amazon one provides a whole host of variables to use. – Xiong Chiamiov May 27 '16 at 23:38
  • We have stuff in aws/digitalocean/rackspace - we don't want to configure only using ec2 variables – rybit Jun 01 '16 at 21:09
  • You can use multiple dynamic inventory files by specifying a directory instead of a file, and merge groups together into one role. That will definitely be a more scalable solution in the long term. – Xiong Chiamiov Jun 06 '16 at 21:06

1 Answers1

1

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.

Arbab Nazar
  • 22,378
  • 10
  • 76
  • 82