Skip to content
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 #53042

Closed
wants to merge 1 commit into from

Conversation

guoqiao
Copy link
Contributor

@guoqiao guoqiao commented Feb 27, 2019

Allow ansible to loop dict like python:

- name: loop dict on key
  debug:
    msg: "dict value for {{item}} is {{my_dict[item]}}"
  loop: '{{my_dict}}'
SUMMARY
  • rm type check for list in task_executor
  • handle loop items for both list and dict

Fix #53041

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

loop/task_executor

ADDITIONAL INFORMATION

Refer to #53041


@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 core_review In order to be merged, this PR must follow the core review workflow. feature This issue/PR relates to a feature request. needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Feb 27, 2019
@mattclay mattclay added the ci_verified Changes made in this PR are causing tests to fail. label Feb 28, 2019
@ansibot
Copy link
Contributor

ansibot commented Feb 28, 2019

The test ansible-test sanity --test ansible-doc --python 2.6 [explain] failed with the error:

Command "ansible-doc -t strategy debug free host_pinned linear" returned exit status 250.
>>> Standard Error
ERROR! Unexpected Exception, this is probably a bug: invalid syntax (task_executor.py, line 272)

The test ansible-test sanity --test compile --python 2.6 [explain] failed with 1 error:

lib/ansible/executor/task_executor.py:272:49: SyntaxError: items = {key: _get_item(item) for key, item in items.items()}

click here for bot help

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Feb 28, 2019
In python, you can loop on dict just like list:

    for key in my_dict:
        val = my_dict[key]
        print(val)

this is handy for usage and consistent in syntax.
But ansible loop forbid that, which is unnature and unnecessary.
This patch enables this feature with tiny change.

Example:

    - hosts: localhost
      vars:
        my_list:
          - item0
          - item1
        my_dict:
          key0: val0
          key1: val1
      tasks:
        - debug: var=item
          loop: '{{my_list}}'
        - debug: var=my_dict[item]
          loop: '{{my_dict}}'

Signed-off-by: Guo Qiao <guoqiao@gmail.com>
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Mar 4, 2019
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Mar 5, 2019
@sivel sivel self-assigned this Mar 5, 2019
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Mar 5, 2019
@sivel
Copy link
Member

sivel commented Mar 5, 2019

Thank you very much for your contribution. While we agree that loop UX can be improved, we do not believe this is the right path forward. If you would like to follow the discussions about improving loop UX, please see:

ansible/proposals#99
ansible/proposals#140

If you have further questions please stop by IRC or the mailing list:

@sivel sivel closed this Mar 5, 2019
@guoqiao
Copy link
Contributor Author

guoqiao commented Mar 5, 2019

thanks

@ansible ansible locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 feature This issue/PR relates to a feature request. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow loop on dict keys like python
5 participants