{"id":926,"date":"2021-11-19T09:56:39","date_gmt":"2021-11-19T09:56:39","guid":{"rendered":"https:\/\/techno.slomka.biz\/?p=926"},"modified":"2026-03-13T14:29:25","modified_gmt":"2026-03-13T14:29:25","slug":"get-systemd-services-in-ansible-plays","status":"publish","type":"post","link":"https:\/\/techno.slomka.biz\/?p=926","title":{"rendered":"Get systemd services in ansible plays"},"content":{"rendered":"\n<p>Hi, this is a tip to get details about linux (e.g. systemd) services in an ansible play. Just use the <a href=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/service_facts_module.html\" data-type=\"URL\" data-id=\"https:\/\/docs.ansible.com\/ansible\/latest\/collections\/ansible\/builtin\/service_facts_module.html\" target=\"_blank\" rel=\"noreferrer noopener\">ansible service_facts module<\/a>. You can use it without passing any argument.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#adbac7;--cbp-line-number-width:calc(1 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(139, 186, 234, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#22272e\"><span style=\"background:#9eadbd;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#22272e\">YAML<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>- name: Populate service facts\n  ansible.builtin.service_facts:<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ADBAC7\">- <\/span><span style=\"color: #8DDB8C\">name<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">Populate service facts<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #8DDB8C\">ansible.builtin.service_facts<\/span><span style=\"color: #ADBAC7\">:<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>You can access services by using <code>ansible_facts.services<\/code>. As this is an dictionary, a single service is addressable by using <code>ansible_facts.services[{{ service-name }}]<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#adbac7;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#22272e\"><span style=\"background:#9eadbd;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#22272e\">YAML<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>- name: show all service facts&lt;br>\n  ansible.builtin.debug:\n    var: ansible_facts.services&#91;{{ service_name }}&#93;<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ADBAC7\">- <\/span><span style=\"color: #8DDB8C\">name<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">show all service facts&lt;br&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #8DDB8C\">ansible.builtin.debug<\/span><span style=\"color: #ADBAC7\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #8DDB8C\">var<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">ansible_facts.services&#91;{{ service_name }}&#93;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>A few more examples:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers cbp-highlight-hover\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#adbac7;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(139, 186, 234, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#22272e\"><span style=\"background:#9eadbd;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#22272e\">YAML<\/span><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#adbac7;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>- name: Retrieve data about single service\n  debug:\n    var: ansible_facts.services.httpd\n\n- name: get a particular service attribute\n  ansible.builtin.debug:\n    var: ansible_facts.services.httpd.state\n\n- name&lt;\/span>: Test (RHEL7 and higher) if service is up and running\n  ansible.builtin.fail:\n    msg&lt;\/span>: \"Service httpd is already running\"\n  when: ansible_facts.services.&#91;\"httpd.service\"&#93; is defined<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark-dimmed\" style=\"background-color: #22272e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #ADBAC7\">- <\/span><span style=\"color: #8DDB8C\">name<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">Retrieve data about single service<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #8DDB8C\">debug<\/span><span style=\"color: #ADBAC7\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #8DDB8C\">var<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">ansible_facts.services.httpd<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">- <\/span><span style=\"color: #8DDB8C\">name<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">get a particular service attribute<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #8DDB8C\">ansible.builtin.debug<\/span><span style=\"color: #ADBAC7\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #8DDB8C\">var<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">ansible_facts.services.httpd.state<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">- <\/span><span style=\"color: #8DDB8C\">name&lt;\/span&gt;<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">Test (RHEL7 and higher) if service is up and running<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #8DDB8C\">ansible.builtin.fail<\/span><span style=\"color: #ADBAC7\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">    <\/span><span style=\"color: #8DDB8C\">msg&lt;\/span&gt;<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">&quot;Service httpd is already running&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ADBAC7\">  <\/span><span style=\"color: #8DDB8C\">when<\/span><span style=\"color: #ADBAC7\">: <\/span><span style=\"color: #96D0FF\">ansible_facts.services.&#91;&quot;httpd.service&quot;&#93; is defined<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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. 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 }}]. A few more examples:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[40],"class_list":["post-926","post","type-post","status-publish","format-standard","hentry","category-ansible","tag-ansible"],"_links":{"self":[{"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/posts\/926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/comments?post=926"}],"version-history":[{"count":27,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/posts\/926\/revisions"}],"predecessor-version":[{"id":1074,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/posts\/926\/revisions\/1074"}],"wp:attachment":[{"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/media?parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/categories?post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techno.slomka.biz\/wp-json\/wp\/v2\/tags?post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}