Below is my playbook that dumps the processes matching .*httpd.*|.*sjsws.*|.*java.*|.*docker.*|.*node.*
Playbook:
vars:
grepelement: ".*httpd.*|.*sjsws.*|.*java.*|.*docker.*|.*node.*"
- name: "Check processes on destination"
ignore_errors: yes
command: ps auxwww
register: ps_out
- set_fact:
processdump: "{{ ps_out.stdout | regex_findall(grepelement) }}"
- set_fact:
processdumpcount: "{{ ps_out.stdout.split() | length }}"
Unfortunately, although only 4 processes are running the count seems to print more than 1000s which could be the number of characters in the output.
I also tried the below but this too prints more than the number of processes running.
- set_fact:
processdumpcount: "{{ ps_out.stdout_lines | length }}"
I can use command: ps auxwww | wc -l
as a solution but prefer solution with a single ps
command.
Can you please suggest?
You need to check the length of processdump
register for the count not ps_out.stdout_lines
- set_fact:
processdumpcount: "{{ processdump | length }}"
Note that ps_out
is holding on to all the processes running on the system and later you are operating ps_out
with regex_findall
to filter out the matching processes to processdump
. you need to take length
of processdump
register.
Minimal working example:
My system process counts:
ps auxwww |wc -l
287
ps auxwww |grep -E ".*httpd.*|.*sjsws.*|.*java.*|.*docker.*|.*node.*" |grep -v grep |wc -l
3
playbook:
---
- name: Sample playbook
connection: local
# gather_facts: false
hosts: localhost
vars:
grepelement: ".*httpd.*|.*sjsws.*|.*java.*|.*docker.*|.*node.*"
tasks:
- name: "Check processes on destination"
ignore_errors: yes
command: ps auxwww
register: ps_out
- set_fact:
processdump: "{{ ps_out.stdout | regex_findall(grepelement) }}"
- set_fact:
processdumpcount: "{{ processdump | length }}"
- debug: msg="{{ processdumpcount }}"
Above playbook would result in:
PLAY [Sample playbook] **********************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]
TASK [Check processes on destination] *******************************************************************************************************************************
changed: [localhost]
TASK [set_fact] *****************************************************************************************************************************************************
ok: [localhost]
TASK [set_fact] *****************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "3"
}
PLAY RECAP **********************************************************************************************************************************************************
localhost : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0