diff --git a/README.md b/README.md index 93c63093ac46d6ec995a6d62a3c3973fcbd04489..fce58c1968dfab4f78a6c29a94e3754a4cc92110 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ -# ansible-functions -Ansible helper functions + +# Ansible Functions +Ansible helper functions for usage in other ansible projects + diff --git a/get_secret.yml b/get_secret.yml new file mode 100644 index 0000000000000000000000000000000000000000..e8b5964e8a911b814182911342400c8e888f5262 --- /dev/null +++ b/get_secret.yml @@ -0,0 +1,49 @@ +--- +# Hilfsfunktion zum auslesen lokal gespeicherter Secrets auf dem Server +# Die Secrets sind aus dem Server jeweils in einer Datei gespeichert +# Zum Auslesen wird die Datei über Slurp geladen und in einer Variable entsprechend dem +# Dateinamen registriert. +# Falls die Datei noch nicht existiert wird das Secret entsprechend der vorgegebenen +# Länge initialisiert +# +# Beispiel: (Auslesen von Passörtern aus /srv/xyz/secret_pw, registrierung als Variable secret_pw, erzeugung mit 24 Zeichen falls nicht vorhanden) +# +# - include: ../functions/get_secret.yml +# with_items: +# - { path: /srv/xyz/secret_pw, length: 24 } +# - { path: /srv/xyz/secret2_pw, length: 12 } + +# Check if file exists +- name: "{{ item.path | basename }} (check directory)" + file: + path: "{{ item.path | dirname }}" + state: "directory" + +# Check if file exists +- name: "{{ item.path | basename }} (check file)" + stat: + path: "{{ item.path }}" + register: filestat + +# Generate secret if missing +- name: "{{ item.path | basename }} (generate: install openssl)" + apt: + pkg: openssl + update_cache: no + state: present + when: filestat.stat.exists == False + +- name: "{{ item.path | basename }} (generate: length = {{ item.length }})" + command: "openssl rand -base64 -out {{ item.path }} {{ item.length }}" + when: filestat.stat.exists == False + +# Get Secret +- name: "{{ item.path | basename }} (slurp)" + slurp: src={{ item.path }} + register: secretfile + +# Decode Secret and register fact +- name: "{{ item.path | basename }} (decode)" + set_fact: + "{{ item.path | basename }}": "{{ secretfile.content | b64decode | regex_replace('\\s', '') }}" +