ansible-role-yedit
ansible-role-yedit copied to clipboard
Manipulate lists of dictionaries
I was trying this very useful role( hopefully a future module ;) ) and I'm not able to manipulate a list of dictionaries.
What I need to manipulate is something like:
nexus_roles:
- id: users
name: users_roles
description: Role to all users
privileges:
- nx-search-read
- all-repos-read
roles: []
- id: devs
name: devs_roles
description: Role to all devs
privileges:
- nx-search-read
- nx-repository-view-docker-*-*
- all-repos-read
roles: []
It would be useful to be able to create, delete or update parts or the full dictionary. I was trying several variants but did not manage to get this to work.
Was this use case not implemented?
Thanks
@keitalbame, if you have a specific example then I can assist. I believe replacing or updating parts of the dictionary does work.
@keitalbame, quick example of how to modify:
# example of modifying an entire dictionary
- yedit:
content: "{{ nexus_roles }}"
key: "[0]"
value:
id: foo
description: test
name: bar
register: output
# example of modifying a dictionary entry
- yedit:
content: "{{ nexus_roles }}"
key: "[0].name"
value: user_roles_updated
register: output
What is content: "{{ nexus_roles }}"
?
I don't even understand the example above from reading it lol
@BarbzYHOOL, content: "{{ nexus_roles}}"
is your variable. content
is just a way to manipulate data in memory. If the data is in a file then use src: path/to/file
.
Ok but on the other issue, you put a similar example, I tried it but I needed to create the var first. So I did it. I put either a dummy value or an empty value or nothing and then
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: __main__.YeditException: Unexpected item type found while going through key path: [1].programs
fatal: [localhost]: FAILED! => changed=false
module_stderr: |-
Traceback (most recent call last):
File "<stdin>", line 113, in <module>
File "<stdin>", line 105, in _ansiballz_main
File "<stdin>", line 48, in invoke_module
File "/tmp/ansible_yedit_payload_BBtCam/__main__.py", line 969, in <module>
File "/tmp/ansible_yedit_payload_BBtCam/__main__.py", line 961, in main
File "/tmp/ansible_yedit_payload_BBtCam/__main__.py", line 889, in run_ansible
File "/tmp/ansible_yedit_payload_BBtCam/__main__.py", line 800, in process_edits
File "/tmp/ansible_yedit_payload_BBtCam/__main__.py", line 693, in put
File "/tmp/ansible_yedit_payload_BBtCam/__main__.py", line 351, in add_entry
__main__.YeditException: Unexpected item type found while going through key path: [1].programs
module_stdout: ''
msg: |-
MODULE FAILURE
See stdout/stderr for the exact error
rc: 1
There are a few elements to your request. It seems to me that you would like to update a list of dictionaries by first finding the dictionary by using an attribute of the dictionary and the proceed to update. Currently this isn't supported.
When updating a list, yedit requires knowledge of the position of the item in the list or the value that is being inserted. Both of these pieces of information can aid in replacing the underlying value. If the list order is changing then you will have a couple of options:
- find the position of the element you wish to modify in a previous task and then use the index or current_value in the subsequent yedit call
- update the yedit module with the behavior you desire (if entry is a dict, find by attribute, and update as necessary)