From 08a0541c7593813be13671b41ba7e49ca50cd6c7 Mon Sep 17 00:00:00 2001 From: Payback159 Date: Fri, 11 Oct 2024 15:24:58 +0200 Subject: [PATCH] use include_tasks to avoid empty json error for Set node_images (crictl) task --- roles/download/tasks/main.yml | 39 ++++++------------- roles/download/tasks/set_node_facts.yml | 7 ++++ .../download/tasks/set_node_facts_crictl.yml | 9 +++++ 3 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 roles/download/tasks/set_node_facts.yml create mode 100644 roles/download/tasks/set_node_facts_crictl.yml diff --git a/roles/download/tasks/main.yml b/roles/download/tasks/main.yml index be5169bf5f0..7eeb83bc00b 100644 --- a/roles/download/tasks/main.yml +++ b/roles/download/tasks/main.yml @@ -16,35 +16,20 @@ - download - upload -- name: Download | Get list of images on the node +- name: Download | Generate a list of information about the images on a node + command: "{{ image_info_command }}" + register: node_images_raw + changed_when: false + check_mode: false + when: + - not (download_always_pull or skip_downloads) | default(false) + +- name: Download | Set node_images + include_tasks: "{{ include_file }}" + vars: + include_file: "set_node_facts{% if image_command_tool == 'crictl' %}_crictl{% else %}{% endif %}.yml" when: - not (download_always_pull or skip_downloads) | default(false) - block: - - name: Download | Generate a list of information about the images on a node - command: "{{ image_info_command }}" - register: node_images_raw - changed_when: false - check_mode: false - - name: Download | Set node_images (crictl) - set_fact: - node_images: >- - {{ - (node_images | default([])) + - [{'Digest': (item.repoDigests[0] | split('@'))[1], 'Repository': ((item.repoTags[0] | split(':'))[0] | regex_replace('^docker\.io/(library/)?', '')), 'Tag': (item.repoTags[0] | split(':'))[1]}] - }} - with_items: "{{ (node_images_raw.stdout | from_json).images }}" - when: - - image_command_tool == 'crictl' - - node_images_raw.stdout != '' - # In case of nerdctl or docker, the output is already in the expected format - # {"Digest":"sha256:847423221ed040798e47df36450edce5581ed642cd087ccb210532764da38b23","Repository":"quay.io/coreos/etcd","Tag":"v3.5.12"} - # {"Digest":"sha256:34fc87c4a60c0b3ba3b3608871f4494de8072c02808a4151953068f2d7c87743","Repository":"flannel/flannel","Tag":"latest"} - - name: Download | Set node_images (nerdctl, docker) - set_fact: - node_images: "{{ node_images_raw.stdout_lines | map('from_json') | list }}" - when: - - image_command_tool in ['nerdctl', 'docker'] - - when node_images_raw.stdout_lines != '' - name: Show node images debug: diff --git a/roles/download/tasks/set_node_facts.yml b/roles/download/tasks/set_node_facts.yml new file mode 100644 index 00000000000..7afbacebbe3 --- /dev/null +++ b/roles/download/tasks/set_node_facts.yml @@ -0,0 +1,7 @@ +--- +# In case of nerdctl or docker, the output is already in the expected format +# {"Digest":"sha256:847423221ed040798e47df36450edce5581ed642cd087ccb210532764da38b23","Repository":"quay.io/coreos/etcd","Tag":"v3.5.12"} +# {"Digest":"sha256:34fc87c4a60c0b3ba3b3608871f4494de8072c02808a4151953068f2d7c87743","Repository":"flannel/flannel","Tag":"latest"} +- name: Download | Set node_images (nerdctl, docker) + set_fact: + node_images: "{{ node_images_raw.stdout_lines | map('from_json') | list }}" diff --git a/roles/download/tasks/set_node_facts_crictl.yml b/roles/download/tasks/set_node_facts_crictl.yml new file mode 100644 index 00000000000..dc4218e2bdf --- /dev/null +++ b/roles/download/tasks/set_node_facts_crictl.yml @@ -0,0 +1,9 @@ +--- +- name: Download | Set node_images (crictl) + set_fact: + node_images: >- + "{{ + (node_images | default([])) + + [{'Digest': (item.repoDigests[0] | split('@'))[1], 'Repository': ((item.repoTags[0] | split(':'))[0] | regex_replace('^docker\.io/(library/)?', '')), 'Tag': (item.repoTags[0] | split(':'))[1]}] + }}" + with_items: "{{ (node_images_raw.stdout | from_json).images }}"