Skip to content
Advertisement

ansible playbook: check linux process count

I am trying to write a playbook which shows failed when number of process running in linux host is not equal to 2. Following playbook works, but is there any better way of doing it? like is there any specific Ansible module to check process linux host?

  ---
  - hosts: web
    become: yes
    become_method: su
    become_user: webuser
    tasks:
    - name: process check
      shell: ps -ef|grep -i etas|grep -v grep|wc -l
      register: command_result
      failed_when: "'2' not in command_result.stdout"

Advertisement

Answer

What is your Ansible version? There is no Ansible module to count the number of processes in Linux. You can make it readable by:

  tasks:
  - name: process check
    shell: pgrep etas | wc -l
    register: command_result
    failed_when: command_result.stdout|int != 2
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement