i am trying to follow this thread but my output is not what was expected. every previous item is getting overwritten with the new item being added.
my input is a list that i am loading into my accounts_list variables is as follows:
account:
- PR_user1
- PR_user2
There are no passwords in the input file. i need to create random passwords for each of the user account, use them in setting up various services, and then dump them into a text file for human use.
my first task on which i am stuck is that once i have read them into a list, i want to iterate over them, create password for each account, and then store it inside a dictionary as key value pairs.
i have tried both of the techniques mentioned to add an item to an existing dictionary, using combine as well as '+'.
my input is a simple list called 'accounts'.
- set_fact:
# domain_accounts: "{{ domain_accounts|default({}) | combine({item|trim: lookup(...)} ) }}"
domain_accounts: "{{ domain_accounts|default([]) + [{item|trim:lookup('...)}] }}"
with_items: "{{account_list.accounts}}"
My output is as follows:
TASK [set account passwords]
******************************************************************
ok: [localhost] => (item=PR_user1) => {"ansible_facts": {"domain_accounts": [{"PR_user1": "u]oT,cU{"}]}, "changed": false, "item": "PR_user1"}
ok: [localhost] => (item=PR_user2) => {"ansible_facts": {"domain_accounts": [{"PR_user2": "b>npKZdi"}]}, "changed": false, "item": "PR_user2"}
Supposing that the variable lists is as follows (That I guess it's not your case):
With:
and
plus:
You will get:
But I guess you have something like:
So:
Will get:
This is the whole play for your reference:
And the results: