--- # This file is managed by include_tasks and is not a standalone playbook. # It provides generic tasks for managing a process within a tmux session. # Required variables: # - tmux_session_name: The name of the tmux session. # - working_dir: The directory where the command should be executed. # - command_to_run: The command to execute inside the tmux session. # - process_grep_pattern: A regex pattern to identify the process for pkill/status check. # # Optional variables (control flags): # - start_process: (bool) Set to true to start the process. Default: false. # - stop_process: (bool) Set to true to stop the process. Default: false. # - check_status: (bool) Set to true to check and display the process status. Default: false. - name: Ensure tmux is installed ansible.builtin.apt: name: tmux state: present become: yes run_once: true # No need to run this on every include - name: Stop existing tmux session ansible.builtin.shell: cmd: "tmux kill-session -t {{ tmux_session_name }} 2>/dev/null || true" when: stop_process | default(false) | bool - name: Kill any orphaned processes ansible.builtin.shell: cmd: | PIDS=$(ps aux | grep -E "{{ process_grep_pattern }}" | grep -v "grep" | awk '{print $2}') if [ -n "$PIDS" ]; then kill $PIDS >/dev/null 2>&1 || true sleep 0.5 kill -9 $PIDS >/dev/null 2>&1 || true fi changed_when: false when: stop_process | default(false) | bool - name: Stop existing process before starting (makes start idempotent) block: - name: Stop existing tmux session ansible.builtin.shell: cmd: "tmux kill-session -t {{ tmux_session_name }} 2>/dev/null || true" - name: Kill any orphaned processes ansible.builtin.shell: cmd: | PIDS=$(ps aux | grep -E "{{ process_grep_pattern }}" | grep -v "grep" | awk '{print $2}') if [ -n "$PIDS" ]; then kill $PIDS >/dev/null 2>&1 || true sleep 0.5 kill -9 $PIDS >/dev/null 2>&1 || true fi changed_when: false when: start_process | default(false) | bool - name: Ensure client script is executable ansible.builtin.file: path: "{{ working_dir }}/bin/ytops-client" mode: "a+x" when: start_process | default(false) | bool - name: Display command for tmux session ansible.builtin.debug: msg: "Command for tmux session '{{ tmux_session_name }}': {{ command_to_run }}" when: start_process | default(false) | bool - name: Start process in tmux session ansible.builtin.shell: cmd: | cd {{ working_dir }} # The command is wrapped with a final sleep to keep the tmux session alive for debugging even if the process fails. # The actual command is run in a subshell (...) to prevent 'exec' from terminating the parent shell. tmux new-session -d -s {{ tmux_session_name }} \ "if [ -f .env ]; then set -a; . ./.env; set +a; fi; \ COMMAND_TO_RUN='{{ command_to_run | replace("'", "'\\''") }}'; \ echo '>>> Running command:'; \ echo "\$COMMAND_TO_RUN"; \ echo '---'; \ (eval "\$COMMAND_TO_RUN") ; \ echo; echo '---'; echo 'Process exited. This tmux session will remain open for debugging.'; echo 'You can attach with: tmux attach -t {{ tmux_session_name }}'; \ sleep 3600" when: start_process | default(false) | bool - name: Check process status ansible.builtin.shell: cmd: | echo "Process status on {{ inventory_hostname }} for session '{{ tmux_session_name }}':" if tmux has-session -t {{ tmux_session_name }} 2>/dev/null; then echo " - Tmux session '{{ tmux_session_name }}' is running" ps aux | grep -E "{{ process_grep_pattern }}" | grep -v grep || echo " - No matching process found" else echo " - Tmux session '{{ tmux_session_name }}' is NOT running" fi register: status_check changed_when: false when: check_status | default(false) | bool - name: Display status ansible.builtin.debug: msg: "{{ status_check.stdout_lines }}" when: check_status | default(false) | bool