ansible.builtin.uri – Interacts with webservices
Note
This module is part of ansible-base and included in all Ansible installations. In most cases, you can use the short module name uri even without specifying the collections: keyword. Despite that, we recommend you use the FQCN for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.
New in version 1.1: of ansible.builtin
Synopsis
- Interacts with HTTP and HTTPS web services and supports Digest, Basic and WSSE HTTP authentication mechanisms.
 - For Windows targets, use the ansible.windows.win_uri module instead.
 
Note
This module has a corresponding action plugin.
Parameters
| Parameter | Choices/Defaults | Comments | 
|---|---|---|
|   attributes    string    added in 2.3 of ansible.builtin    |    The attributes the resulting file or directory should have.  To get supported flags look at the man page for chattr on the target system.  This string should contain the attributes in the same order as the one displayed by lsattr.  The   = operator is assumed as default, otherwise + or - operators need to be included in the string.aliases: attr  |  |
|   body    raw    |    The body of the http request/response to the web service. If   body_format is set to 'json' it will take an already formatted JSON string or convert a data structure into JSON.If   body_format is set to 'form-urlencoded' it will convert a dictionary or list of tuples into an 'application/x-www-form-urlencoded' string. (Added in v2.7)If   body_format is set to 'form-multipart' it will convert a dictionary into 'multipart/form-multipart' body. (Added in v2.10) |  |
|   body_format    string    added in 2.0 of ansible.builtin    |   
  |    The serialization format of the body. When set to   json, form-multipart, or form-urlencoded, encodes the body argument, if needed, and automatically sets the Content-Type header accordingly.As of   2.3 it is possible to override the `Content-Type` header, when set to json or form-urlencoded via the headers option.The 'Content-Type' header cannot be overridden when using   form-multipart
form-urlencoded was added in v2.7.form-multipart was added in v2.10. |  
|   client_cert    path    added in 2.4 of ansible.builtin    |    PEM formatted certificate chain file to be used for SSL client authentication.  This file can also include the key as well, and if the key is included, client_key is not required   |  |
|   client_key    path    added in 2.4 of ansible.builtin    |    PEM formatted file that contains your private key to be used for SSL client authentication.  If client_cert contains both the certificate and key, this option is not required.   |  |
|   creates    path    |    A filename, when it already exists, this step will not be run.   |  |
|   dest    path    |    A path of where to download the file to (if desired). If dest is a directory, the basename of the file on the remote server will be used.   |  |
|   follow_redirects    string    |   
  |    Whether or not the URI module should follow redirects.   all will follow all redirects. 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. none will not follow any redirects. Note that yes and no choices are accepted for backwards compatibility, where yes is the equivalent of all and no is the equivalent of safe. yes and no are deprecated and will be removed in some future version of Ansible. |  
|   force    boolean    |   
  |    If   yes do not get a cached copy.Alias   thirsty has been deprecated and will be removed in 2.13.aliases: thirsty  |  
|   force_basic_auth    boolean    |   
  |    Force the sending of the Basic authentication header upon initial request.  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.   |  
|   group    string    |    Name of the group that should own the file/directory, as would be fed to chown.   |  |
|   headers    dictionary    added in 2.1 of ansible.builtin    |    Add custom HTTP headers to a request in the format of a YAML hash. As of   2.3 supplying Content-Type here will override the header generated by supplying json or form-urlencoded for body_format. |  |
|   http_agent    string    |   Default: "ansible-httpget"   |    Header to identify as, generally appears in web server logs.   |  
|   method    string    |   Default: "GET"   |    The HTTP method of the request or response.  In more recent versions we do not restrict the method at the module level anymore but it still must be a valid method accepted by the service handling the request.   |  
|   mode    raw    |    The permissions the resulting file or directory should have.  For those used to /usr/bin/chmod remember that modes are actually octal numbers. You must either add a leading zero so that Ansible's YAML parser knows it is an octal number (like   0644 or 01777) or quote it (like '644' or '1777') so Ansible receives a string and can do its own conversion from string into number.Giving Ansible a number without following one of these rules will end up with a decimal number which will have unexpected results.  As of Ansible 1.8, the mode may be specified as a symbolic mode (for example,   u+rwx or u=rw,g=r,o=r). |  |
|   owner    string    |    Name of the user that should own the file/directory, as would be fed to chown.   |  |
|   remote_src    boolean    added in 2.7 of ansible.builtin    |   
  |    If   no, the module will search for src on originating/master machine.If   yes the module will use the src path on the remote/target machine. |  
