I’m looking for a way to generate a random string required by an application (that will be put inside a template configuration) based on the encryption choice made by the user.
Inside the inventory:
vars:
# Depending on the algorithm you prefer, the playbook
# has to generate either a 32 key length (for A, B, C)
# or 16 key length (D, E) required by the program.
chosen_encryption: "A"
On the playbook side:
---
- name: Program setup
hosts: localhost
gather_facts: yes
tasks:
- name: Encoded random password (32 key length)
ansible.builtin.set_fact:
password: "{{lookup('community.general.random_string', base64=true, length=32)}}"
when: chosen_encryption == "A" or "B" or "C"
no_log: true
- name: Encoded random password (16 key length)
ansible.builtin.set_fact:
password: "{{lookup('community.general.random_string', base64=true, length=16)}}"
when: chosen_encryption == "D" or "E"
no_log: true
This string is then registered into a json configuration file:
However, the string written in the file is always 16 key length.
Am I missing something ? There is also probably a better way to write this playbook ? Despite having made some research, I can’t find a way to define a proper “if else” condition in this case.
Thanks @tanganellilore that would be way more elegant & efficient - I just focused on pointing-out how the conditions work on Ansible, but yeah you’re totally right