1+ ---
2+ - name : Gather info on a current nginx image
3+ containers.podman.podman_image_info :
4+ name : " {{ files.name }}"
5+ register : local_image_tags
6+ with_items : " {{ podman.images.remotes }}"
7+ loop_control :
8+ loop_var : files
9+
10+ - name : Store current nginx image value in a variable
11+ set_fact :
12+ nginx_latest_image_tag : " {{ local_image_tags.results[0].images[0].RepoTags[0] }}"
13+
14+ - name : Pull the latest Nginx image
15+ shell : " podman pull {{ nginx.latest_image }}"
16+ register : nginx_latest_image_result
17+ when : " nginx.latest_image not in nginx_latest_image_tag"
18+
19+ - name : Get current Nginx container info
20+ command : podman ps --format "{{ '{{.Names}}' }}" --filter ancestor="{{ nginx_latest_image_tag }}"
21+ register : nginx_container_info
22+ when : nginx_latest_image_result.changed # Only execute if the image was updated
23+
24+ - name : Extract current container name
25+ set_fact :
26+ container_name : " {{ nginx_container_info.stdout_lines | first }}"
27+ when : nginx_latest_image_result.changed
28+
29+ - name : Stop and remove the current container
30+ containers.podman.podman_container :
31+ name : " {{ container_name }}"
32+ state : absent
33+ ignore_errors : true # Ignore errors if the container doesn't exist
34+
35+ - name : Start the latest Nginx container
36+ containers.podman.podman_container :
37+ name : " {{ container_name }}"
38+ image : " {{ nginx.latest_image }}"
39+ state : started
40+ when : nginx_latest_image_result.changed
41+ notify :
42+ - restart nginx
0 commit comments