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

Simplify and improve package name squashing #15657

Closed
wants to merge 2 commits into from

Conversation

willthames
Copy link
Contributor

@willthames willthames commented Apr 29, 2016

ISSUE TYPE
  • Bugfix Pull Request
ANSIBLE VERSION
ansible 2.2.0 (devel 7af47a3886) last updated 2016/04/29 11:28:56 (GMT +1000)
  lib/ansible/modules/core: (detached HEAD e78ee3b128) last updated 2016/04/29 11:28:58 (GMT +1000)
  lib/ansible/modules/extras: (detached HEAD ca310f3d15) last updated 2016/04/29 11:28:58 (GMT +1000)
  config file = 
  configured module search path = Default w/o overrides
SUMMARY
  • Allow variables that contain a dict lookup

  • Squash names built from dictionary values e.g.

    package: name="{{item.name}}-{{item.version}}"
    with_items:
    - name: hello
      version: 1.2.3
    - name: goodbye
      version: 2.3.4
    

    now works the same as

    package: name="{{item}}"
    with_items:
    - hello-1.2.3
    - goodbye-2.3.4
    
  • Fix some erroneous test assumptions e.g.

    package: name="{{pkg_mgr}}"
    with_items:
    - hello-1.2.3
    - goodbye-2.3.4
    

    should just install a single package with the name pkg_mgr (in effect item is not a bound variable)

Fixes #15649

@bcoca
Copy link
Member

bcoca commented Apr 29, 2016

I wanted to go one step further and make it configurable as shown in #12086

ignore how we went with loop_control

@willthames
Copy link
Contributor Author

@bcoca, the tests broke on loop_control with my first push, but it's now working with loop_control: { 'loop_var': 'another_name_for_item' } too.

It would be good to get the fix for #15649 backported to 2.0 and 2.1. Looks like it can be trivially cherry-picked onto stable-2.1 and reasonably easily cherry-picked onto stable-2.0.

@willthames
Copy link
Contributor Author

Also, this is mislabelled feature_pull_request.

@kustodian
Copy link
Contributor

Could we please merge this into devel, and not wait for 2.2, since this is a bugfix pull request, not a feature request. Currently in devel modules which use squashing of with loops + dict lookup are broken as it was reported in #15649 (which was closed even though it wasn't fixed).

I tested this pull request on the latest devel and it worked like a charm.

@abadger abadger modified the milestones: 2.1.0, 2.2.0 May 5, 2016
@abadger abadger added P2 Priority 2 - Issue Blocks Release bugfix_pull_request and removed feature_pull_request labels May 5, 2016
@abadger
Copy link
Contributor

abadger commented May 5, 2016

Okay, I cudgeled my brain and finally recalled the reason that we template the name field twice in the old code. Things like this are caught by the old code but not by the proposed code:

---
- hosts: localhost
  vars:
    list_of_pkgs:
      - python-q
      - python-epdb
  tasks:
  # Uninstall package and then make sure it is the latest to work around
  # improper deps inside old package
  - dnf:
      name: "{{ list_of_pkgs }}"
      state: "{{ items }}"
    with_items:
      - absent
      - latest

Templating the name field twice catches the fact that list_of_pkgs is templated to the same value in both instances of the loop. Simply comparing the templated name field to the original name field does not catch this case.

@abadger
Copy link
Contributor

abadger commented May 5, 2016

I don't think that's a deal breaker, though. I don't think the problem being solved was in the double templating so we should be able to drop that portion back into the new code without issues (with a better comment as to why we're doing things that way).

@abadger
Copy link
Contributor

abadger commented May 5, 2016

How does this new code handle things that aren't strings? For instance,

dnf:
  name: "{{ item }}"
  state: present
with_items:
  - [ "python", "python-q"]
  - [ "python3", "python3-q" ]

It looks like name will end up being

name: [ ["python", "python-q"], ["python3", "python3-q"] ]

@willthames
Copy link
Contributor Author

The old code only coped with strings - is that last use case actually a regression?

@abadger
Copy link
Contributor

abadger commented May 5, 2016

if the new code puts the items together like that, it is a regression... The old code would have skipped squashing in that case.

* Allow variables that contain a dict lookup
* Squash names built from dictionary values e.g.

  ```
  package: name="{{item.name}}-{{item.version}}"
  with_items:
  - name: hello
    version: 1.2.3
  - name: goodbye
    version: 2.3.4
  ```

  now works the same as

  ```
  package: name="{{item}}"
  with_items:
  - hello-1.2.3
  - goodbye-2.3.4
  ```

* Fix some erroneous test assumptions e.g.

  ```
  package: name="{{pkg_mgr}}"
  with_items:
  - hello-1.2.3
  - goodbye-2.3.4
  ```

  should just install a single package with
  the name pkg_mgr (in effect item is not a bound variable)

Fixes ansible#15649
Reordered some tests to better reflect comments
Fixed quite a few breaking tests (mostly where the conditional
evaluation resulted in True)
@willthames
Copy link
Contributor Author

@abadger squashing now squashes lists of lists. I've "fixed" a bunch of tests where the test was broken for the results returned (usually because the squash happened where it wasn't before, and the conditional evaluation removed items)

@sivel sivel mentioned this pull request May 10, 2016
@jimi-c jimi-c assigned abadger and unassigned jimi-c May 12, 2016
@abadger
Copy link
Contributor

abadger commented May 12, 2016

Okay, update -- will and I talked on IRC last night. I didn't think some of the test cases were right so I worked on those for a while and merged a new set today. The cleanups here don't pass the new tests so also decided to push a fix jsut for #15649. Those changes are in and cherry-picked to stable-2.1 and stable-2.0 branches.

Aside from the tests, I think we still need to template the name field twice in order to catch whether it contains a loop_var or not. Checking against the original string wouldn't catch things like a non-loop-var being templated into name.

when in doubt about the validity of squashing we should err on the side of safety and not squash. Squashing is really a dead-end feature now that every module that squashes has to take a list type (as users can just pass a list directly instead of having to use with_items to create their list).

@abadger abadger removed the P2 Priority 2 - Issue Blocks Release label May 13, 2016
@jimi-c jimi-c modified the milestones: 2.2.0, stable-2.1 Jun 24, 2016
@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bugfix_pullrequest labels Dec 13, 2016
@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Dec 15, 2016
@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html and removed needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html labels Jan 2, 2017
@ansibot
Copy link
Contributor

ansibot commented Jan 6, 2017

@willthames This PR was tested by travis-ci.org, which is no longer used. Please rebase your branch to trigger running of current tests.

click here for bot help

@willthames willthames closed this Jan 6, 2017
@willthames willthames deleted the squash_dict_value branch January 6, 2017 06:11
@mattclay mattclay moved this from Tests In Progress to Done in Testing Feb 15, 2017
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bugfix_pull_request labels Mar 5, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR.
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

yum module fails with 'dict object' has no attribute '\\x00$' when used in loop and with dict lookup
6 participants