# Ansible unable to fetch/use temporary credentials when using metadata (IMDSv2) in Amazon linux 2023

**URL:** <https://forum.ansible.com/t/ansible-unable-to-fetch-use-temporary-credentials-when-using-metadata-imdsv2-in-amazon-linux-2023/6723>\
**Category:** Get Help\
**Tags:** playbook, aws, ansible-core\
**Created:** [June 23, 2024, 4:03am UTC](https://forum.ansible.com/t/ansible-unable-to-fetch-use-temporary-credentials-when-using-metadata-imdsv2-in-amazon-linux-2023/6723 "2024-06-23T04:03:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shreyas\_D](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shreyas_d/32/3270_2.png) [@Shreyas\_D](https://forum.ansible.com/u/Shreyas_D)\
**Post date:** [June 23, 2024, 4:03am UTC](https://forum.ansible.com/t/ansible-unable-to-fetch-use-temporary-credentials-when-using-metadata-imdsv2-in-amazon-linux-2023/6723/1 "2024-06-23T04:03:27Z")

</div>

Purpose - Starting ec2 instance via ansible playbook using “amazon.aws.ec2” module with “delegate\_to: localhost”

Error -  
\*\*  
File “/home/user/.local/lib/python3.9/site-packages/boto/auth.py”, line 1018, in get\_auth\_handler raise boto.exception.NoAuthHandlerFound( failed: [ansibleHost-ip-address → localhost] (item… instance details here … }, “msg”: “No handler was ready to authenticate. 1 handlers were checked. [‘HmacAuthV4Hanler’] Check your credentials” }  
\*\*

The same works fine when IMDSv2 is disabled and IMDSv1 is put to use

Context - IMDSV2 has additional token security while fetching AWS temporary credentials, while IMDSv1 does not.

The problem is faced only when trying to perform AWS operations (starting ec2 in our case) from within playbook, same operation works fine when performed directly via AWS cli on the same server(ec2)

Ansible version -  
ansible [core 2.15.3]  
python version = 3.9.16

Server - Managed server/node is Amazon linux 2023

Note - The IMDSv2 configs are also correctly in place like endpoint access is enabled, hop-limit is set to 2

Need some help to expedite this issue to make use of the IMDSv2 from inside the playbook

---

<div class="post-metadata">

**Author:** ![tremble](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tremble/32/247_2.png) [@tremble](https://forum.ansible.com/u/tremble)\
**Post date:** [June 24, 2024, 8:25am UTC](https://forum.ansible.com/t/ansible-unable-to-fetch-use-temporary-credentials-when-using-metadata-imdsv2-in-amazon-linux-2023/6723/2 "2024-06-24T08:25:42Z")

</div>

This is expected behaviour. Please migrate to the _ec2\_instance_ module, which should support credentials from IMDSv2.  
**Note** : While _ec2\_instance_ is **not** a _drop-in_ replacement for _ec2_, it should support all of the underlying _ec2_ functionality.

The _ec2_ module was deprecated and removed in favour of the _ec2\_instance_ module which is based upon the “boto3” SDK, and should (assuming you’re using a recent version) support IMDSv2. The _ec2_ module was removed from the amazon.aws collection in release 4.0.0 of amazon.aws (2022), primarily because it was based upon the original “boto” SDK which hasn’t seen a release since 2018, predating IMDSv2.

The availability of the _ec2_ module would seem to imply you’re using an old and unsupported version of the amazon.aws collection, and I would strongly recommend upgrading.

Latest available versions  
amazon.aws: 8.0.1  
community.aws: 8.0.0

---

<div class="post-metadata">

**Author:** ![Shreyas\_D](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shreyas_d/32/3270_2.png) [@Shreyas\_D](https://forum.ansible.com/u/Shreyas_D)\
**Post date:** [June 24, 2024, 12:19pm UTC](https://forum.ansible.com/t/ansible-unable-to-fetch-use-temporary-credentials-when-using-metadata-imdsv2-in-amazon-linux-2023/6723/3 "2024-06-24T12:19:32Z")

</div>

Yes Correct, the catch in my case was that I somehow had 2 versions of amazon.aws collection showing up  
One was version 1.x another was version 6.x

and ansible was using the first in the list i.e. 1.x

Had to update ~/.ansible.cfg file with  
“[defaults]  
collections\_path=/path/to/version6.x/ansible\_collections”  
and then additionally use the amazon.aws.ec2\_instance module

Thankyou for the help, your answer guided me to get hold of my broken piece.

---

<div class="post-metadata">

**Author:** ![tremble](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tremble/32/247_2.png) [@tremble](https://forum.ansible.com/u/tremble)\
**Post date:** [June 24, 2024, 12:57pm UTC](https://forum.ansible.com/t/ansible-unable-to-fetch-use-temporary-credentials-when-using-metadata-imdsv2-in-amazon-linux-2023/6723/4 "2024-06-24T12:57:11Z")

</div>

Just FYI, 6.x is in the final stages of its lifecycle and will likely only receive security updates at this point. It’s worth upgrading to 8.x if you can.

---

<div class="post-metadata">

**Author:** ![Shreyas\_D](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shreyas_d/32/3270_2.png) [@Shreyas\_D](https://forum.ansible.com/u/Shreyas_D)\
**Post date:** [June 25, 2024, 3:01am UTC](https://forum.ansible.com/t/ansible-unable-to-fetch-use-temporary-credentials-when-using-metadata-imdsv2-in-amazon-linux-2023/6723/5 "2024-06-25T03:01:23Z")

</div>

Definitely will give it a thought after reviewing our plans. Appreciate the Help!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 25, 2024, 3:02am UTC](https://forum.ansible.com/t/ansible-unable-to-fetch-use-temporary-credentials-when-using-metadata-imdsv2-in-amazon-linux-2023/6723/6 "2024-07-25T03:02:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
