Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 912
nva Avatar asked Aug 07 '15 17:08

nva


2 Answers

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.

like image 167
alfredocambera Avatar answered Nov 16 '22 06:11

alfredocambera


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.

like image 1
user2245138 Avatar answered Nov 16 '22 08:11

user2245138