Ansible fact from another host

amazon ec2ansible

I'm trying to get a fact from another host in my ansible playbook:

- hosts: localhost
    tasks:
    - site_facts: name={{ var }}

- hosts: "{{ apphost }}"
  tasks:
    - shell:
        command: docker inspect --format='\{\{.Image\}\}' {{ dbname }}
        register: imagehash

- hosts: some_host
  tasks:
    - debug: var=hostvars[apphost].inventory_hostname
    - debug: var=hostvars['localhost'].inventory_hostname

The tricky part is that 'apphost' variable, passed with –extra-vars, contains the tag name of host on ec2.

However the hostvars list use ip adrress as a key, so I get this error:

fatal: [192.168.XX.XX] => Failed to template {{hostvars[apphost].inventory_hostname}}: host not found: tag_Name_docker1

Any ideas how should I get the fact from this host?

Best Answer

The proper variable appear to be

 - debug: var=hostvars[groups[apphost][0]].inventory_hostname

instead of

 - debug: var=hostvars[apphost].inventory_hostname