List hosts that have an active PHP process running by using Ansible

Viewed 29

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!

1 Answers

Using community.general.pids you can get a list of PIDs associated with a regex pattern (available in community.general version 5.2.0).

- name: pids
  community.general.pids:
      pattern: .*php.*
  register: pids

- name: debug
  debug:
      var: pids

For example, on my testing VM it is running NGINX and using a few php processes:

root         915  0.0  0.0 228644  3280 ?        Ss   Jan20   8:25 php-fpm: master process (/etc/php/8.1/fpm/php-fpm.conf)
nginx        989  0.0  0.1 229136  7284 ?        S    Jan20   0:01 php-fpm: pool www
nginx        990  0.0  0.1 229136  7788 ?        S    Jan20   0:01 php-fpm: pool www

Therefore the output on my system from this playbook is:

PLAY [play] *************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************
ok: [localhost]

TASK [pid : pids] *******************************************************************************************************************************************************
ok: [localhost]

TASK [pid : debug] ******************************************************************************************************************************************************
ok: [localhost] => {
    "pids": {
        "changed": false,
        "failed": false,
        "pids": [
            915,
            989,
            990
        ]
    }
}

Using this, you can perform updates on systems which have running PHP processes:

- name: Upgrade PHP tasks
  debug:
     msg: "Upgrade PHP with this task"
  when: pids.pids|length > 0
Related