2 # Copyright (c) 2017 Intel Corporation
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
20 module: fetch_url_and_verify
21 short_description: Fetch image and verify against a SHA256SUM URL
23 - Download a URL and check it against a remote SHA256SUMS file
26 image_dest: Image filename
27 sha256_url: SHA256SUMS URL
28 dest: python file mode (w, wb, a, ab)
29 retries: fetch retries
34 module = AnsibleModule(
36 'url': {'required': True, 'type': 'str'},
37 'sha256url': {'required': True, 'type': 'str'},
38 'dest': {'required': True, 'type': 'path'},
39 'retries': {'required': False, 'type': 'int', 'default': 3},
42 params = module.params
45 sha256url = params['sha256url']
46 retries = params['retries']
48 image_dir, image_filename = os.path.split(dest)
49 rc, stdout, stderr = module.run_command(['curl', '-sS', sha256url])
50 if rc == 0 and stdout:
52 (l for l in stdout.splitlines() if image_filename in l), "")
55 msg="Unable to find SHA256SUMS line for file {}".format(
58 module.run_command(['sha256sum', '-c'], data=sha256line, cwd=image_dir)[0]
60 sha256sum = sha256line.split()[0]
61 module.exit_json(changed=False, dest=dest, url=url,
64 for _ in range(retries):
65 curl_rc, stdout, stderr = module.run_command(
66 ['curl', '-sS', '-o', dest, url], cwd=image_dir)
68 sha256_rc, stdout, stderr = module.run_command(['sha256sum', '-c'],
72 module.exit_json(changed=True)
74 module.fail_json(msg="Unable to download {}".format(url), stdout=stdout,
78 # <<INCLUDE_ANSIBLE_MODULE_COMMON>>
79 from ansible.module_utils.basic import *
81 if __name__ == '__main__':