From 77f3e8451cb8508c623b0efeb12dfa1245f8c44b Mon Sep 17 00:00:00 2001 From: "Jon \"The Nice Guy\" Spriggs" Date: Mon, 20 May 2019 21:18:48 +0100 Subject: [PATCH] Update uri.py (#56395) 1. Note that uri doesn't honor the no_proxy environment variable (due to https://github.com/ansible/ansible/issues/52705), and suggest a work around. 2. Added an example showing a test waiting for a URL to become available (using the `until:`, `retries:` and `delay:` settings) - based on https://gist.github.com/mikeifomin/67e233cd461331de16707ef59a07e372#gistcomment-2718587 Co-Authored-By: Felix Fontein --- lib/ansible/modules/net_tools/basics/uri.py | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lib/ansible/modules/net_tools/basics/uri.py b/lib/ansible/modules/net_tools/basics/uri.py index 455d7729c92..f1c0c747b63 100644 --- a/lib/ansible/modules/net_tools/basics/uri.py +++ b/lib/ansible/modules/net_tools/basics/uri.py @@ -259,6 +259,55 @@ EXAMPLES = r''' method: POST src: /path/to/my/file.json remote_src: yes + +- 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 = r'''