title: How to know external ip address of ansible client
body: <p>During deployment needed setup ufw firewall rule: enable access for hosts group <code>servers</code> from inventory. Target host is anazon ec2 instance, but it maybe placed at other provider.</p>
  
  <p>I tried:</p>
  
  <pre><code>- name: Ufw rules
    ufw: rule=allow from_ip={{ hostvars[item]['ansible_eth0']['ipv4']['address'] }}
    with_items: groups['servers']
    notify: 
      - Restart ufw
  </code></pre>
  
  <p>file <code>hosts</code>:</p>
  
  <pre><code>..
  [servers]
  server1.site.com
  server2.site.com
  server3.site.com
  ..
  </code></pre>
  
  <p>But host <code>server1.site.com</code>, actually have host_vars:</p>
  
  <pre><code>"ansible_eth0": {
     "active": true, 
     "device": "eth0", 
       "ipv4": {
          "address": "10.x.x.x", 
           ...
  </code></pre>
  
  <p>Ip address 10.x.x.x, as I understand, is internal amazon network ip address.</p>
  
  <p>If execute <code>ping server1.site.com</code> from outside ec2, I get:</p>
  
  <pre><code>64 bytes from server1.site.com (46.x.x.x): icmp_seq=1 ttl=56 time=49.5 ms
  ...
  </code></pre>
  
  <p>Executing <code>ansible server1.site.com -m setup -i hosts | grep 46.x.x.x</code> found nothing.</p>
  
  <p>How to known external ip address of host from inventory group using ansible?
  Or how setup ufw firewall using host names from inventory?</p>