0

I am using below code snippet where image details would be printed:

- set_fact:
    image_name: "{{ load.results|map(attribute='stdout_lines')|list }}"

- debug:
    var: image_name

Output:

TASK [set_fact] ***************************************************************************************************************************************************************************
ok: [xx.xx.xx.xx]

TASK [debug] ******************************************************************************************************************************************************************************
ok: [xx.xx.xx.xx] => {
  "image_name": [
    [
        "Loaded image(s): localhost/cim:v1.5"
    ],
    [
        "Loaded image(s): localhost/cim:v1.8"
    ]
  ]
}

Is there a way I can store the image name and tag in two separate variables under set_fact itself or in any other form so I can reuse those 2 variables for the next task?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
sanjibn
  • 11
  • 2

1 Answers1

1

You can use a regex_findall filter in order to achieve this.

The regex used here is (\S*):(\S+). if needed, more explanation on it can be found here

Given the playbook:

- hosts: all
  gather_facts: no
  vars:
    load:
      results:
        - stdout_lines:
          - "Loaded image(s): localhost/cim:v1.5"
        - stdout_lines:
          - "Loaded image(s): localhost/cim:v1.8"
      
  tasks:
    - set_fact:
        images: "{{ images | default([]) + item | regex_findall('(\\S*):(\\S+)') }}"
      loop: "{{ load.results | map(attribute='stdout_lines') | flatten }}"
  
    - debug:
        msg: "This image repository is `{{ item.0 }}` and its tag is `{{ item.1 }}`"
      loop: "{{ images }}"

This yields the recap:

PLAY [all] *********************************************************************************************************

TASK [set_fact] ****************************************************************************************************
ok: [localhost] => (item=Loaded image(s): localhost/cim:v1.5)
ok: [localhost] => (item=Loaded image(s): localhost/cim:v1.8)

TASK [debug] *******************************************************************************************************
ok: [localhost] => (item=['localhost/cim', 'v1.5']) => {
    "msg": "This image repository is `localhost/cim` and its tag is `v1.5`"
}
ok: [localhost] => (item=['localhost/cim', 'v1.8']) => {
    "msg": "This image repository is `localhost/cim` and its tag is `v1.8`"
}

PLAY RECAP *********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Thank you for you quick support..Now i am able to fetch the corresponding image id TASK [debug] ********************* ok: [xx.xx.xx.xx] => { "msg": { "changed": true, "msg": "All items completed", "results": [ { "stdout": "v1.5 124715a6d46f", "stdout_lines": [ "v1.5 124715a6d46f" ] }, How can i fetch get only the image id corresponding to tag? – sanjibn Sep 01 '20 at 06:57
  • i was trying this but getting error: - set_fact: image_id: "{{ results.stdout | regex_findall('(^v[0-9].* )([0-9a-z]*)') }}" TASK [set_fact] ************** fatal: [10.10.6.83]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ results.stdout | regex_findall('(^v[0-9].* )([0-9a-z]*)') }}): expected string or buffer"} – sanjibn Sep 01 '20 at 06:57
  • Most likely because your `results` is coming from a loop so you will have a list of `stdout` and not a simple string. – β.εηοιτ.βε Sep 01 '20 at 09:40
  • ok, now i am performing this: - set_fact: image_id: "{{ image_id | default([]) + item | regex_findall('(\\S*[a-z0-9])[[:space:]](\\S*)') }}" loop: "{{ imagestatus.results | map(attribute='stdout_lines') | flatten }}" - debug: msg: "image tag is `{{ item.0 }}`" loop: "{{ image_id }}" But not getting image_id and debug o/p TASK [set_fact] ***************** ok: [localhost] => (item=v1.5 124715a6d46f) => { "ansible_facts": { "image_id": [] }, "ansible_loop_var": "item", "changed": false, "item": "v1.5 124715a6d46f" } – sanjibn Sep 01 '20 at 22:11