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 As this is an dictionary, a single service is addressable by using[{{ service-name }}].

name: show all service facts
    var:[{{ service_name }}]

A few more examples:

name: Retrieve data about single service

name: get a particular service attribute

name: Test (RHEL7 and higher) if service is up and running
    msg: “Service httpd is already running”
    when:[“httpd.service”] is defined

Leave a Reply