Ansible: How to get service status by Ansible?

Ansible: How to get service status by Ansible?

To check the status of a service using Ansible, you can use the service_facts module or the systemd module. Here's how to do it with both methods:

Method 1: Using service_facts

The service_facts module gathers facts about the services on a host. Here's an example playbook:

--- - name: Check service status hosts: your_target_host tasks: - name: Gather service facts service_facts: - name: Display the status of a specific service debug: msg: "Service {{ item }} is {{ services[item].state }}" loop: - your_service_name # Replace with your service name when: item in services 

Method 2: Using systemd

If you want to check the status of a service directly, you can use the systemd module:

--- - name: Check service status hosts: your_target_host tasks: - name: Check if the service is running systemd: name: your_service_name # Replace with your service name state: started register: service_status - name: Display service status debug: msg: "Service is {{ service_status.state }}" 

Summary

  • service_facts: Use this to gather and display facts about all services on the host.
  • systemd module: Use this for checking the status of a specific service and can also be used to start or stop services.
  • debug module: This is used to output the status for review.

Replace your_service_name and your_target_host with the actual service name and host.

Examples

  1. "Ansible - Check service status on a remote host using service module"

    Description: Use the service module to check the status of a service on a remote host.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Get the status of the nginx service service: name: nginx state: status register: service_status - name: Show the service status debug: msg: "Nginx service is {{ service_status.state }}" 

    Explanation: This playbook checks the status of the nginx service and prints whether it is running or stopped.

  2. "Ansible - Use systemd module to check service status"

    Description: Use the systemd module to manage and check the status of services on systems using systemd.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Get the status of the apache2 service systemd: name: apache2 state: started register: service_status - name: Show the service status debug: msg: "Apache2 service status: {{ service_status.status }}" 

    Explanation: This playbook uses the systemd module to get the status of the apache2 service and outputs the result.

  3. "Ansible - Check service status and handle service not found"

    Description: Check the status of a service and handle the case where the service is not found.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Get the status of the mysql service command: systemctl is-active mysql register: service_status ignore_errors: yes - name: Show the service status debug: msg: "MySQL service is {{ service_status.stdout if service_status.rc == 0 else 'not found' }}" 

    Explanation: This playbook uses the command module to check if the mysql service is active and handles cases where the service might not be found.

  4. "Ansible - Use command module to check service status via ps"

    Description: Check if a service is running by using ps and grep commands.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Check if nginx process is running command: ps aux | grep '[n]ginx' register: process_check - name: Show the service status debug: msg: "Nginx is running" if process_check.stdout else "Nginx is not running" 

    Explanation: This playbook uses ps and grep to check if the nginx process is running and outputs the status.

  5. "Ansible - Get detailed service status using ansible.builtin.command"

    Description: Use ansible.builtin.command to get detailed status information from a service.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Get detailed status of the redis-server ansible.builtin.command: systemctl status redis-server register: service_status - name: Show the service status debug: msg: "{{ service_status.stdout }}" 

    Explanation: This playbook retrieves detailed status information of the redis-server service and prints it.

  6. "Ansible - Check service status using service_facts"

    Description: Use setup module with service_facts to gather service information.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Gather service facts setup: filter: 'services' - name: Check status of the sshd service debug: msg: "SSHD service status: {{ ansible_facts.services['sshd'].state }}" 

    Explanation: This playbook gathers all service facts and checks the status of the sshd service.

  7. "Ansible - Use systemd module to check multiple services"

    Description: Check the status of multiple services using the systemd module.

    # playbook.yml - name: Check multiple services hosts: all tasks: - name: Check the status of nginx and apache2 systemd: name: "{{ item }}" state: status loop: - nginx - apache2 register: service_status - name: Show the service statuses debug: msg: "{{ item.name }} service is {{ item.status }}" loop: "{{ service_status.results }}" 

    Explanation: This playbook checks the status of multiple services and prints their statuses.

  8. "Ansible - Check service status using ansible.builtin.system"

    Description: Use ansible.builtin.system to get the status of a service.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Check the status of the apache2 service ansible.builtin.system: systemctl status apache2 register: service_status - name: Show the service status debug: msg: "{{ service_status.stdout }}" 

    Explanation: This playbook uses ansible.builtin.system to run a command and get the status of the apache2 service.

  9. "Ansible - Check if a service is active using systemd and when condition"

    Description: Check if a service is active and use a condition to handle the result.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Get the status of the mysql service systemd: name: mysql state: started register: service_status - name: Notify if mysql is not active debug: msg: "MySQL service is not active" when: service_status.status != 'active' 

    Explanation: This playbook checks if the mysql service is active and sends a notification if it is not.

  10. "Ansible - Check service status and handle service stopped state"

    Description: Check the service status and handle scenarios where the service is stopped.

    # playbook.yml - name: Check service status hosts: all tasks: - name: Get the status of the docker service service: name: docker state: status register: service_status - name: Notify if docker service is stopped debug: msg: "Docker service is stopped" when: service_status.state == 'stopped' 

    Explanation: This playbook checks if the docker service is stopped and sends a notification if it is.


More Tags

excel-2016 apache assertj ngxs ecmascript-next arrow-functions postman ddl fileapi iphone-web-app

More Programming Questions

More Animal pregnancy Calculators

More Electronics Circuits Calculators

More Genetics Calculators

More Gardening and crops Calculators