Get systemd services in ansible plays
Hi, this is a tip to get details about linux (e.g. systemd) services in an ansible play. Just use the ansible service_facts module. You can use it without passing any argument.
YAML
- name: Populate service facts
ansible.builtin.service_facts:You can access services by using ansible_facts.services. As this is an dictionary, a single service is addressable by using ansible_facts.services[{{ service-name }}].
YAML
- name: show all service facts<br>
ansible.builtin.debug:
var: ansible_facts.services[{{ service_name }}]A few more examples:
YAML
- name: Retrieve data about single service
debug:
var: ansible_facts.services.httpd
- name: get a particular service attribute
ansible.builtin.debug:
var: ansible_facts.services.httpd.state
- name</span>: Test (RHEL7 and higher) if service is up and running
ansible.builtin.fail:
msg</span>: "Service httpd is already running"
when: ansible_facts.services.["httpd.service"] is defined