-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow loop on dict keys like python #53041
Comments
Files identified in the description: If these files are inaccurate, please update the |
I've created a PR #53042 for this. |
Files identified in the description: If these files are inaccurate, please update the |
We prefer to keep the types accepted strict, you can already do what you want this way: - name: loop dict on key
debug:
msg: "dict value for {{item}} is {{my_dict[item]}}"
loop: '{{my_dict|list}}'
vars:
my_dict:
a: 1
b: 2 |
@bcoca nice, this is already much better than dict2items. |
Most non programmers understand 'a list' but not 'iterable', a string is also 'iterable' but we don't want to:
to output
|
We have discussed this and decided that ansible/proposals#99 If you have further questions please stop by IRC or the mailing list:
|
@sivel thanks. |
SUMMARY
Allow loop on dict keys like python.
ISSUE TYPE
COMPONENT NAME
ADDITIONAL INFORMATION
Currently ansible support dict loop with
dict2items
filter:To loop dict, users need to remember a few things in mind:
dict2items
item
to refer to itemitem.key
to refer to keyitem.value
to refer to valueConsidering dict is so common everywhere, above syntax is actually too trivial and overloaded.
The python equavelant is:
In python, you can also loop dict just like list:
this is handy for usage, breif and consistent in syntax.
Any one with basic python expericence will expect to use similar syntax in ansible:
But ansible loop forbid that, which is a pity and unnecessary.
This patch enables this feature with tiny change.
Example and compare:
The text was updated successfully, but these errors were encountered: