Last active
April 24, 2022 17:50
-
-
Save h3po/495a51a58d22db30f4295d03dcbafd4a to your computer and use it in GitHub Desktop.
ansible tasks for getting the url and checksum of a debian cloud image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: example playbook for downloading the latest debian cloud vm image | |
hosts: localhost | |
vars: | |
debian_cloudimage_repo_subdir: bullseye/daily | |
debian_cloudimage_type: genericcloud-amd64 | |
debian_cloudimage_format: qcow2 | |
debian_cloudimage_repo_url: "https://cloud.debian.org/images/cloud/{{ debian_cloudimage_repo_subdir }}/" | |
debian_cloudimage_release: latest | |
tasks: | |
- name: get debian cloud image versions | |
ansible.builtin.uri: | |
url: "{{ debian_cloudimage_repo_url }}" | |
return_content: true | |
register: debian_cloudimage_versions_page | |
when: debian_cloudimage_release == "latest" | |
- name: parse latest debian cloud image version | |
ansible.builtin.set_fact: | |
debian_cloudimage_release: >- | |
{{ debian_cloudimage_versions_page.content.split('</tr>') | | |
map('regex_search', '<a href="(\d{8}-\d+)/">', '\1') | | |
select() | map('first') | list | last }} | |
when: debian_cloudimage_release == "latest" | |
- name: get debian cloud image urls | |
ansible.builtin.uri: | |
url: "{{ debian_cloudimage_repo_url }}{{ debian_cloudimage_release }}/" | |
return_content: true | |
register: debian_cloudimage_images_page | |
- name: parse debian cloud image filename | |
ansible.builtin.set_fact: | |
debian_cloudimage_filename: >- | |
{{ debian_cloudimage_images_page.content.split('\n') | | |
map('regex_search', '<a href="([^\"]*-%s-(daily-)?%s.%s)">' % | |
(debian_cloudimage_type, debian_cloudimage_release, debian_cloudimage_format), '\1') | |
| select() | first | first }} | |
- name: construct debian cloud image url | |
ansible.builtin.set_fact: | |
debian_cloudimage_url: >- | |
{{ debian_cloudimage_repo_url }}{{ debian_cloudimage_release }}/{{ debian_cloudimage_filename }} | |
- name: get debian cloud image checksums | |
ansible.builtin.uri: | |
url: "{{ debian_cloudimage_repo_url }}{{ debian_cloudimage_release }}/SHA512SUMS" | |
return_content: true | |
register: debian_cloudimage_checksums | |
delegate_to: localhost | |
- name: parse debian cloud image checksum | |
ansible.builtin.set_fact: | |
debian_cloudimage_checksum: >- | |
{{ debian_cloudimage_checksums.content.split('\n') | map('regex_search', '(\w+) %s' % debian_cloudimage_filename, '\1') | select() | first }} | |
- name: show url and checksum | |
ansible.builtin.debug: | |
msg: | |
debian_cloudimage_url: "{{ debian_cloudimage_url }}" | |
debian_cloudimage_checksum: "{{ debian_cloudimage_checksum }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment