From a39389768247a5ff4ab7a20cd3424594e61f16fc Mon Sep 17 00:00:00 2001 From: Christian Elberfeld <christian.elberfeld@adesso.de> Date: Sun, 2 Dec 2018 21:04:17 +0100 Subject: [PATCH] get_secret --- README.md | 6 ++++-- get_secret.yml | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 get_secret.yml diff --git a/README.md b/README.md index 93c6309..fce58c1 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 0000000..e8b5964 --- /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', '') }}" + -- GitLab