1

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.

2
  • Thanks to everyone. I guess I should have said what I wanted to do. I have a bunch of servers almost all of them have ansible. What I want is to ask each server if it is running a web server (apache or nginx) so that I'll have a list of servers running apache or NginX. From the answer yall gave me, I may be able to just that.
    – Mike
    Nov 7 '19 at 14:00
  • You do not need ansible on all your servers. You only need it on the control system you want to run ansible from. You need SSH access, sudo (or su), and python on the servers you want to manage with ansible.
    – 0xSheepdog
    Nov 14 '19 at 1:00
2

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
0

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:

- name: check http
  command: /etc/init.d/httpd status
  register: httpd_status
  failed_when: "'NOT' in httpd_status.stdout"
  changed_when: False
change NOT with any other output that you see while checking status.
1
  • I think you misread the question.
    – womble
    Oct 29 '19 at 21:51
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.