Hello everyone,
I have a problem that I just can’t solve.
I want to find and replace a value in a dict.
I need a task that searches for the elements under Part1 in the dict Starting_situation and replaces them with the value from To_be_replaced.
For example, the Part1-A is replaced by AAA1234.
At the end the same DIct with the replaced values should remain.
I would be very grateful for a solution, because somehow all my jinja attempts do not lead to the desired goal.
The ansible.utils.replace_keys filter does exactly what you want, but with keys instead of values. It should be possible to use ansible_collections/ansible/utils/plugins/plugin_utils/replace_keys.py as a starting point and create a filter that does a similar thing with values.
Another approach would be to convert Starting_situation to a string with to_yaml or to_json, then loop over your To_be_replaced keys substituting the values with regex_replace, and finally converting the whole thing back with from_yaml or from_json as appropriate. This may be a bit shaky if you have “interesting” strings in To_be_replaced – i.e. regex meta-characters.
Here’s a working implementation of the latter method:
@utoddl : Thank you very much, that is a very cool idea to serialize the dict and then search and replace and then deserialize again.
The solution works perfectly in my case.
Thank you, also a very interesting idea.
If I understand it correctly, then I would have to build the same structure with Part1 and then it would simply replace that. But searching for the right value makes it more difficult. But I think I can still use this for other challenges.
I tried using the combine filter on this problem, but everything I came up with (which wasn’t much) needed to have the parameters reversed. That is, instead of “A | combine(B)” I needed “B | combine(A)”. I’ve fruitlessly tried that before, so abandoned the attempt.