How to get service status by ansible

Ansible is an open-source automation tool that allows you to automate IT tasks such as configuration management, application deployment, and task automation. It uses a simple, human-readable language called YAML to define automation tasks and playbooks, making it easy to understand and use even for those without extensive programming knowledge. Ansible operates over SSH or WinRM, and it does not require any additional software to be installed on the managed nodes, which makes it lightweight and easy to get started with.

Service status in Ansible

In Ansible, a service refers to a system service or daemonA daemon is a service process that runs in the background and supervises the system or provides functionality to other processes. that runs on a managed node. Services are background processes that perform specific functions or provide functionality to the system. Examples of services include web servers (e.g., Apache or Nginx), database servers (e.g., MySQL or PostgreSQL) and various other system-level services (e.g., SSH, cron).

The provided Ansible playbook below is designed to check the status of the cron service on localhost (the Ansible control node). It uses shell commands to query the status of the service through various methods (systemctl and service). The playbook tries different commands in case one method fails, allowing for more flexibility and compatibility with different systems.

---
- name: Get service status using shell command
  hosts: localhost
  connection: local
  tasks:
    - name: Check service status with systemctl
      shell: systemctl is-active cron || true
      register: service_status_systemctl
      ignore_errors: yes

    - name: Check service status with service (cron)
      shell: service cron status || true
      register: service_status_service_cron
      ignore_errors: yes

    - name: Check service status with service (crond) - fallback
      shell: service crond status || true
      register: service_status_service_crond
      ignore_errors: yes

    - name: Display service status
      debug:
        msg: "Cron Service status: {{ service_status_systemctl.stdout | default(service_status_service_cron.stdout, true) | default(service_status_service_crond.stdout, true) }}"

Code explanation

  • Line 1: The YAML file starts with three hyphens (---) to signify the beginning of a YAML document.

  • Line 3: This line specifies that the playbook runs on the Ansible control node and targets the localhost as the managed node where the tasks will be executed.

  • Line 4: Here, we specify that the playbook should be executed locally on the Ansible control node, not over SSH.

  • Line 69: The first task is named Check service status with systemctl. It uses the shell module to run the command systemctl is-active cron || true. This command checks the status of the cron service using systemctl is-active. The OR with true ensures that if the command returns a non-zero exit code (indicating an error), Ansible will not treat it as a failure. The result is then stored in the variable service_status_systemctl.

  • Line 1114: The second task is named Check service status with service (cron). It uses the shell module to run the command service cron status || true. This command checks the status of the cron service using the traditional service command.

  • Line 1619: The third task is named Check service status with service (crond) - fallback. It uses the shell module to run the command service crond status || true. This command checks the status of the crond service as a fallback method, in case the cron service is not found. The result is stored in the variable service_status_service_crond.

  • Line 2123: The fourth task is named Display service status. It uses the debug module to print the status of the cron service. The task includes an expression {{ ... }}, which retrieves the output of the previous tasks using the registered variables. The default(..., true) filter handle cases where a particular method might have failed, and the fallback method's result is used instead. The final status message is displayed using msg.

Conclusion

The playbook serves as an example of how Ansible can be used to manage and automate tasks related to services on managed nodes, allowing administrators to ensure service availability and troubleshoot potential issues easily. It can be adapted to check the status of other services by modifying the service names accordingly.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved