Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to get service status by Ansible?

Tags:

ansible

I want to get service like redis-server running status by Ansible.

I know how to use Ansible service module to stop or start system service. But how can I get the current service status?

like image 427
Moxmi Avatar asked Aug 09 '16 10:08

Moxmi


People also ask

How do I check my service status in Ansible?

Use command module with service redis-server status and parse stdout.

What is BOF in Ansible?

A value is available; BOF for inserting the line at the beginning of the file. If specified regular expression has no matches, the line will be inserted at the end of the file.

How do you check uptime on Ansible?

Create a file that lists all nodes where this task is required to be executed. Create an Ansible. cfg file and make sure it is pointing to inventory file which lists IP addresses of nodes. Run the Ansible Adhoc command to fetch uptime.

What is Service_facts?

service_facts module – Return service state information as fact data. Note. This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name service_facts even without specifying the collections: keyword.


3 Answers

You can also use the service_facts module.

Example usage:

- name: collect facts about system services
  service_facts:
  register: services_state

- name: Debug
  debug:
    var: services_state

Example output:

...

TASK [Debug] ***************************************************************************************************************************************************************************************************************
ok: [local] => {
    "services_state": {
        "ansible_facts": {
            "services": {
                "cloud-init-local.service": {
                    "name": "cloud-init-local.service",
                    "source": "systemd",
                    "state": "stopped"
                },
                "firewalld.service": {
                    "name": "firewalld.service",
                    "source": "systemd",
                    "state": "stopped"
                },
                ...
            }
        }
    }
}
like image 70
Jay Taylor Avatar answered Nov 11 '22 14:11

Jay Taylor


Just run the task service: name=httpd state=started with the option --check. This tells you, if the service needs to be started, which means that it is down. If the task shows no change, it is up already.

Example service is down, changed is true, because it needs to be started:

$ ansible -m service -a 'name=rpc/bind state=started' --check host
host | SUCCESS => {
    "changed": true, 
    "msg": "service state changed"
}

Example service is up, changed is false, because nothings need to be done:

$ ansible -m service -a 'name=system-log state=started' --check host
host | SUCCESS => {
    "changed": false, 
    "name": "system-log", 
    "state": "started"
}
like image 32
ceving Avatar answered Nov 11 '22 14:11

ceving


A very short program for checking services using ansible -

- name: checking service status
  hosts: www.linuxfoundation.org
  tasks:
  - name: checking service status
    command: systemctl status "{{ item }}"
    with_items:
    - firewalld
    - httpd
    - vsftpd
    - sshd
    - postfix
    register: result
    ignore_errors: yes
  - name: showing report
    debug:
     var: result
like image 37
linux.cnf Avatar answered Nov 11 '22 13:11

linux.cnf