Last active
March 14, 2016 23:34
-
-
Save aurelienmaury/d0bb552bcc0adcb6d581 to your computer and use it in GitHub Desktop.
Builds a list of all systemctl-managed services so you can check if a given service exist before applying "service" ansible module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
become: yes | |
tasks: | |
- shell: systemctl list-unit-files --type=service | grep ".service" | tr -s " " | sed -e "s/ *$//g" | |
register: clean_systemctl | |
changed_when: false | |
- set_fact: | |
yaml_ready_systemctl: > | |
{{ clean_systemctl.stdout_lines | map("regex_replace", "^(.*) (.*)$", "{'name': '\1','state': '\2'}") | list }} | |
- set_fact: | |
yaml_systemctl: > | |
{{ yaml_ready_systemctl | map('regex_replace', "\.service", "") | map('from_yaml') | list }} | |
- set_fact: | |
all_services: > | |
{{ yaml_systemctl | map(attribute='name') | list }} | |
enabled_services: > | |
{{ yaml_systemctl | selectattr("state", "equalto", "enabled") | map(attribute='name') | list }} | |
disabled_services: > | |
{{ yaml_systemctl | selectattr("state", "equalto", "disabled") | map(attribute='name') | list }} | |
- debug: var=enabled_services |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment