Fix the if-then-else statement. The keyword then is missing. I've added ps parameter -x to include "processes which do not have a controlling terminal" (which normally web servers do not). The script below works as expected
#!/bin/sh
if ps -axef | egrep 'apache|http' | grep -v grep > /dev/null; then
echo 'process_running'
else
echo 'process_not_running'
fi
Then use the script in Ansible. You might want to use Literal block scalar to improve the code's readability. For example
- name: verify application/database processes are not running
shell: |
if ps -axef | egrep 'apache|http' | grep -v grep > /dev/null; then
echo 'process_running'
else
echo 'process_not_running'
fi
ignore_errors: false
register: app_process_check
- debug:
var: app_process_check.stdout
gives, if Apache is running
app_process_check.stdout: process_running
otherwise
app_process_check.stdout: process_not_running