ansible.builtin.uri (v2.3.0.0-1) — module

Interacts with webservices

| "added in version" 1.1 of ansible.builtin"

Authors: Romeo Theriault (@romeotheriault)

stableinterface | supported by core

Install Ansible via pip

Install with pip install ansible==2.3.0.0.post1

Description

Interacts with HTTP and HTTPS web services and supports Digest, Basic and WSSE HTTP authentication mechanisms.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Check that you can connect (GET) to a page and it returns a status 200
  uri:
    url: http://www.example.com
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Check that a page returns a status 200 and fail if the word AWESOME is not
# in the page contents.
- uri:
    url: http://www.example.com
    return_content: yes
  register: webpage
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Fail if AWESOME is not in the page content
  fail:
  when: "'AWESOME' not in webpage.content"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.

- name: Create a JIRA issue
  uri:
    url: https://your.jira.example.com/rest/api/2/issue/
    method: POST
    user: your_username
    password: your_pass
    body: "{{ lookup('file','issue.json') }}"
    force_basic_auth: yes
    status_code: 201
    body_format: json
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Login to a form based webpage, then use the returned cookie to
# access the app in later tasks

- uri:
    url: https://your.form.based.auth.example.com/index.php
    method: POST
    body: "name=your_username&password=your_password&enter=Sign%20in"
    status_code: 302
    headers:
      Content-Type: "application/x-www-form-urlencoded"
  register: login
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- uri:
    url: https://your.form.based.auth.example.com/dashboard.php
    method: GET
    return_content: yes
    headers:
      Cookie: "{{login.set_cookie}}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Queue build of a project in Jenkins
  uri:
    url: "http://{{ jenkins.host }}/job/{{ jenkins.job }}/build?token={{ jenkins.token }}"
    method: GET
    user: "{{ jenkins.user }}"
    password: "{{ jenkins.password }}"
    force_basic_auth: yes
    status_code: 201

Inputs

    
url:
    default: null
    description:
    - HTTP or HTTPS URL in the form (http|https)://host.domain[:port]/path
    required: true

body:
    default: null
    description:
    - The body of the http request/response to the web service. If C(body_format) is set
      to 'json' it will take an already formatted JSON string or convert a data structure
      into JSON.
    required: false

dest:
    default: null
    description:
    - path of where to download the file to (if desired). If I(dest) is a directory, the
      basename of the file on the remote server will be used.
    required: false

user:
    default: null
    description:
    - username for the module to use for Digest, Basic or WSSE authentication.
    required: false

method:
    choices:
    - GET
    - POST
    - PUT
    - HEAD
    - DELETE
    - OPTIONS
    - PATCH
    - TRACE
    - CONNECT
    - REFRESH
    default: GET
    description:
    - The HTTP method of the request or response. It MUST be uppercase.
    required: false

others:
    description:
    - all arguments accepted by the M(file) module also work here
    required: false

HEADER_:
    default: null
    description:
    - Any parameter starting with "HEADER_" is a sent with your request as a header. For
      example, HEADER_Content-Type="application/json" would send the header "Content-Type"
      along with your request with a value of "application/json". This option is deprecated
      as of C(2.1) and may be removed in a future release. Use I(headers) instead.
    required: false

creates:
    description:
    - a filename, when it already exists, this step will not be run.
    required: false

headers:
    default: null
    description:
    - Add custom HTTP headers to a request in the format of a YAML hash. As of C(2.3)
      supplying C(Content-Type) here will override the header generated by supplying C(json)
      for I(body_format).
    required: false
    version_added: '2.1'
    version_added_collection: ansible.builtin

removes:
    description:
    - a filename, when it does not exist, this step will not be run.
    required: false

timeout:
    default: 30
    description:
    - The socket level timeout in seconds
    required: false

password:
    default: null
    description:
    - password for the module to use for Digest, Basic or WSSE authentication.
    required: false

body_format:
    choices:
    - raw
    - json
    default: raw
    description:
    - The serialization format of the body. When set to json, encodes the body argument,
      if needed, and automatically sets the Content-Type header accordingly. As of C(2.3)
      it is possible to override the `Content-Type` header, when set to json via the I(headers)
      option.
    required: false
    version_added: '2.0'
    version_added_collection: ansible.builtin

status_code:
    default: 200
    description:
    - A valid, numeric, HTTP status code that signifies success of the request. Can also
      be comma separated list of status codes.
    required: false

return_content:
    choices:
    - 'yes'
    - 'no'
    default: 'no'
    description:
    - Whether or not to return the body of the request as a "content" key in the dictionary
      result. If the reported Content-type is "application/json", then the JSON is additionally
      loaded into a key called C(json) in the dictionary results.
    required: false

validate_certs:
    choices:
    - 'yes'
    - 'no'
    default: 'yes'
    description:
    - If C(no), SSL certificates will not be validated.  This should only set to C(no)
      used on personally controlled sites using self-signed certificates.  Prior to 1.9.2
      the code defaulted to C(no).
    required: false
    version_added: 1.9.2
    version_added_collection: ansible.builtin

follow_redirects:
    choices:
    - all
    - safe
    - none
    default: safe
    description:
    - Whether or not the URI module should follow redirects. C(all) will follow all redirects.
      C(safe) will follow only "safe" redirects, where "safe" means that the client is
      only doing a GET or HEAD on the URI to which it is being redirected. C(none) will
      not follow any redirects. Note that C(yes) and C(no) choices are accepted for backwards
      compatibility, where C(yes) is the equivalent of C(all) and C(no) is the equivalent
      of C(safe). C(yes) and C(no) are deprecated and will be removed in some future version
      of Ansible.
    required: false

force_basic_auth:
    choices:
    - 'yes'
    - 'no'
    default: 'no'
    description:
    - The library used by the uri module only sends authentication information when a
      webservice responds to an initial request with a 401 status. Since some basic auth
      services do not properly send a 401, logins will fail. This option forces the sending
      of the Basic authentication header upon initial request.
    required: false