I've got a bunch of hosts that need their PHP packages updated. However, only the hosts which have an actual PHP process currently running need to be updated and therefore I need a list of only these specific machines.
I've created the following playbook:
---
- name: Check if PHP is running
hosts: "{{ host }}"
gather_facts: no
tasks:
- name: Process check
shell: ps aux | grep php
register: ps
- debug: var=ps.stdout_lines
which nets me the following results:
ansible-playbook php-running-check.yaml --ask-pass --e host=host1,host2
TASK [debug] *********************************************************************************************************************************************
ok: [host1] => {
"ps.stdout_lines": [
"user123 24356 0.0 0.0 113288 1204 pts/0 S+ 12:39 0:00 /bin/sh -c ps aux | grep php",
"user123 24358 0.0 0.0 113288 192 pts/0 R+ 12:39 0:00 /bin/sh -c ps aux | grep php"
]
}
ok: [host2] => {
"ps.stdout_lines": [
"user123 8941 0.0 0.0 113288 1204 pts/0 S+ 12:39 0:00 /bin/sh -c ps aux | grep php",
"user123 8943 0.0 0.0 112816 960 pts/0 S+ 12:39 0:00 grep php",
"root 12027 0.0 0.0 188 4 ? S 01:00 0:00 s6-supervise php-fpm",
"root 12046 0.0 0.1 31624 10212 ? Ss 01:00 0:01 php-fpm: master process (/etc/php7/php-fpm.conf)",
"911 12059 0.0 0.0 31648 5796 ? S 01:00 0:00 php-fpm: pool www",
"911 12060 0.0 0.0 31648 5796 ? S 01:00 0:00 php-fpm: pool www"
]
}
As you can see both hosts contain lines starting with user123 which is the shell command I run in the playbook. I only want to see hosts that contain php processes other than /bin/sh -c ps aux | grep php or grep php.
Ideally something like this:
TASK [Print] *********************************************************************************************************************************************
skipping: [host1]
ok: [host2]
ok: [host3]
skipping" [host4]
.
.
.
Apart from updating or installing packages, I'm a total noob when it comes to Ansible. Maybe I'm looking at this problem from a totally wrong direction. I'd appreciate any help or guidance. Thanks!