forked from jakub/ansible
Add playbook to update Redpanda Console on VM via Proxmox
This commit is contained in:
183
update_redpanda_console.yml
Normal file
183
update_redpanda_console.yml
Normal file
@@ -0,0 +1,183 @@
|
||||
# update_redpanda_console.yml
|
||||
|
||||
- name: Update Redpanda Console 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 }}"
|
||||
|
||||
# --- Redpanda Console specifics ---
|
||||
redpanda_console_image: "docker.redpanda.com/redpandadata/console:latest"
|
||||
redpanda_console_container: "kafka-ui"
|
||||
redpanda_console_port: 8084
|
||||
|
||||
# Controller-side URL (default to direct VM IP/port)
|
||||
redpanda_console_url: "{{ lookup('env', 'REDPANDA_CONSOLE_URL') | default('http://192.168.69.254:8084/overview', true) }}"
|
||||
|
||||
redpanda_retries: "{{ RETRIES }}"
|
||||
redpanda_delay: 2
|
||||
|
||||
# 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)
|
||||
# NOTE: This only pulls the latest image; it does NOT recreate the container.
|
||||
redpanda_console_commands:
|
||||
- "{{ docker_prefix }} pull -q {{ redpanda_console_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 Redpanda Console update commands on VM (via SSH) # use SSHPASS env, hide item value
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- sshpass
|
||||
- -e # read password from SSHPASS environment
|
||||
- ssh
|
||||
- -o
|
||||
- StrictHostKeyChecking=no
|
||||
- -o
|
||||
- ConnectTimeout=15
|
||||
- "{{ vm_user }}@{{ vm_ip }}"
|
||||
- bash
|
||||
- -lc
|
||||
- "{{ ('sudo ' if use_sudo else '') + item }}"
|
||||
environment:
|
||||
SSHPASS: "{{ vm_pass }}" # supply password via environment
|
||||
loop: "{{ redpanda_console_commands }}"
|
||||
loop_control:
|
||||
index_var: idx # capture loop index
|
||||
label: "cmd-{{ idx }}" # avoid printing full command in (item=...) line
|
||||
register: redpanda_cmds
|
||||
changed_when: false
|
||||
no_log: "{{ DEBUG == 0 }}" # hide outputs and env when not debugging
|
||||
|
||||
- name: Show outputs for each Redpanda Console command
|
||||
ansible.builtin.debug:
|
||||
msg: |
|
||||
CMD: {{ item.item }}
|
||||
RC: {{ item.rc }}
|
||||
STDOUT:
|
||||
{{ (item.stdout | default('')).strip() }}
|
||||
STDERR:
|
||||
{{ (item.stderr | default('')).strip() }}
|
||||
loop: "{{ redpanda_cmds.results }}"
|
||||
when: DEBUG == 1
|
||||
|
||||
- name: Fail play if any Redpanda Console command failed # also hide item label
|
||||
ansible.builtin.assert:
|
||||
that: "item.rc == 0"
|
||||
fail_msg: "Redpanda Console update failed on VM: {{ item.item }} (rc={{ item.rc }})"
|
||||
success_msg: "All Redpanda Console update commands succeeded."
|
||||
loop: "{{ redpanda_cmds.results }}"
|
||||
loop_control:
|
||||
index_var: idx
|
||||
label: "cmd-{{ idx }}"
|
||||
|
||||
# -------------------------
|
||||
# Readiness checks (controller first, then VM fallback)
|
||||
# -------------------------
|
||||
|
||||
- name: Redpanda Console | Wait for overview page (controller first, with retries)
|
||||
ansible.builtin.uri:
|
||||
url: "{{ redpanda_console_url }}"
|
||||
method: GET
|
||||
return_content: true
|
||||
validate_certs: false # plain HTTP on 192.168.69.254
|
||||
status_code: 200
|
||||
register: redpanda_controller
|
||||
delegate_to: localhost
|
||||
run_once: true
|
||||
when: redpanda_console_url is defined and (redpanda_console_url | length) > 0
|
||||
retries: "{{ redpanda_retries }}"
|
||||
delay: "{{ redpanda_delay }}"
|
||||
until: redpanda_controller.status == 200
|
||||
failed_when: false
|
||||
changed_when: false
|
||||
|
||||
- name: Redpanda Console | VM-side fetch (HTML via Python, with retries)
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- sshpass
|
||||
- -e
|
||||
- ssh
|
||||
- -o
|
||||
- StrictHostKeyChecking=no
|
||||
- -o
|
||||
- ConnectTimeout=15
|
||||
- "{{ vm_user }}@{{ vm_ip }}"
|
||||
- bash
|
||||
- -lc
|
||||
- |
|
||||
python3 - <<'PY'
|
||||
# Fetch Redpanda Console overview page from localhost and print HTML to stdout
|
||||
import urllib.request, sys
|
||||
try:
|
||||
with urllib.request.urlopen("http://127.0.0.1:{{ redpanda_console_port }}/overview", timeout=15) as r:
|
||||
sys.stdout.write(r.read().decode(errors='ignore'))
|
||||
except Exception:
|
||||
pass
|
||||
PY
|
||||
environment:
|
||||
SSHPASS: "{{ vm_pass }}"
|
||||
register: redpanda_vm
|
||||
changed_when: false
|
||||
failed_when: false
|
||||
when: redpanda_controller.status | default(0) != 200 or redpanda_controller.content is not defined
|
||||
retries: "{{ redpanda_retries }}"
|
||||
delay: "{{ redpanda_delay }}"
|
||||
until: (redpanda_vm.stdout | default('') | trim | length) > 0
|
||||
no_log: "{{ DEBUG == 0 }}"
|
||||
|
||||
- name: Redpanda Console | Choose overview HTML (controller wins, else VM)
|
||||
ansible.builtin.set_fact:
|
||||
redpanda_overview_html: >-
|
||||
{{
|
||||
(
|
||||
redpanda_controller.content
|
||||
if (redpanda_controller is defined)
|
||||
and ((redpanda_controller.status|default(0))==200)
|
||||
and (redpanda_controller.content is defined)
|
||||
else
|
||||
(redpanda_vm.stdout | default('') | trim)
|
||||
)
|
||||
}}
|
||||
when:
|
||||
- (redpanda_controller is defined and (redpanda_controller.status|default(0))==200 and (redpanda_controller.content is defined))
|
||||
or ((redpanda_vm.stdout | default('') | trim | length) > 0)
|
||||
|
||||
- name: Redpanda Console | Print concise summary
|
||||
ansible.builtin.debug:
|
||||
msg: >-
|
||||
Redpanda Console overview {{ 'reachable' if (redpanda_overview_html is defined) else 'NOT reachable' }}.
|
||||
Source={{ 'controller' if ((redpanda_controller is defined) and (redpanda_controller.status|default(0))==200 and (redpanda_controller.content is defined)) else 'vm' if (redpanda_vm.stdout|default('')|trim|length>0) else 'n/a' }};
|
||||
length={{ (redpanda_overview_html | default('')) | length }};
|
||||
contains('Redpanda')={{ (redpanda_overview_html is defined) and ('Redpanda' in redpanda_overview_html) }}
|
||||
when: DEBUG == 1
|
||||
|
||||
- name: Redpanda Console | Web UI unavailable (after retries)
|
||||
ansible.builtin.debug:
|
||||
msg: "Redpanda Console web UI není dostupná ani po pokusech."
|
||||
when: redpanda_overview_html is not defined and DEBUG == 1
|
||||
|
||||
# Optional detailed dump (short excerpt only)
|
||||
- name: Redpanda Console | HTML excerpt (debug)
|
||||
ansible.builtin.debug:
|
||||
msg: "{{ (redpanda_overview_html | default(''))[:500] }}"
|
||||
when: redpanda_overview_html is defined and DEBUG == 1
|
||||
Reference in New Issue
Block a user