3
0
forked from jakub/ansible

Add Ansible playbook for updating Portainer Agent on VM via Proxmox

This commit is contained in:
martin.fencl
2025-11-28 18:22:29 +01:00
parent 6ccbb97fbb
commit f93373b1c8
2 changed files with 128 additions and 4 deletions

View File

@@ -0,0 +1,118 @@
# update_portainer_agent.yml
- name: Update Portainer Agent on VM via Proxmox
hosts: proxmox
gather_facts: false
become: true
become_user: root
become_method: sudo
vars:
# --- Connection to VM (provided by Semaphore env vars) ---
vm_ip: "{{ lookup('env', 'VM_IP') }}"
vm_user: "{{ lookup('env', 'VM_USER') }}"
vm_pass: "{{ lookup('env', 'VM_PASS') }}"
use_sudo: false
# --- Debug mode (controlled via Semaphore variable) ---
DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}"
RETRIES: "{{ lookup('env', 'RETRIES') | default(25) | int }}"
# --- Portainer Agent specifics ---
portainer_agent_image: "portainer/agent:latest"
portainer_agent_container: "portainer_agent"
portainer_agent_port: 9001
# Docker command prefix (consistent behavior and quiet hints)
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
# Commands to run on the target VM (quiet outputs)
portainer_commands:
- "{{ docker_prefix }} pull -q {{ portainer_agent_image }} >/dev/null"
- "{{ docker_prefix }} stop {{ portainer_agent_container }} >/dev/null 2>&1 || true"
- "{{ docker_prefix }} rm {{ portainer_agent_container }} >/dev/null 2>&1 || true"
- >
{{ docker_prefix }} run -d
--name {{ portainer_agent_container }}
--restart=always
-p {{ portainer_agent_port }}:9001
-v /var/run/docker.sock:/var/run/docker.sock
-v /var/lib/docker/volumes:/var/lib/docker/volumes
{{ portainer_agent_image }} >/dev/null
tasks:
- name: Ensure sshpass is installed (for password-based SSH) # English comments
ansible.builtin.apt:
name: sshpass
state: present
update_cache: yes
- name: Run Portainer Agent update commands on VM (via SSH) # run all commands via sshpass
ansible.builtin.command:
argv:
- sshpass
- -e
- ssh
- -o
- StrictHostKeyChecking=no
- -o
- ConnectTimeout=15
- "{{ vm_user }}@{{ vm_ip }}"
- bash
- -lc
- "{{ ('sudo ' if use_sudo else '') + item }}"
environment:
SSHPASS: "{{ vm_pass }}"
loop: "{{ portainer_commands }}"
loop_control:
index_var: idx # capture loop index
label: "cmd-{{ idx }}" # avoid printing full command in (item=...) line
register: portainer_cmds
changed_when: false
no_log: "{{ DEBUG == 0 }}" # hide outputs and env when not debugging
- name: Show outputs for each Portainer command
ansible.builtin.debug:
msg: |
CMD: {{ item.item }}
RC: {{ item.rc }}
STDOUT:
{{ (item.stdout | default('')).strip() }}
STDERR:
{{ (item.stderr | default('')).strip() }}
loop: "{{ portainer_cmds.results }}"
when: DEBUG == 1
- name: Fail play if any Portainer command failed
ansible.builtin.assert:
that: "item.rc == 0"
fail_msg: "Portainer Agent update failed on VM: {{ item.item }} (rc={{ item.rc }})"
success_msg: "All Portainer Agent update commands succeeded."
loop: "{{ portainer_cmds.results }}"
loop_control:
index_var: idx
label: "cmd-{{ idx }}"
# -------------------------
# Readiness check (TCP port)
# -------------------------
- name: Portainer Agent | Wait for TCP port to be open from controller
ansible.builtin.wait_for:
host: "{{ vm_ip }}"
port: "{{ portainer_agent_port }}"
delay: 2 # initial delay before first check
timeout: "{{ RETRIES * 2 }}" # total timeout in seconds
state: started
register: portainer_wait
delegate_to: localhost
run_once: true
changed_when: false
- name: Portainer Agent | Print concise summary
ansible.builtin.debug:
msg: >-
Portainer Agent TCP {{ vm_ip }}:{{ portainer_agent_port }}
reachable={{ (portainer_wait is defined) and (not portainer_wait.failed | default(false)) }}
elapsed={{ portainer_wait.elapsed | default('n/a') }}s
when: DEBUG == 1

View File

@@ -20,7 +20,8 @@
# --- Kafka / Redpanda Console specifics ---
kafka_project: "kafka"
kafka_compose_file: "/data/compose/kafka/docker-compose.yml"
# Adjusted to match your actual compose file location
kafka_compose_file: "/data/compose/docker-compose.yml"
kafka_services:
- broker3
@@ -28,7 +29,7 @@
redpanda_console_port: 8084
# Controller-side URL (default to direct VM IP/port)
# Controller-side URL (default to direct VM IP/port or external URL)
redpanda_console_url: "{{ lookup('env', 'REDPANDA_CONSOLE_URL') | default('http://192.168.69.254:8084/overview', true) }}"
redpanda_retries: "{{ RETRIES }}"
@@ -38,9 +39,14 @@
docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker"
# Commands to run on the target VM (quiet outputs)
# Uses the compose stack for broker3 + kafka-ui
# 1) Pull latest images for broker3 + kafka-ui
# 2) Stop any running containers with these names (legacy or compose-managed)
# 3) Remove any containers with these names to avoid name conflicts
# 4) Recreate services via docker compose
kafka_commands:
- "{{ docker_prefix }} compose -p {{ kafka_project }} -f {{ kafka_compose_file }} pull {{ kafka_services | join(' ') }} >/dev/null"
- "{{ docker_prefix }} stop {{ kafka_services | join(' ') }} >/dev/null 2>&1 || true"
- "{{ docker_prefix }} rm -f {{ kafka_services | join(' ') }} >/dev/null 2>&1 || true"
- "{{ docker_prefix }} compose -p {{ kafka_project }} -f {{ kafka_compose_file }} up -d --no-deps --force-recreate {{ kafka_services | join(' ') }} >/dev/null"
tasks:
@@ -105,7 +111,7 @@
url: "{{ redpanda_console_url }}"
method: GET
return_content: true
validate_certs: false # plain HTTP on 192.168.69.254
validate_certs: false # plain HTTP on 192.168.69.254 (or as configured)
status_code: 200
register: redpanda_controller
delegate_to: localhost