Search code examples
ansiblejinja2

Adding an element to a list of dictionaries with new random string


I have an ansible variable which is a list of dicts:

userdata:
  - { first: Mike, last: Smith, username: msmith}
  - { first: Jane, last: Largo, username: jlargo}
  - { first: Evan, last: Jones, username: ejones}

I would like to add a field to each entry, password that has a randomly generated 20 character alpha-numeric password assigned to each entry so I can use it to as applicable during the playbook for the various actions that need to sync that password to. I tried doing a block:, but learned that you can't do that with_items. I also couldn't find a way to use with_items and save back to that item's dictionary. The password lookup plugin looks promissing to get a random password without having to save it to a file (/dev/null), but I can't seem to get it in each element of the list.

In the end, I would like a structure like this:

userdata:
  - { first: Mike, last: Smith, username: msmith, password: FdUChpPtfonoPexMZmt0}
  - { first: Jane, last: Largo, username: jlargo, password: QR13sN7CP00LbdXZ0gON}
  - { first: Evan, last: Jones, username: ejones, password: OCQSmqCbO8239jHpQiRm}

Solution

  • You need to create a new list looping over your original one. For each item, you combine the existing with the password attribute. The following is an example:

    ---
    - hosts: localhost
      gather_facts: false
    
      vars:
        userdata:
          - { first: Mike, last: Smith, username: msmith }
          - { first: Jane, last: Largo, username: jlargo }
          - { first: Evan, last: Jones, username: ejones }
    
      tasks:
    
        - name: Create a new list with random password
          vars:
            user_entry: "{{ item | combine({'password': lookup('password', '/dev/null')}) }}"
          set_fact:
            users_with_pass: "{{ users_with_pass | default([]) + [user_entry] }}"
          loop: "{{ userdata }}"
    
        - name: Show result
          debug:
            var: users_with_pass
    

    Which gives:

    $ ansible-playbook test.yml 
    
    PLAY [localhost] **************************************************************************************************************************************************************************************************
    
    TASK [Create a new list with random password] *********************************************************************************************************************************************************************
    ok: [localhost] => (item={'first': 'Mike', 'last': 'Smith', 'username': 'msmith'})
    ok: [localhost] => (item={'first': 'Jane', 'last': 'Largo', 'username': 'jlargo'})
    ok: [localhost] => (item={'first': 'Evan', 'last': 'Jones', 'username': 'ejones'})
    
    TASK [Show result] ************************************************************************************************************************************************************************************************
    ok: [localhost] => {
        "users_with_pass": [
            {
                "first": "Mike",
                "last": "Smith",
                "password": "w-zk-BAqJuJ090mTy8g0",
                "username": "msmith"
            },
            {
                "first": "Jane",
                "last": "Largo",
                "password": "J6g07utP.ANtbY7Z58Za",
                "username": "jlargo"
            },
            {
                "first": "Evan",
                "last": "Jones",
                "password": "eqHdpKDpkpEBY21qTyz5",
                "username": "ejones"
            }
        ]
    }
    
    PLAY RECAP ********************************************************************************************************************************************************************************************************
    localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0