Skip to content

Commit 07f027a

Browse files
SET-626 Olympus - Ensure Ansible updates Nginx images
1 parent 804420c commit 07f027a

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

roles/nginx/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ nginx_https_ssl_home: "/home/nginx/ssl"
1010
nginx_https_ssl_cert: "nginx.crt"
1111
nginx_https_ssl_private_key: "nginx.pem"
1212
nginx_https_ssl_csr: "nginx.csr"
13+
nginx_latest_image: "nginx:latest"
1314
nginx_apps:
1415
- { name: 'prbz_view', url: "{{ prbz_overview_url | default('/prbz-overview/') }}", logo: 'img/pr-bz-overview-logo.png' }
1516
- { name: 'jenkins', url: "https://{{ ansible_nodename }}/jenkins", logo: 'img/jenkins-logo.png' }

roles/nginx/tasks/nginx_update.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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: "{{ item.images[0].RepoTags[0] }}"
13+
when: "'nginx' in item.images[0].RepoTags[0]"
14+
loop: "{{ local_image_tags.results }}"
15+
16+
- name: Pull the latest Nginx image
17+
shell: "podman pull {{ nginx.latest_image }}"
18+
register: nginx_latest_image_result
19+
when: "nginx.latest_image not in nginx_latest_image_tag"
20+
21+
- name: Get current Nginx container info
22+
command: podman ps --format "{{ '{{.Names}}' }}" --filter ancestor="{{ nginx_latest_image_tag }}"
23+
register: nginx_container_info
24+
when: nginx_latest_image_result.changed # Only execute if the image was updated
25+
26+
- name: Extract current container name
27+
set_fact:
28+
container_name: "{{ nginx_container_info.stdout_lines | first }}"
29+
when: nginx_latest_image_result.changed
30+
31+
- name: Stop and remove the current container
32+
containers.podman.podman_container:
33+
name: "{{ container_name }}"
34+
state: absent
35+
ignore_errors: true # Ignore errors if the container doesn't exist
36+
37+
- name: Start the latest Nginx container
38+
containers.podman.podman_container:
39+
name: "{{ container_name }}"
40+
image: "{{ nginx.latest_image }}"
41+
state: started
42+
when: nginx_latest_image_result.changed
43+
notify:
44+
- restart nginx

roles/nginx/vars/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ nginx:
77
home: "{{ nginx_home }}"
88
volume: "{{ nginx_volume }}"
99
docroot: "{{ nginx_http_docroot }}"
10+
latest_image: "{{ nginx_latest_image }}"
1011
https:
1112
ssl:
1213
home: "{{ nginx_https_ssl_home }}"

roles/podman/tasks/images.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
loop_control:
3333
loop_var: files
3434

35+
- name: "Update nginx image if required"
36+
ansible.builtin.include_role:
37+
name: nginx
38+
tasks_from: nginx_update.yml
39+
3540
- name: "Ensure local images are successfully build"
3641
containers.podman.podman_image:
3742
name: "{{ files.tag }}"

0 commit comments

Comments
 (0)