creating new dictionary from existing dictionary

Hi Everyone,

I am trying to read a dict from a file (list of user ids), and create another dict with key/value pairs (user ids with passwords). But once my script completes, the new dictionary only has the final item. Every previous items gets overwritten with the new item instead of new item being added to the existing item(s).

My gist is here:

https://gist.github.com/akhawaja-aurea/9b0b66bcedb0d019898c656cddde5147

and I am following the below stackoverflow article which solves exactly my problem but I am not sure what I am missing:

https://stackoverflow.com/questions/35605603/using-ansible-set-fact-to-create-a-dictionary-from-register-results

Regards

Ali

Try with

var: “{{ var + item.pass }}”

Cheers

I tried the following as well:

  • set_fact:

domain_accounts: “{{ domain_accounts|default() + [{item|trim: lookup(…)}] }}”

with_items: “{{account_list.accounts}}”

  • name: look up password

debug:

msg: ‘{{ domain_accounts[“PR_user2”] }}’

I get the following output (cleanedup to show relevant info only). The items are not added and lookup fails as well. It seems I have an array of arrays. How does my lookup changes in this case?

TASK [set account passwords] ************************************************************************************************************

(item=PR_user1) => {“ansible_facts”: {“domain_accounts”: [{“PR_user1”: “u]oT,cU{”}]}, “changed”: false, “item”: “PR_user1”}

(item=PR_user2) => {“ansible_facts”: {“domain_accounts”: [{“PR_user2”: “b>npKZdi”}]}, “changed”: false, “item”: “PR_user2”}

TASK [look up password] ***************************************************

fatal: [localhost]: FAILED! => {“msg”: “The task includes an option with an undefined variable. The error was: ‘list object’ has no attribute ‘PR_user2’\n\nThe error appears to have been in ‘/mnt/c/GitHub/aurea-nextdocs-operations/terraform-ansible/akhawaja-spnextdocs/TestScripts/test-template.yaml’: line 21, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: look up password\n ^ here\n”}