# IMDS V2 Unauthorised Error || Guidance Required

**URL:** <https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [July 26, 2022, 12:11am UTC](https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902 "2022-07-26T00:11:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SARANSH\_BANKIWAL](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@SARANSH\_BANKIWAL](https://forum.ansible.com/u/SARANSH_BANKIWAL)\
**Post date:** [July 26, 2022, 12:11am UTC](https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902/1 "2022-07-26T00:11:54Z")

</div>

Hello all,  
Currently i was working on migration from IMDS v1 to IMDS v2 in AWS and got the below error.

FAILED! =\> {“changed”: false, “msg”: “Failed to retrieve metadata from AWS: HTTP Error 401: Unauthorized”, “response”: {“body”: “\<?xml version=\"1.0\" encoding=\"iso-8859-1\"?\>\n\n\<html xmlns="[http://www.w3.org/1999/xhtml\](http://www.w3.org/1999/xhtml%5C)” xml:lang="en" lang="en"\>\n \n 401 - Unauthorized\n \n \n

# 401 - Unauthorized
\n \n\n", “connection”: “close”, “content-length”: “343”, “content-type”: “text/html”, “date”: “Mon, 25 Jul 2022 23:26:11 GMT”, “msg”: “HTTP Error 401: Unauthorized”, “server”: “EC2ws”, “status”: 401, “url”: “[http://169.254.169.254/latest/meta-data/](http://169.254.169.254/latest/meta-data/)”}}

**My ansible version on this Instance is : (ansible --version)**

ansible 2.9.15  
config file = /etc/ansible/ansible.cfg  
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]  
ansible python module location = /usr/lib/python2.7/dist-packages/ansible  
executable location = /usr/bin/ansible  
python version = 2.7.17 (default, Jul 1 2022, 15:56:32) [GCC 7.5.0]

Can someone please in this as stuck on this.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [July 26, 2022, 12:24am UTC](https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902/2 "2022-07-26T00:24:58Z")

</div>

Hi Saransh,

I am going to guess that it has to do with your version of ansible as it appears 2.9 does not yet support IMDS v2, where as latest version (at least 5 and above) doesdoes, as noted in this link:

“The module is configured to utilize the session oriented Instance Metadata Service v2 (IMDSv2) [https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html).”

[https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2\_metadata\_facts\_module.html](https://docs.ansible.com/ansible/latest/collections/amazon/aws/ec2_metadata_facts_module.html)  
[https://docs.ansible.com/ansible/5/collections/amazon/aws/ec2\_metadata\_facts\_module.html](https://docs.ansible.com/ansible/5/collections/amazon/aws/ec2_metadata_facts_module.html)

- Rilindo

---

<div class="post-metadata">

**Author:** ![SARANSH\_BANKIWAL](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@SARANSH\_BANKIWAL](https://forum.ansible.com/u/SARANSH_BANKIWAL)\
**Post date:** [July 26, 2022, 12:46am UTC](https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902/3 "2022-07-26T00:46:48Z")

</div>

Hi , thanks for the prompt response,  
I read the article previously as well but was not able to install Ansible to a version of 5 or above because of less available resources. Can you please help in the same.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [July 26, 2022, 1:53am UTC](https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902/4 "2022-07-26T01:53:53Z")

</div>

You probably won’t be able to upgrade as long as you are using Python 2.7. You will have to figure out a way to get a newer version of Python on the machine if you intend to upgrade to the most recent version of Ansible.

- Rilindo

---

<div class="post-metadata">

**Author:** ![SARANSH\_BANKIWAL](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@SARANSH\_BANKIWAL](https://forum.ansible.com/u/SARANSH_BANKIWAL)\
**Post date:** [July 26, 2022, 2:38am UTC](https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902/5 "2022-07-26T02:38:04Z")

</div>

Hi Rilindo,  
As stated by you i upgraded the python version and ansible version as well. Attaching screenshots for the reference. But still the same issue persists in the Metadata fetch.

 ![Screenshot 2022-07-26 at 8.02.56 AM.png](https://us1.discourse-cdn.com/ansible/original/2X/6/65fb6995129f931bd654e4399d2dd7b0b916c2ac.png)

---

<div class="post-metadata">

**Author:** ![Rilindo\_Foster1](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@Rilindo\_Foster1](https://forum.ansible.com/u/Rilindo_Foster1)\
**Post date:** [July 26, 2022, 2:43am UTC](https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902/6 "2022-07-26T02:43:46Z")

</div>

At this point with a supported ansible version, can you paste in the code you were using to query the metadata service?

> **(attachments)**
>
> ![Screenshot 2022-07-26 at 8.02.56 AM.png](https://us1.discourse-cdn.com/ansible/original/2X/6/65fb6995129f931bd654e4399d2dd7b0b916c2ac.png)

---

<div class="post-metadata">

**Author:** ![SARANSH\_BANKIWAL](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@SARANSH\_BANKIWAL](https://forum.ansible.com/u/SARANSH_BANKIWAL)\
**Post date:** [July 26, 2022, 2:54am UTC](https://forum.ansible.com/t/imds-v2-unauthorised-error-guidance-required/36902/7 "2022-07-26T02:54:18Z")

</div>

PFB the used code for checking the IMDS V2 instance.

 ![Screenshot 2022-07-26 at 8.23.50 AM.png](https://us1.discourse-cdn.com/ansible/original/2X/b/b16c2508af255b2449b57bbccbb0777f78d6e366.png)
