Use official Portainer updater
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
# Watchtower is used only as a one-shot updater. It recreates the selected
|
# Portainer's official updater recreates the server container while preserving
|
||||||
# Portainer containers with their existing configuration and then removes itself.
|
# its existing Docker configuration and persistent data.
|
||||||
portainer_watchtower_image: containrrr/watchtower:latest
|
portainer_updater_image: portainer/portainer-updater:latest
|
||||||
portainer_target_tag: latest
|
portainer_target_tag: latest
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
changed_when: false
|
changed_when: false
|
||||||
failed_when: false
|
failed_when: false
|
||||||
|
|
||||||
- name: Find Docker containers
|
- name: Find and update Portainer
|
||||||
when: portainer_docker_info.rc == 0
|
when: portainer_docker_info.rc == 0
|
||||||
block:
|
block:
|
||||||
- name: List all container IDs
|
- name: List all container IDs
|
||||||
@@ -22,83 +22,50 @@
|
|||||||
register: portainer_all_containers
|
register: portainer_all_containers
|
||||||
changed_when: false
|
changed_when: false
|
||||||
|
|
||||||
- name: Inspect container images
|
- name: Inspect all container images
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- docker
|
|
||||||
- container
|
|
||||||
- inspect
|
|
||||||
- --format
|
|
||||||
- "{% raw %}{{.Config.Image}}{% endraw %}"
|
|
||||||
- "{{ item }}"
|
|
||||||
loop: "{{ portainer_all_containers.stdout_lines }}"
|
|
||||||
register: portainer_container_inspections
|
|
||||||
changed_when: false
|
|
||||||
|
|
||||||
- name: Select Portainer CE and Business Edition server containers
|
|
||||||
ansible.builtin.set_fact:
|
|
||||||
portainer_container_ids: >-
|
|
||||||
{{ portainer_container_inspections.results
|
|
||||||
| selectattr('stdout', 'match',
|
|
||||||
'^(?:[^/]+/)?portainer/(?:portainer|portainer-(?:ce|ee))(?::|@|$)')
|
|
||||||
| map(attribute='item')
|
|
||||||
| list }}
|
|
||||||
|
|
||||||
- name: Pull the latest release for each installed Portainer edition
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- docker
|
|
||||||
- image
|
|
||||||
- pull
|
|
||||||
- >-
|
|
||||||
{{ 'portainer/portainer-ee:' ~ portainer_target_tag
|
|
||||||
if 'portainer-ee' in item.stdout
|
|
||||||
else 'portainer/portainer-ce:' ~ portainer_target_tag }}
|
|
||||||
loop: "{{ portainer_container_inspections.results }}"
|
|
||||||
loop_control:
|
|
||||||
label: "{{ item.stdout }}"
|
|
||||||
register: portainer_image_pulls
|
|
||||||
changed_when: "'Downloaded newer image' in portainer_image_pulls.stdout"
|
|
||||||
when:
|
|
||||||
- item.stdout is match('^(?:[^/]+/)?portainer/(?:portainer|portainer-(?:ce|ee))(?::|@|$)')
|
|
||||||
- not ansible_check_mode
|
|
||||||
|
|
||||||
- name: Point the existing image reference at the latest release
|
|
||||||
ansible.builtin.command:
|
|
||||||
argv:
|
|
||||||
- docker
|
|
||||||
- image
|
|
||||||
- tag
|
|
||||||
- >-
|
|
||||||
{{ 'portainer/portainer-ee:' ~ portainer_target_tag
|
|
||||||
if 'portainer-ee' in item.stdout
|
|
||||||
else 'portainer/portainer-ce:' ~ portainer_target_tag }}
|
|
||||||
- "{{ item.stdout }}"
|
|
||||||
loop: "{{ portainer_container_inspections.results }}"
|
|
||||||
loop_control:
|
|
||||||
label: "{{ item.stdout }}"
|
|
||||||
changed_when: false
|
|
||||||
when:
|
|
||||||
- item.stdout is match('^(?:[^/]+/)?portainer/(?:portainer|portainer-(?:ce|ee))(?::|$)')
|
|
||||||
- not ansible_check_mode
|
|
||||||
|
|
||||||
- name: Update discovered Portainer containers
|
|
||||||
ansible.builtin.command:
|
ansible.builtin.command:
|
||||||
argv: >-
|
argv: >-
|
||||||
{{ ['docker', 'run', '--rm', '--pull', 'always',
|
{{ ['docker', 'container', 'inspect']
|
||||||
'--volume', '/var/run/docker.sock:/var/run/docker.sock',
|
+ portainer_all_containers.stdout_lines }}
|
||||||
portainer_watchtower_image, '--run-once', '--cleanup', '--no-pull',
|
register: portainer_container_inspection
|
||||||
'--include-stopped']
|
changed_when: false
|
||||||
+ portainer_container_ids }}
|
when: portainer_all_containers.stdout_lines | length > 0
|
||||||
register: portainer_watchtower
|
|
||||||
changed_when: >-
|
- name: Select installed Portainer editions
|
||||||
(portainer_watchtower.stdout ~ portainer_watchtower.stderr)
|
ansible.builtin.set_fact:
|
||||||
is regex('Updated=[1-9][0-9]*')
|
portainer_installed_images: >-
|
||||||
|
{{ (portainer_container_inspection.stdout | default('[]') | from_json)
|
||||||
|
| map(attribute='Config.Image')
|
||||||
|
| select('match',
|
||||||
|
'^(?:[^/]+/)?portainer/(?:portainer|portainer-(?:ce|ee))(?::|@|$)')
|
||||||
|
| unique
|
||||||
|
| list }}
|
||||||
|
|
||||||
|
- name: Update Portainer to the newest release
|
||||||
|
ansible.builtin.command:
|
||||||
|
argv:
|
||||||
|
- docker
|
||||||
|
- run
|
||||||
|
- --rm
|
||||||
|
- --pull
|
||||||
|
- always
|
||||||
|
- --volume
|
||||||
|
- /var/run/docker.sock:/var/run/docker.sock
|
||||||
|
- "{{ portainer_updater_image }}"
|
||||||
|
- >-
|
||||||
|
--image={{ 'portainer/portainer-ee:' ~ portainer_target_tag
|
||||||
|
if 'portainer-ee' in item
|
||||||
|
else 'portainer/portainer-ce:' ~ portainer_target_tag }}
|
||||||
|
loop: "{{ portainer_installed_images }}"
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item }}"
|
||||||
|
register: portainer_update
|
||||||
|
changed_when: true
|
||||||
when:
|
when:
|
||||||
- portainer_container_ids | length > 0
|
- portainer_installed_images | length > 0
|
||||||
- not ansible_check_mode
|
- not ansible_check_mode
|
||||||
|
|
||||||
- name: Report when no Portainer server is installed
|
- name: Report when no Portainer server is installed
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: No Portainer CE or Business Edition server container found; skipping.
|
msg: No Portainer CE or Business Edition server container found; skipping.
|
||||||
when: portainer_container_ids | length == 0
|
when: portainer_installed_images | length == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user