# amazon.aws.ec2\_metadata\_facts from remote hosts

**URL:** <https://forum.ansible.com/t/amazon-aws-ec2-metadata-facts-from-remote-hosts/44381>\
**Category:** Get Help\
**Tags:** facts, aws\
**Created:** [August 27, 2025, 5:12pm UTC](https://forum.ansible.com/t/amazon-aws-ec2-metadata-facts-from-remote-hosts/44381 "2025-08-27T17:12:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![herda05](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@herda05](https://forum.ansible.com/u/herda05)\
**Post date:** [August 27, 2025, 5:12pm UTC](https://forum.ansible.com/t/amazon-aws-ec2-metadata-facts-from-remote-hosts/44381/1 "2025-08-27T17:12:56Z")

</div>

This module says in the title:

# amazon.aws.ec2\_metadata\_facts module – Gathers facts (instance metadata) about remote hosts within EC2

However, no matter what I do the metadata returned is all on the ansible host itself an not on the designated hos tin the playbook.

```auto
- name: Build AMI from ec2 instance
  hosts: "{{ host }}"
  connection: local
  gather_facts: true

  tasks:
      #this allows us to get facts about the AWS environment and the ec2 instance
    - name: get ec2 facts
      action: amazon.aws.ec2_metadata_facts
      delegate_to: "{{ host }}"
      register: metadata

    - name: set some basic facts
      ansible.builtin.set_fact:
        aws_region: "{{ ansible_ec2_placement_region }}"
        #env: "{{ ansible_ec2_instance_tags.Name.split('-')[0] }}"
        #cust_id: "{{ ansible_ec2_instance_tags.Name.split('-')[1] }}"
        inst_id: "{{ ansible_ec2_instance_id }}"
        ami_name: "{{ env }}-{{ cust_id }}-{{ app_name }}-{{ now(utc=false,fmt='%Y%m%d-%H%M%S') }}"

    - name: show instance_id
      ansible.builtin.debug:
        msg: "{{ inst_id }}"

```

{{ host }} is the name from the inventory list that matches a web application server. However, the instance id returned is always the instance id of the ansible server, not the remote host.

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [August 27, 2025, 5:26pm UTC](https://forum.ansible.com/t/amazon-aws-ec2-metadata-facts-from-remote-hosts/44381/2 "2025-08-27T17:26:48Z")

</div>

I believe it’s because you are telling your task to connect to local - `connection: local`

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [August 27, 2025, 5:54pm UTC](https://forum.ansible.com/t/amazon-aws-ec2-metadata-facts-from-remote-hosts/44381/3 "2025-08-27T17:54:28Z")

</div>

> **[Controlling where tasks run: delegation and local actions — Ansible Community...](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#delegating-facts)**

---

<div class="post-metadata">

**Author:** ![herda05](https://avatars.discourse-cdn.com/v4/letter/h/e68b1a/32.png) [@herda05](https://forum.ansible.com/u/herda05)\
**Post date:** [August 27, 2025, 6:42pm UTC](https://forum.ansible.com/t/amazon-aws-ec2-metadata-facts-from-remote-hosts/44381/4 "2025-08-27T18:42:06Z")

</div>

Oh man! thanks. 🤦‍♂️
