nva
nva

Reputation: 71

Ansible get_url fails to download a protected by basic auth

I'm trying to download a protected file using HTTP from a remote server with the get_url module but the username password does not seem to get passed in the request and the task therefore fails.

I'm using Ansible 1.9.2

Here is the get_url definition I'm using:

- name: Downloading Artifact
  get_url:  
    url: "http://myserver/somefile.tar.gz" 
    dest: "/home/jdoe/somefile.tar.gz"
    url_username: "jdoe"
    url_password: "mysecret"
    mode: 0600

Here is the error I get:

failed: [myserver] => {"dest": "/home/jdoe/somefile.tar.gz", "failed": true,
"response": "HTTP Error 403: Forbidden", "state": "absent", 
"status_code": 403, "url": "http://myserver/somefile.tar.gz"}
msg: Request failed
FATAL: all hosts have already failed -- aborting

Now, I tried to download the file using cURL and it works.

Any help is appreciated as I've struggling with this for 2 days.

Upvotes: 6

Views: 13732

Answers (3)

Anna Bogdanovich
Anna Bogdanovich

Reputation: 1

I've had a similar issue in ansible 2.9. Turns out curl was also getting HTTP 403 but showing content anyway. GET_URL module is just more strict.

For me, the issue was solved by switching from the default Apache welcome page to the smth custom made.

Upvotes: 0

user2245138
user2245138

Reputation: 11

The problem is that your server does not return 401 status so that the httplib2 library can send over the BASIC authentication credentials afterwards. The solution is to upgrade to ansible >= 2.0 and use force_basic_auth: True so that the BASIC authentication credentials from the beginning.

Upvotes: 1

alfredocambera
alfredocambera

Reputation: 3410

You can use the uri module:

---                                             

- hosts: hostname                                

  tasks:                                        

  - name: "download file"                       
    uri:                                        
      url: "http://somedomain.com/file.json"    
      method: GET                               
      user: "{{ somedomain.user }}"            
      password: "{{ somedomain.password }}"    
      force_basic_auth: yes                    
      dest: /tmp/somedomain.file.json           
      return_content: yes                       

If this doesn't work, probably it will have something to do with the httplib2 library version.

Upvotes: 2

Related Questions