amazon.aws.ec2_metadata_facts from remote hosts

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.

- 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.

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

Oh man! thanks. :man_facepalming:

1 Like