title: How to access hostname tags in ansible playbook based on aws ec2 inventory hostname's ip addresses?
body: <p>I am using aws ec2 plugin in ansible 2.9. I would like to connect to aws ec2 instances with private ip address but in my playbook, I want to display other hostname tags such as hostname and region along with corresponding private ip addresses. Any suggestions on how to accomplish this within the playbook?</p>
  <p>Here is my ec2 inventory:
  testall.aws_ec2.yml</p>
  <pre><code>plugin: aws_ec2
  
  hostnames:
         - tag:Name
         - tag: Region
         - private-ip-address
  compose:
    ansible_host: private_ip_address
  </code></pre>
  <p>And here is what I have in my playbook:</p>
  <pre><code>test-playbook.yml 
  ---
  - hosts:  &quot;{{ variable_host | default('test')}}&quot;
    remote_user: ubuntu
    become: yes
    become_method: sudo
    tasks:
        - debug: 
                 msg: &quot;{{ inventory_hostname }}&quot;
  
  ...
  </code></pre>
  <p>I run my playbook as follows:</p>
  <pre><code>ansible-playbook -i testall.aws_ec2.yml test-playbook.yml --extra-vars variable_host=testall
  </code></pre>
  <p>However, my playbook returns only the tag:Name (the first item specified in hostnames in inventory):</p>
  <pre><code>TASK [Gathering Facts] ************************************************************************************************
  ok: [test-abc-host1]
  ok: [test-abc-host2]
  </code></pre>