Skip to content

Instantly share code, notes, and snippets.

@aurelienmaury
Last active March 14, 2016 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aurelienmaury/d0bb552bcc0adcb6d581 to your computer and use it in GitHub Desktop.
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
---
- 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