How to get service status by Ansible?

2019-04-24 14:04发布

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?

标签: ansible
7条回答
何必那么认真
2楼-- · 2019-04-24 14:41

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

查看更多
走好不送
3楼-- · 2019-04-24 14:42

You wouldn't typically do this with Ansible. Ansible should be for declaratively defining how you want a server to look like.

As such you would typically just do something like:

- name: start redis
  service:
    name=redis-server
    state=started
    enabled=yes

You might do things conditionally like this:

- name: restart redis
  service:
    name=redis-server
    state=restarted
    enabled=yes
  when: redis_config.changed

To restart Redis when the configuration has changed but it would be rare to need to check whether a service is running.

In the absolute case that you do need to check whether a service is running (and I would strongly suggest that you think again about your Ansible role/playbook) then you could always shell out:

- name: check redis status
  shell: service redis-service status
查看更多
我只想做你的唯一
4楼-- · 2019-04-24 14:43

Personally, I like to have some kind of support Playbooks for getting the status of my services across my environments and to be able to restart them etc.

I'll therefore use on the one side the command module as recommended by Konstantin Suvorov but additionally i'll also check the expected port(s) to ensure that all required ports are up and my service is working as expected. This would look like the following in your case:

- name: verify redis-server service
  command: /usr/sbin/sservice redis-server status
  changed_when: false

- name: verify redis-server is listening on 6379
  wait_for: port=6379 timeout=1

The changed_when is just used because the command module will always set changed to true, although it is just a read-only command.

查看更多
Viruses.
5楼-- · 2019-04-24 14:54

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
查看更多
Bombasti
6楼-- · 2019-04-24 14:55

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"
                },
                ...
            }
        }
    }
}
查看更多
The star\"
7楼-- · 2019-04-24 14:58

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, change 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"
}
查看更多
登录 后发表回答