--- # commands.yml - Execute commands on Prezenta devices # Provides structured command execution with logging and error handling - name: Execute Command on Prezenta Devices hosts: "{{ target_devices | default('prezenta_devices') }}" gather_facts: no vars: command_to_run: "{{ command | default('pwd') }}" command_user: pi log_commands: true tasks: - name: Display command execution info debug: msg: | Executing on: {{ inventory_hostname }} ({{ ansible_host }}) Command: {{ command_to_run }} User: {{ command_user }} - name: Execute command shell: "{{ command_to_run }}" register: command_result become: yes become_user: "{{ command_user }}" ignore_errors: yes - name: Display command output debug: msg: | Return Code: {{ command_result.rc }} STDOUT: {{ command_result.stdout }} STDERR: {{ command_result.stderr | default('None') }} - name: Log command execution lineinfile: path: "{{ app_directory }}/data/command_history.log" line: "[{{ ansible_date_time.iso8601 }}] [{{ inventory_hostname }}] Command: {{ command_to_run }} | RC: {{ command_result.rc }}" create: yes state: present delegate_to: "{{ inventory_hostname }}" become: yes when: log_commands - name: Fail if command failed fail: msg: "Command failed on {{ inventory_hostname }} with return code {{ command_result.rc }}" when: command_result.rc != 0 and fail_on_error | default(false)