Closed tonyswu closed 3 months ago
can we get any alternative solution for this as we are also facing similar issue.
I am just doing it manually right now. I really only need instance ID from metadata, and this is what I am doing:
- name: Get instance ID
block:
- shell: |
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 120")
curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/instance-id
register: metadata_curl_return
- set_fact:
ansible_ec2_instance_id: '{{ metadata_curl_return.stdout_lines[0] }}'
Hello @tonyswu @abhishek-yadav32024 will you be willing to propose a patch and eventually open a PR for this issue? Thanks.
Yeah, I'd be willing to work on a PR for this. I'll see if I can put some time into this over the weekend.
@alinabuzachis I've submitted pull request https://github.com/ansible-collections/amazon.aws/pull/2209 for this.
Summary
ec2_metadata_facts
sometimes returns 401 unauthorized if the IMDSv2 token times out.Looking at code here https://github.com/ansible-collections/amazon.aws/blob/main/plugins/modules/ec2_metadata_facts.py#L613, 60 seconds may be too short if there are a lot of metadata to be loaded. Couple of potential solutions:
ec2_metadata_facts
to retrieve specific set of keys (rather than the entire metadata).Issue Type
Bug Report
Component Name
ec2_metadata_facts
Ansible Version
Collection Versions
AWS SDK versions
Configuration
OS / Environment
Rocky9
Steps to Reproduce
Expected Results
Expected to see variable such as
ansible_ec2_instance_id
populated.Actual Results
To be clear, this is not a problem with the instance itself. Manually retrieving metadata works.
Code of Conduct