Add check_mode to get_url (#20532)
* Add check_mode to get_url that does a HEAD request to make sure the URL exists, but doesn't write the real file * Add info about new --check behavior to docs. Add tests for the new behavior. Populate res_args with the info the tests are looking for. * Add trailing comma * Change nonexistent test URL to http://{{httpbin_host}/DOESNOTEXIST. Fix spacing while I'm at it * Further spacing cleanup * State that this functionality is in Ansible 2.4+
This commit is contained in:
parent
ca7616b4a1
commit
3f321e7591
2 changed files with 37 additions and 2 deletions
|
@ -40,6 +40,8 @@ description:
|
||||||
or by using the use_proxy option.
|
or by using the use_proxy option.
|
||||||
- HTTP redirects can redirect from HTTP to HTTPS so you should be sure that
|
- HTTP redirects can redirect from HTTP to HTTPS so you should be sure that
|
||||||
your proxy environment for both protocols is correct.
|
your proxy environment for both protocols is correct.
|
||||||
|
- From Ansible 2.4 when run with C(--check), it will do a HEAD request to validate the URL but
|
||||||
|
will not download the entire file or verify it against hashes.
|
||||||
version_added: "0.6"
|
version_added: "0.6"
|
||||||
options:
|
options:
|
||||||
url:
|
url:
|
||||||
|
@ -241,8 +243,12 @@ def url_get(module, url, dest, use_proxy, last_mod_time, force, timeout=10, head
|
||||||
|
|
||||||
Return (tempfile, info about the request)
|
Return (tempfile, info about the request)
|
||||||
"""
|
"""
|
||||||
|
if module.check_mode:
|
||||||
|
method='HEAD'
|
||||||
|
else:
|
||||||
|
method='GET'
|
||||||
|
|
||||||
rsp, info = fetch_url(module, url, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, headers=headers)
|
rsp, info = fetch_url(module, url, use_proxy=use_proxy, force=force, last_mod_time=last_mod_time, timeout=timeout, headers=headers, method=method)
|
||||||
|
|
||||||
if info['status'] == 304:
|
if info['status'] == 304:
|
||||||
module.exit_json(url=url, dest=dest, changed=False, msg=info.get('msg', ''))
|
module.exit_json(url=url, dest=dest, changed=False, msg=info.get('msg', ''))
|
||||||
|
@ -318,7 +324,8 @@ def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
# not checking because of daisy chain to file module
|
# not checking because of daisy chain to file module
|
||||||
argument_spec = argument_spec,
|
argument_spec = argument_spec,
|
||||||
add_file_common_args=True
|
add_file_common_args=True,
|
||||||
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
url = module.params['url']
|
url = module.params['url']
|
||||||
|
@ -412,6 +419,12 @@ def main():
|
||||||
checksum_src = None
|
checksum_src = None
|
||||||
checksum_dest = None
|
checksum_dest = None
|
||||||
|
|
||||||
|
# If the remote URL exists, we're done with check mode
|
||||||
|
if module.check_mode:
|
||||||
|
os.remove(tmpsrc)
|
||||||
|
res_args = dict( url = url, dest = dest, src = tmpsrc, changed = True, msg = info.get('msg', ''))
|
||||||
|
module.exit_json(**res_args)
|
||||||
|
|
||||||
# raise an error if there is no tmpsrc file
|
# raise an error if there is no tmpsrc file
|
||||||
if not os.path.exists(tmpsrc):
|
if not os.path.exists(tmpsrc):
|
||||||
os.remove(tmpsrc)
|
os.remove(tmpsrc)
|
||||||
|
|
|
@ -65,6 +65,28 @@
|
||||||
that:
|
that:
|
||||||
- result.failed
|
- result.failed
|
||||||
|
|
||||||
|
- name: test HTTP HEAD request for file in check mode
|
||||||
|
get_url: url="http://{{ httpbin_host }}/get" dest={{ output_dir }}/get_url_check.txt force=yes
|
||||||
|
check_mode: True
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- name: assert that the HEAD request was successful in check mode
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result.changed
|
||||||
|
- '"OK" in result.msg'
|
||||||
|
|
||||||
|
- name: test HTTP HEAD for nonexistent URL in check mode
|
||||||
|
get_url: url="http://{{ httpbin_host }}/DOESNOTEXIST" dest={{ output_dir }}/shouldnotexist.html force=yes
|
||||||
|
check_mode: True
|
||||||
|
register: result
|
||||||
|
ignore_errors: True
|
||||||
|
|
||||||
|
- name: assert that HEAD request for nonexistent URL failed
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- result.failed
|
||||||
|
|
||||||
- name: test https fetch
|
- name: test https fetch
|
||||||
get_url: url="https://{{ httpbin_host }}/get" dest={{output_dir}}/get_url.txt force=yes
|
get_url: url="https://{{ httpbin_host }}/get" dest={{output_dir}}/get_url.txt force=yes
|
||||||
register: result
|
register: result
|
||||||
|
|
Loading…
Reference in a new issue