Buggy B
Buggy B

Reputation: 765

Ansible 2.4 uri module downloads file (1K) even when it does not exist - url returns 404 error

I am trying to run the following code, looking for myfile.JAR file which does not exist at the given url. I am expecting this task to fail and not download any file.

    - name: Download using URI
      uri:
        url: "http://1.2.3.4:8081/myurl/myfile.JAR"
        dest: ./
        method: GET
        status_code: [200] 

The task fails but yet under ./ there is a myfile.JAR of 1K size.

Why is it downloading this file (true size is 20MB)

Upvotes: 1

Views: 1089

Answers (1)

JGK
JGK

Reputation: 4168

Slightly modifying your playbook to

---
- hosts: localhost
  connection: local
  gather_facts: False

  tasks:
  - name: Download using URI
    uri:
      url: "http://www/myurl/myfile.JAR"
      dest: ./uri.jar
    ignore_errors: yes
  - name: Download using GET_URL
    get_url:
      url: "http://www/myurl/myfile.JAR"
      dest: ./get_url.jar
    ignore_errors: yes

results to the following:

  • uri downloads the file, i.e. the contents of the html error message of the webserver.
  • get_url does not download anything that does not exist

Why?

get_url is especially for downloading the contents of the given url. If the http status code is not 200 then nothing is downloaded, that means if the file does not exist on the remote webserver the task only fails.

uri on the other hand is for interacting with webservices like REST or SOAP or what ever. When a restful request fails with a http status code of 400 for example additional information can be transmitted in the body of the response. For examples see https://praveer09.github.io/technology/2016/07/07/rest-error-responses-in-spring-boot/ And exactly this contents is downloaded with the uri task. The saved result can then be processed by further tasks.

Upvotes: 1

Related Questions