Is there any option in Ansible systemd
module to print details of Cgroup?
Below snippet does not print any Cgroup details of particular service.
- name: service
systemd:
name: test.service
register: service_status
- name: print
debug:
msg: "{{ service_status.status.ControlGroup }}"
Above code will print only below output
msg: /system.slice/test.service
whereby CLI command
sudo systemctl status test.service
would print below Cgroup PID details
Cgroup: /system.slice/test.service
1000 processname1
1002 processname2
1003 processname3
Ansible is mainly a Configuration Management tool with which you declare a Desired State. By using a service
module (to) Manage services like systemd
module (to) Manage systemd units you can make sure that a service is in a certain state
.
Even it seems to be possible to gather the service state by using (annot.: not using service_facts
module – Return service state information as fact data).
---
- hosts: localhost
become: true
gather_facts: false
tasks:
- name: Get current service state
systemd:
name: "{{ SERVICE_NAME }}"
register: result
- name: Show result
debug:
msg: "{{ result }}"
it will result in to the output of
...
ControlPID: '0'
ExecMainPID: '123'
...
MainPID: '123'
...
and the main PID only.
Looking into the Source Code of the module systemd.py
this for intension
def parse_systemctl_show(lines):
# The output of 'systemctl show' can contain values that span multiple lines. At first glance it
...
# part of that value. Cryptically, this would lead to Ansible reporting that the service file
# couldn't be found.
#
# To avoid this issue, the following code only accepts multi-line values for keys whose names
# start with Exec (e.g., ExecStart=), since these are the only keys whose values are known to
# span multiple lines.
...
For monitoring a service or processes and sub-processes you may use an other approach or provide more details and information regarding your use case and what you try to achieve.
You may also have a look into pids
module – Retrieves process IDs list if the process is running otherwise return empty list and
- name: Get current service PIDs
pids:
name: "{{ SERVICE_NAME }}"
register: result
- name: Show result
debug:
msg: "{{ result.pids }}"
which would result into an additional output of
TASK [Show result] ******
ok: [localhost] =>
msg:
- 124
and the sub-processes of the example service here.