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.
– 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 }}]
.
– name: show all service facts
debug:
var: ansible_facts.services[{{ service_name }}]
A few more examples:
– name: Retrieve data about single service
debug:
var: ansible_facts.services.httpd
– name: get a particular service attribute
debug:
var: ansible_facts.services.httpd.state
– name: Test (RHEL7 and higher) if service is up and running
fail:
msg: “Service httpd is already running”
when: ansible_facts.services.[“httpd.service”] is defined