# Can't get authentication to work with uri module

**URL:** <https://forum.ansible.com/t/cant-get-authentication-to-work-with-uri-module/18120>\
**Category:** Ansible Project\
**Created:** [April 29, 2015, 12:19am UTC](https://forum.ansible.com/t/cant-get-authentication-to-work-with-uri-module/18120 "2015-04-29T00:19:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brenda\_Bell](https://avatars.discourse-cdn.com/v4/letter/b/6bbea6/32.png) [@Brenda\_Bell](https://forum.ansible.com/u/Brenda_Bell)\
**Post date:** [April 29, 2015, 12:19am UTC](https://forum.ansible.com/t/cant-get-authentication-to-work-with-uri-module/18120/1 "2015-04-29T00:19:21Z")

</div>

I can ssh to the server and curl successfully with authentication:

`  
$ curl --user user:password [http://localhost:8080/api/v1/clusters](http://localhost:8080/api/v1/clusters)  
{  
“href” : “[http://localhost:8080/api/v1/clusters](http://localhost:8080/api/v1/clusters)”,  
“items” :   
}

`

But when I try to use Ansible to get the same url with the same credentials, it fails to authenticate. My task looks like this:

`

- uri:  
url: [http://localhost:8080/api/v1/clusters](http://localhost:8080/api/v1/clusters)  
method: GET  
user: “user”  
password: “password”

`

I get an authentication error from my web service:

`  
failed: [172.98.1.129] =\> {“content”: “{\n "status": 403,\n "message": "Full authentication is required to access this resource"\n}”, “content\_length”: “91”, “content\_type”: “text/plain;charset=ISO-8859-1”, “failed”: true, “redirected”: false, “server”: “Jetty(7.6.7.v20120910)”, “set\_cookie”: “AMBARISESSIONID=7ek08rb33476kg9z8jyiw40y;Path=/”, “status”: 403}  
msg: Status code was not [200]

`

I’m definitely using the correct username and password in both places.

So my question is how does the uri module’s authentication differ from curl?

---

<div class="post-metadata">

**Author:** ![Mirko\_Friedenhagen](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Mirko\_Friedenhagen](https://forum.ansible.com/u/Mirko_Friedenhagen)\
**Post date:** [April 29, 2015, 5:42am UTC](https://forum.ansible.com/t/cant-get-authentication-to-work-with-uri-module/18120/2 "2015-04-29T05:42:40Z")

</div>

Just a guess:

Curl uses preemptive authentication, i.e. sends the Authorization header on the first contact.

URL first goes to the resource and only sends authorization data on a second call, after it received http/401 on the first call.

My suggestion: use curl here. IIRR, there is already a PR for preemptive authorization.

Regards  
Mirko

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 29, 2015, 11:31am UTC](https://forum.ansible.com/t/cant-get-authentication-to-work-with-uri-module/18120/3 "2015-04-29T11:31:07Z")

</div>

You need to specify the force\_basic\_auth option with a value of yes/true and things should start working

---

<div class="post-metadata">

**Author:** ![Joaquin\_Menchaca](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Joaquin\_Menchaca](https://forum.ansible.com/u/Joaquin_Menchaca)\
**Post date:** [April 4, 2017, 1:51am UTC](https://forum.ansible.com/t/cant-get-authentication-to-work-with-uri-module/18120/4 "2017-04-04T01:51:19Z")

</div>

force\_basic auth did not work, getting 401:

- name: Get License  
uri:  
url: http://{{ ansible\_host }}:{{ artifactory\_port }}/artifactory/api/system/licenses  
method: GET  
user: “{{ artifactory\_admin.user }}”  
passwd: “{{ artifactory\_admin.password }}”  
force\_basic\_auth: yes  
return\_content: yes

But, this works fine:

- name: Get License  
uri:  
url: http://{{ artifactory\_admin.user }}:{{ artifactory\_admin.password }}@{{ ansible\_host }}:{{ artifactory\_port }}/artifactory/api/system/licenses  
method: GET  
return\_content: yes

Anyway to get user and password to work?
