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 lists and dicts as msg/var parameters to debug module #31733

Open
wants to merge 10 commits into
base: devel
from

Conversation

Projects
None yet
6 participants
@gnosek
Copy link
Contributor

gnosek commented Oct 14, 2017

SUMMARY

This pull request allows lists and dicts to be passed as values of the debug module. Strictly speaking, a similar effect may be achieved using with_items, but the output is clearer when implemented natively. Please see additional information for before/after comparison:

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

plugins/action/debug.py

ANSIBLE VERSION
ansible 2.5.0 (debug-list 4b50d22be3) last updated 2017/10/14 12:09:47 (GMT +200)
  config file = None
  configured module search path = [u'/Users/blackfire/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/blackfire/src/ansible/lib/ansible
  executable location = /Users/blackfire/src/ansible/bin/ansible
  python version = 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)]
ADDITIONAL INFORMATION

Before:

  - debug:
      var: "{{ item }}"
    with_items:
    - str1_var
    - str2_var
ok: [localhost] => (item=str1_var) => {
    "item": "str1_var",
    "str1_var": "string variable 1"
}
ok: [localhost] => (item=str2_var) => {
    "item": "str2_var",
    "str2_var": "string variable 2"
}

After:

  - debug:
      var:
      - str1_var
      - str2_var
ok: [localhost] => {
    "var": {
        "str1_var": "string variable 1",
        "str2_var": "string variable 2"
    }
}

gnosek added some commits Oct 14, 2017

[debug] Use type.__name__ for more readable output
Before:
    "<type 'list'>": "VARIABLE IS NOT DEFINED!"

After:
    "list": "VARIABLE IS NOT DEFINED!"
[debug] Support lists/dicts in debug var parameter
Now the following construct is legal:

    - debug:
        var:
        - str1_var
        - str2_var

It templates all list items separately and returns them as a dict:

    ok: [localhost] => {
        "list": {
            "str1_var": "string variable 1",
            "str2_var": "string variable 2"
        }
    }

It works the same way for dicts:

    - debug:
        var:
          str1_var_label: str1_var
          str2_var_label: str2_var

Which expands into:

    ok: [localhost] => {
        "dict": {
            "str1_var_label": "string variable 1",
            "str2_var_label": "string variable 2"
        }
    }
@ansibot

This comment has been minimized.

Copy link
Contributor

ansibot commented Oct 14, 2017

@gnosek gnosek force-pushed the gnosek:debug-list branch to 8b0f40d Oct 14, 2017

@ansibot ansibot added needs_ci and removed needs_ci labels Oct 14, 2017

Fix ansible-test sanity checks
failure: The test `ansible-test sanity --test pep8` failed with the following error:

lib/ansible/plugins/action/debug.py:58:21: E731 do not assign a lambda expression, use a def

@ansibot ansibot added needs_revision and removed core_review labels Oct 14, 2017

gnosek added some commits Oct 14, 2017

[debug] Remove one nesting level in return value
Instead of returning a dict called 'list' or 'dict',
return the debug values at the top level of the result
[debug] Use consistent logic for scalars and list/dict items
Now referring to a nonexistent variable in a list/dict yields:

    ok: [localhost] => {
        "str1_var": "string variable 1",
        "str2_var": "string variable 2",
        "str3_var": "VARIABLE IS NOT DEFINED!"
    }

@ansible ansible deleted a comment from ansibot Oct 14, 2017

@dagwieers
Copy link
Member

dagwieers left a comment

I like this functionality, but leave it up to core developers to merge this.

@ansibot ansibot removed the needs_triage label Oct 14, 2017

@gnosek

This comment has been minimized.

Copy link
Contributor Author

gnosek commented Oct 14, 2017

Hmm,

The test failures at https://app.shippable.com/github/ansible/ansible/runs/40890/33/tests show usage like debug var={{ external_ip }} -- this doesn't seem legal (it would evaluate to e.g. debug var=10.0.0.1) but if it worked before, it's probably widespread enough to cause compatibility problems. I'll see what I can do about it

gnosek added some commits Oct 14, 2017

Revert "[debug] Remove one nesting level in return value"
This reverts commit c2ac764.

With `debug: var={{ a_dict_variable }}` we would have overwritten
all variables matching keys of the variable. While this isn't really
proper syntax, it's popular enough to end up in the Ansible test
suite.
Don't use debug: var={{ ... }} in tests
This has probably never worked (tried to access a weird named variable).
@ansibot

This comment has been minimized.

@ansibot ansibot added the vmware label Oct 14, 2017

@bcoca

This comment has been minimized.

Copy link
Member

bcoca commented Oct 16, 2017

@gnosek that test was incorrect, but the expression is legal, you should not fail if var points to a template.

@gnosek

This comment has been minimized.

Copy link
Contributor Author

gnosek commented Oct 19, 2017

@bcoca, the tests didn't fail. I had found the error you mention earlier and fixed it by reverting one commit. AFAIK the failures come from an OpenSUSE repo having problems. I fixed the tests as a cleanup but if you prefer to keep them as is, I can drop that commit.

@ansibot

This comment has been minimized.

Copy link
Contributor

ansibot commented May 5, 2018

@Akasurde Akasurde removed the vmware label Jun 25, 2018

@mkrizek

This comment has been minimized.

Copy link
Contributor

mkrizek commented Sep 4, 2018

@gnosek are you still interested in this PR? If so, would you mind rebasing and resolving conflicts? Thanks!

@ansibot

This comment has been minimized.

Copy link
Contributor

ansibot commented Nov 26, 2018

@ansibot

This comment has been minimized.

Copy link
Contributor

ansibot commented Feb 17, 2019

@ansibot ansibot added the utilities label Feb 17, 2019

@ansibot

This comment has been minimized.

Copy link
Contributor

ansibot commented Feb 25, 2019

@ansibot

This comment has been minimized.

Copy link
Contributor

ansibot commented Mar 13, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.