|   removes    path    |    A filename, when it does not exist, this step will not be run.   |  |
|   return_content    boolean    |   
  |    Whether or not to return the body of the response as a "content" key in the dictionary result no matter it succeeded or failed.  Independently of this option, if the reported Content-type is "application/json", then the JSON is always loaded into a key called   json in the dictionary results. |  
|   selevel    string    |    The level part of the SELinux file context.  This is the MLS/MCS attribute, sometimes known as the   range.When set to   _default, it will use the level portion of the policy if available. |  |
|   serole    string    |    The role part of the SELinux file context.  When set to   _default, it will use the role portion of the policy if available. |  |
|   setype    string    |    The type part of the SELinux file context.  When set to   _default, it will use the type portion of the policy if available. |  |
|   seuser    string    |    The user part of the SELinux file context.  By default it uses the   system policy, where applicable.When set to   _default, it will use the user portion of the policy if available. |  |
|   src    path    added in 2.7 of ansible.builtin    |    Path to file to be submitted to the remote server.  Cannot be used with body.   |  |
|   status_code    list / elements=string    |   Default: [200]   |    A list of valid, numeric, HTTP status codes that signifies success of the request.   |  
|   timeout    integer    |   Default: 30   |    The socket level timeout in seconds   |  
|   unix_socket    string    added in 2.8 of ansible.builtin    |    Path to Unix domain socket to use for connection   |  |
|   unsafe_writes    boolean    added in 2.2 of ansible.builtin    |   
  |    Influence when to use atomic operation to prevent data corruption or inconsistent reads from the target file.  By default this module uses atomic operations to prevent data corruption or inconsistent reads from the target files, but sometimes systems are configured or just broken in ways that prevent this. One example is docker mounted files, which cannot be updated atomically from inside the container and can only be written in an unsafe manner.  This option allows Ansible to fall back to unsafe methods of updating files when atomic operations fail (however, it doesn't force Ansible to perform unsafe writes).  IMPORTANT! Unsafe writes are subject to race conditions and can lead to data corruption.   |  
|   url    string / required    |    HTTP or HTTPS URL in the form (http|https)://host.domain[:port]/path   |  |
|   url_password    string    |    A password for the module to use for Digest, Basic or WSSE authentication.  aliases: password  |  |
|   url_username    string    |    A username for the module to use for Digest, Basic or WSSE authentication.  aliases: user  |  |
|   use_proxy    boolean    |   
  |    If   no, it will not use a proxy, even if one is defined in an environment variable on the target hosts. |  
|   validate_certs    boolean    added in 1.9.2 of ansible.builtin    |   
  |    If   no, SSL certificates will not be validated.This should only set to   no used on personally controlled sites using self-signed certificates.Prior to 1.9.2 the code defaulted to   no. |  
Notes
Note
- The dependency on httplib2 was removed in Ansible 2.1.
 - The module returns all the HTTP headers in lower-case.
 - For Windows targets, use the ansible.windows.win_uri module instead.
 
See Also
See also
- ansible.builtin.get_url
 - 
The official documentation on the ansible.builtin.get_url module.
 - ansible.windows.win_uri
 - 
The official documentation on the ansible.windows.win_uri module.
 
Examples
- name: Check that you can connect (GET) to a page and it returns a status 200
  uri:
    url: http://www.example.com
- name: 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: this
  failed_when: "'AWESOME' not in this.content"
- name: Create a JIRA issue
  uri:
    url: https://your.jira.example.com/rest/api/2/issue/
    user: your_username
    password: your_pass
    method: POST
    body: "{{ lookup('file','issue.json') }}"
    force_basic_auth: yes
    status_code: 201
    body_format: json
- name: 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_format: form-urlencoded
    body:
      name: your_username
      password: your_password
      enter: Sign in
    status_code: 302
  register: login
- name: Login to a form based webpage using a list of tuples
  uri:
    url: https://your.form.based.auth.example.com/index.php
    method: POST
    body_format: form-urlencoded
    body:
    - [ name, your_username ]
    - [ password, your_password ]
    - [ enter, Sign in ]
    status_code: 302
  register: login
- name: Upload a file via multipart/form-multipart
  uri:
    url: https://httpbin.org/post
    method: POST
    body_format: form-multipart
    body:
      file1:
        filename: /bin/true
        mime_type: application/octet-stream
      file2:
        content: text based file content
        filename: fake.txt
        mime_type: text/plain
      text_form_field: value
- name: Connect to website using a previously stored cookie
  uri:
    url: https://your.form.based.auth.example.com/dashboard.php
    method: GET
    return_content: yes
    headers:
      Cookie: "{{ login.cookies_string }}"
- name: Queue build of a project in Jenkins
  uri:
    url: http://{{ jenkins.host }}/job/{{ jenkins.job }}/build?token={{ jenkins.token }}
    user: "{{ jenkins.user }}"
    password: "{{ jenkins.password }}"
    method: GET
    force_basic_auth: yes
    status_code: 201
- name: POST from contents of local file
  uri:
    url: https://httpbin.org/post
    method: POST
    src: file.json
- name: POST from contents of remote file
  uri:
    url: https://httpbin.org/post
    method: POST
    src: /path/to/my/file.json
    remote_src: yes
- name: Create workspaces in Log analytics Azure
  uri:
    url: https://www.mms.microsoft.com/Embedded/Api/ConfigDataSources/LogManagementData/Save
    method: POST
    body_format: json
    status_code: [200, 202]
    return_content: true
    headers:
      Content-Type: application/json
      x-ms-client-workspace-path: /subscriptions/{{ sub_id }}/resourcegroups/{{ res_group }}/providers/microsoft.operationalinsights/workspaces/{{ w_spaces }}
      x-ms-client-platform: ibiza
      x-ms-client-auth-token: "{{ token_az }}"
    body:
- name: Pause play until a URL is reachable from this host
  uri:
    url: "http://192.0.2.1/some/test"
    follow_redirects: none
    method: GET
  register: _result
  until: _result.status == 200
  retries: 720 # 720 * 5 seconds = 1hour (60*60/5)
  delay: 5 # Every 5 seconds
# There are issues in a supporting Python library that is discussed in
# https://github.com/ansible/ansible/issues/52705 where a proxy is defined
# but you want to bypass proxy use on CIDR masks by using no_proxy
- name: Work around a python issue that doesn't support no_proxy envvar
  uri:
    follow_redirects: none
    validate_certs: false
    timeout: 5
    url: "http://{{ ip_address }}:{{ port | default(80) }}"
  register: uri_data
  failed_when: false
  changed_when: false
  vars:
    ip_address: 192.0.2.1
  environment: |
      {
        {% for no_proxy in (lookup('env', 'no_proxy') | regex_replace('\s*,\s*', ' ') ).split() %}
          {% if no_proxy | regex_search('\/') and
                no_proxy | ipaddr('net') != '' and
                no_proxy | ipaddr('net') != false and
                ip_address | ipaddr(no_proxy) is not none and
                ip_address | ipaddr(no_proxy) != false %}
            'no_proxy': '{{ ip_address }}'
          {% elif no_proxy | regex_search(':') != '' and
                  no_proxy | regex_search(':') != false and
                  no_proxy == ip_address + ':' + (port | default(80)) %}
            'no_proxy': '{{ ip_address }}:{{ port | default(80) }}'
          {% elif no_proxy | ipaddr('host') != '' and
                  no_proxy | ipaddr('host') != false and
                  no_proxy == ip_address %}
            'no_proxy': '{{ ip_address }}'
          {% elif no_proxy | regex_search('^(\*|)\.') != '' and
                  no_proxy | regex_search('^(\*|)\.') != false and
                  no_proxy | regex_replace('\*', '') in ip_address %}
            'no_proxy': '{{ ip_address }}'
          {% endif %}
        {% endfor %}
      }
   Return Values
Common return values are documented here, the following are the fields unique to this module:
| Key | Returned | Description | 
|---|---|---|
|   content    string    |  status not in status_code or return_content is true |   The response body content.  Sample:  {}   |  
|   cookies    dictionary   added in 2.4 of ansible.builtin   |  on success |   The cookie values placed in cookie jar.  Sample:  {'SESSIONID': '[SESSIONID]'}   |  
|   cookies_string    string   added in 2.6 of ansible.builtin   |  on success |   The value for future request Cookie headers.  Sample:  SESSIONID=[SESSIONID]   |  
|   elapsed    integer    |  on success |   The number of seconds that elapsed while performing the download.  Sample:  23   |  
|   msg    string    |  always |   The HTTP message from the request.  Sample:  OK (unknown bytes)   |  
|   redirected    boolean    |  on success |   Whether the request was redirected.   |  
|   status    integer    |  always |   The HTTP status code from the request.  Sample:  200   |  
|   url    string    |  always |   The actual URL used for the request.  Sample:  https://www.ansible.com/   |  
Authors
- Romeo Theriault (@romeotheriault)
 
    © 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
    https://docs.ansible.com/ansible/2.10/collections/ansible/builtin/uri_module.html