2
\$\begingroup\$

I have a task which checks the status of a process running on a Linux server using a shell. The task should report as failed when the status is NOT RUNNING. The following works but is there a better way of checking the status string in the command.

  - name: check status of NC services and processes
    become: yes
    become_method: sudo
    become_user: nc
    environment:
         PATH: "{{ ncpath }}"
    shell: nco_pa_status -server yyyy -password xxxx
    register: command_result
    failed_when: >
      'RUNNING' not in command_result.stdout or
      'PENDING' in command_result.stdout or
      'WAITING' in command_result.stdout or
      'STARTING' in command_result.stdout or
      'DEAD' in command_result.stdout or
      'ERROR' in command_result.stdout

  - debug:
      msg="{{ command_result.stdout }}"
    tags: nervecenter-status

command_result.stdout looks something like the one below. If any process is not running the status will have any of the following: PENDING, WAITING, STARTING, DEAD, ERROR.

 -------------------------------------------------------------------------------
 Service Name         Process Name         Hostname   User      Status      PID
 -------------------------------------------------------------------------------
 Core                 ObjectServer         xxxx      nco       RUNNING    1234
                      Proxy                xxxx      root      RUNNING     256
 -------------------------------------------------------------------------------
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.