# Generating string based on inventory variable

**URL:** <https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [November 1, 2023, 8:48pm UTC](https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084 "2023-11-01T20:48:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![su8319](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/su8319/32/1285_2.png) [@su8319](https://forum.ansible.com/u/su8319)\
**Post date:** [November 1, 2023, 8:48pm UTC](https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084/1 "2023-11-01T20:48:17Z")

</div>

Hiii everyone 😊

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:

```auto
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:

```auto
---
- 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:

```auto
{
    "algorithm": "{{ chosen_encryption }}",
    "key": "{{ password }}"
}

```

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.

In advance, thanks for helping a beginner 💓

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [November 2, 2023, 12:54am UTC](https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084/2 "2023-11-02T00:54:32Z")

</div>

Hello @su8319

You’re setting the `when` conditions incorrectly - That is, you must specify both operators for each condition, like this:

```yaml
---
- name: Program setup
  hosts: localhost
  gather_facts: true
  vars:
    chosen_encryption: "A"

  tasks:

    - name: Encoded random password (32 key length)
      ansible.builtin.set_fact:
        password32: "{{ lookup('community.general.random_string', base64=true, length=32) }}"
      when: >-
        chosen_encryption == "A" or
        chosen_encryption == "B" or
        chosen_encryption == "C"
      no_log: true

    - name: Encoded random password (16 key length)
      ansible.builtin.set_fact:
        password16: "{{ lookup('community.general.random_string', base64=true, length=16) }}"
      when: >-
        chosen_encryption == "D" or
        chosen_encryption == "E"
      no_log: true

    - name: DEBUG
      ansible.builtin.debug:
        msg:
          - "password16 is {{ password16 | default('null') }}"
          - "password32 is {{ password32 | default('null') }}"
...

```

**Result:**

```auto
PLAY [Program setup] *************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************
ok: [localhost]

TASK [Encoded random password (32 key length)] ***********************************************************************************************************************************************************************
ok: [localhost]

TASK [Encoded random password (16 key length)] ***********************************************************************************************************************************************************************
skipping: [localhost]

TASK [DEBUG] *********************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "password16 is null",
        "password32 is KGh3N2o/WGc5bVg0bFA8UEwlK2BHeGFOVyJGaFg3QFs="
    ]
}

PLAY RECAP ***********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0  

```

Hope it helps!

---

<div class="post-metadata">

**Author:** ![su8319](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/su8319/32/1285_2.png) [@su8319](https://forum.ansible.com/u/su8319)\
**Post date:** [November 4, 2023, 5:43pm UTC](https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084/3 "2023-11-04T17:43:18Z")

</div>

Thanks for your solution, it work like a charm !

---

<div class="post-metadata">

**Author:** ![tanganellilore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tanganellilore/32/178_2.png) [@tanganellilore](https://forum.ansible.com/u/tanganellilore)\
**Post date:** [November 4, 2023, 5:50pm UTC](https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084/4 "2023-11-04T17:50:35Z")

</div>

Hi,  
Why not use a “map” of choice that you need instead use a double when with double variable?

Something like

```yaml
criptography_map:
  A: 32
  B: 32
  C: 32
  D: 16
  E: 16

```

And then in task

```yaml
- name: Encoded random password 
  ansible.builtin.set_fact:
    password: "{{ lookup('community.general.random_string', base64=true, length=criptography_map[chosen_encryption] ) }}"
  no_log: true

```

This will be much simple, will not use 2 tasks and avoid usage of 2 different variables.

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [November 4, 2023, 5:55pm UTC](https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084/5 "2023-11-04T17:55:28Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![jbericat](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jbericat/32/627_2.png) [@jbericat](https://forum.ansible.com/u/jbericat)\
**Post date:** [November 4, 2023, 5:56pm UTC](https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084/6 "2023-11-04T17:56:31Z")

</div>

Glad it worked for you! Take a look to @tanganellilore answer too, you might find it interesting 😉

> [@tanganellilore](#):
>
> Hi,  
> Why not use a “map” of choice that you need instead use a double when with double variable?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 4, 2023, 5:56pm UTC](https://forum.ansible.com/t/generating-string-based-on-inventory-variable/2084/7 "2023-12-04T17:56:51Z")

</div>

Closed on December 4, 2023, 5:56pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
