forked from jakub/ansible
Refactor Semaphore update process to use a single compose command and simplify task structure
This commit is contained in:
@@ -21,18 +21,10 @@
|
|||||||
semaphore_project: "semaphore"
|
semaphore_project: "semaphore"
|
||||||
semaphore_compose_file: "/data/compose/semaphore/docker-compose.yml"
|
semaphore_compose_file: "/data/compose/semaphore/docker-compose.yml"
|
||||||
semaphore_service: "semaphore"
|
semaphore_service: "semaphore"
|
||||||
semaphore_image: "semaphoreui/semaphore:latest"
|
|
||||||
|
|
||||||
# Docker command prefix (consistent behavior and quiet hints)
|
# Docker command prefix (consistent behavior and quiet hints)
|
||||||
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
|
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
|
||||||
|
|
||||||
# Commands to run on the target VM (quiet outputs)
|
|
||||||
# NOTE: This will likely kill the current Semaphore job when the container restarts.
|
|
||||||
semaphore_commands:
|
|
||||||
- "{{ docker_prefix }} pull -q {{ semaphore_image }} >/dev/null"
|
|
||||||
- "{{ docker_prefix }} compose -p {{ semaphore_project }} -f {{ semaphore_compose_file }} pull {{ semaphore_service }} >/dev/null"
|
|
||||||
- "{{ docker_prefix }} compose -p {{ semaphore_project }} -f {{ semaphore_compose_file }} up -d --no-deps --force-recreate {{ semaphore_service }} >/dev/null"
|
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
||||||
ansible.builtin.apt:
|
ansible.builtin.apt:
|
||||||
@@ -40,7 +32,7 @@
|
|||||||
state: present
|
state: present
|
||||||
update_cache: yes
|
update_cache: yes
|
||||||
|
|
||||||
- name: Run Semaphore update commands on VM (via SSH) # use SSHPASS env, hide item value
|
- name: Run Semaphore self-update on VM (single compose command) # last thing this job does
|
||||||
ansible.builtin.command:
|
ansible.builtin.command:
|
||||||
argv:
|
argv:
|
||||||
- sshpass
|
- sshpass
|
||||||
@@ -53,35 +45,22 @@
|
|||||||
- "{{ vm_user }}@{{ vm_ip }}"
|
- "{{ vm_user }}@{{ vm_ip }}"
|
||||||
- bash
|
- bash
|
||||||
- -lc
|
- -lc
|
||||||
- "{{ ('sudo ' if use_sudo else '') + item }}"
|
- >
|
||||||
|
{{ ('sudo ' if use_sudo else '') }}
|
||||||
|
{{ docker_prefix }}
|
||||||
|
compose -p {{ semaphore_project }}
|
||||||
|
-f {{ semaphore_compose_file }}
|
||||||
|
up -d --no-deps --force-recreate --pull always {{ semaphore_service }} >/dev/null
|
||||||
environment:
|
environment:
|
||||||
SSHPASS: "{{ vm_pass }}" # supply password via environment
|
SSHPASS: "{{ vm_pass }}" # supply password via environment
|
||||||
loop: "{{ semaphore_commands }}"
|
register: semaphore_update
|
||||||
loop_control:
|
|
||||||
index_var: idx # capture loop index
|
|
||||||
label: "cmd-{{ idx }}" # avoid printing full command in (item=...) line
|
|
||||||
register: semaphore_cmds
|
|
||||||
changed_when: false
|
changed_when: false
|
||||||
no_log: "{{ DEBUG == 0 }}" # hide outputs and env when not debugging
|
no_log: "{{ DEBUG == 0 }}"
|
||||||
|
|
||||||
- name: Show outputs for each Semaphore command
|
- name: Show result of Semaphore self-update (debug)
|
||||||
ansible.builtin.debug:
|
ansible.builtin.debug:
|
||||||
msg: |
|
msg: |
|
||||||
CMD: {{ item.item }}
|
RC: {{ semaphore_update.rc }}
|
||||||
RC: {{ item.rc }}
|
STDOUT: {{ (semaphore_update.stdout | default('')).strip() }}
|
||||||
STDOUT:
|
STDERR: {{ (semaphore_update.stderr | default('')).strip() }}
|
||||||
{{ (item.stdout | default('')).strip() }}
|
|
||||||
STDERR:
|
|
||||||
{{ (item.stderr | default('')).strip() }}
|
|
||||||
loop: "{{ semaphore_cmds.results }}"
|
|
||||||
when: DEBUG == 1
|
when: DEBUG == 1
|
||||||
|
|
||||||
- name: Fail play if any Semaphore command failed # also hide item label
|
|
||||||
ansible.builtin.assert:
|
|
||||||
that: "item.rc == 0"
|
|
||||||
fail_msg: "Semaphore update failed on VM: {{ item.item }} (rc={{ item.rc }})"
|
|
||||||
success_msg: "All Semaphore update commands (pull/compose) succeeded."
|
|
||||||
loop: "{{ semaphore_cmds.results }}"
|
|
||||||
loop_control:
|
|
||||||
index_var: idx
|
|
||||||
label: "cmd-{{ idx }}"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user