forked from jakub/ansible
Refactor miniplay.yml: enhance command execution structure, improve documentation, and add error handling for VM commands.
This commit is contained in:
@@ -1,4 +1,22 @@
|
||||
- name: Get VM hostname via Proxmox jump
|
||||
# """
|
||||
# Run the command and check the response.
|
||||
#
|
||||
# :parameter command: The command to be executed.
|
||||
# :parameter add_command: Whether to include the command in the database values.
|
||||
# :parameter add_response: Whether to include the response in the database values.
|
||||
# :parameter measurement: The measurement index for the command.
|
||||
# :parameter measure_retries: The number of retries for measurement in case of failure.
|
||||
# :parameter check_port: Whether to check the port before sending the command.
|
||||
# :parameter response_equals: The expected response string to compare against.
|
||||
# :parameter response_length: The expected length or format of the response.
|
||||
# :parameter send_skipped: Whether to mark the test as skipped without execution.
|
||||
# :parameter no_response: Whether to expect no response from the command.
|
||||
# :parameter expect_patch_id_decimal: The expected patch ID in decimal format for validation.
|
||||
# :parameter response_equals_match: The expected response string to match against.
|
||||
# :return: RunResultType
|
||||
# """
|
||||
|
||||
- name: Run Nextcloud maintenance on VM via Proxmox
|
||||
hosts: proxmox
|
||||
gather_facts: false
|
||||
become: true
|
||||
@@ -10,14 +28,24 @@
|
||||
vm_user: "howard"
|
||||
vm_pass: "Papadopolus0"
|
||||
|
||||
# Flip to true if Docker needs sudo on the VM
|
||||
use_sudo: false
|
||||
|
||||
vm_commands:
|
||||
- "docker exec -u www-data nextcloud php -f /var/www/html/cron.php"
|
||||
- "docker exec -u www-data nextcloud php occ app:update --all"
|
||||
- "docker exec -u www-data nextcloud php occ maintenance:repair --include-expensive"
|
||||
- "docker exec -u www-data nextcloud php occ status"
|
||||
- "bash /data/compose/nextcloud/stack-health.sh"
|
||||
|
||||
tasks:
|
||||
- name: Ensure sshpass is installed (for password-based SSH) # English comments as requested
|
||||
- name: Ensure sshpass is installed (for password-based SSH) # English comments
|
||||
ansible.builtin.apt:
|
||||
name: sshpass
|
||||
state: present
|
||||
update_cache: yes
|
||||
|
||||
- name: Get hostname from VM using argv (no shell, no line breaks)
|
||||
- name: Run Nextcloud commands on VM (via SSH, argv, no line breaks)
|
||||
ansible.builtin.command:
|
||||
argv:
|
||||
- sshpass
|
||||
@@ -27,14 +55,29 @@
|
||||
- -o
|
||||
- StrictHostKeyChecking=no
|
||||
- -o
|
||||
- ConnectTimeout=10
|
||||
- ConnectTimeout=15
|
||||
- "{{ vm_user }}@{{ vm_ip }}"
|
||||
- hostname
|
||||
register: vm_hostname
|
||||
- bash
|
||||
- -lc
|
||||
- "{{ ('sudo ' if use_sudo else '') + item }}"
|
||||
loop: "{{ vm_commands }}"
|
||||
register: vm_cmds
|
||||
changed_when: false
|
||||
failed_when: vm_hostname.rc != 0
|
||||
no_log: false # set to true once you move creds into Vault
|
||||
|
||||
- name: Print VM hostname
|
||||
- name: Show outputs for each command
|
||||
ansible.builtin.debug:
|
||||
msg: "VM hostname: {{ vm_hostname.stdout | default('N/A') }}"
|
||||
msg: |
|
||||
CMD: {{ item.item }}
|
||||
RC: {{ item.rc }}
|
||||
STDOUT:
|
||||
{{ (item.stdout | default('')).strip() }}
|
||||
STDERR:
|
||||
{{ (item.stderr | default('')).strip() }}
|
||||
loop: "{{ vm_cmds.results }}"
|
||||
|
||||
- name: Fail play if any command failed
|
||||
ansible.builtin.assert:
|
||||
that: "item.rc == 0"
|
||||
fail_msg: "Command failed on VM: {{ item.item }} (rc={{ item.rc }})"
|
||||
success_msg: "All commands succeeded."
|
||||
loop: "{{ vm_cmds.results }}"
|
||||
Reference in New Issue
Block a user