How can I get ansible to report whether or not a daemon is running on linux? I am not looking to perform an action based on the results. I just want a list of servers with Apache running. I am running RHEL 6 and 7. So RHEL 7 is using systemd and RHEL 6 is using the old init scripts.
The Ansible service_facts module is probably a good bet.
IIRC the Apache service is named
httpd on all major RHEL releases so you don’t have to make release depending plays and the Ansible module takes care of systemd and upstart differences.
Something along the lines of the example below should do the trick (note there is a difference in checking if a service is enabled or actually running. A service that is not currently running, because it failed or was stopped by an admin, but should be running because the service is enabled or vice-versa )
- name: Check for apache status service_facts: - debug: var: ansible_facts.services.httpd.state
Better to use some monitoring tool. Ansible is config mgt. tool based on action perform at server side via ssh. It's not a productive idea.
If this is the only option then you can use Ansible Tower -> with Schedule (like cron) -> that can be used with Slack/email notification.
You can use command : /etc/init.d/httpd status with register to check stdout output and if it matches condition then it can trigger alert. Ex:
change NOT with any other output that you see while checking status.
- name: check http command: /etc/init.d/httpd status register: httpd_status failed_when: "'NOT' in httpd_status.stdout" changed_when: False
The answer using
service_facts is very interesting, but you also could solve this by the implementation of some "custom_facts" that can return customised values.
Custom-facts are scripts that are executed at fact-gathering and return a JSON-object.