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

debug with_items prints the whole item even if using loop_control label #35493

Closed
valerio-gr opened this issue Jan 30, 2018 · 16 comments · Fixed by #36307
Closed

debug with_items prints the whole item even if using loop_control label #35493

valerio-gr opened this issue Jan 30, 2018 · 16 comments · Fixed by #36307
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@valerio-gr
Copy link

ISSUE TYPE

Bug Report

COMPONENT NAME

debug module

ANSIBLE VERSION
ansible 2.4.0.0
  config file = /home/valeriog/ANSIBLE_PROD/ansible.cfg
  configured module search path = [u'/home/valeriog/ANSIBLE_PROD/library']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Aug  2 2016, 04:20:16) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)]
CONFIGURATION

From ansible-config dump --only-changed:
"""
ANSIBLE_SSH_ARGS = -o ControlPersist=5m
DEFAULT_HASH_BEHAVIOUR = merge
DEFAULT_STDOUT_CALLBACK = skippy
HOST_KEY_CHECKING = False
RETRY_FILES_ENABLED = False
"""

OS / ENVIRONMENT

Redhat 7.3

SUMMARY

This is similar to #32947 but I'm only using loop_contorl/label (and not using loop_var).
The debug module print out on screen the whole content of each item instead of the content of the label var.

STEPS TO REPRODUCE

Save the following playbook to test.yml and run it with the command: ansible-playbook -i localhost, test.yml

---
- name: TEST LOOP CONTROL LABEL
  hosts: all
  connection: local
  gather_facts: no

  tasks:
    - name: TEST LOOP CONTROL LABEL
      debug:
        msg: "{{ item.name }}"
      with_items:
        - name: server1
          disks: 3gb
          ram: 15Gb
          network:
            nic01: 100Gb
            nic02: 10Gb     
        - name: server2
          disks: 3gb
          ram: 15Gb
          network:
            nic01: 100Gb
            nic02: 10Gb     
        - name: server3
          disks: 3gb
          ram: 15Gb
          network:
            nic01: 100Gb
            nic02: 10Gb             
      loop_control:
        label: '{{ item.name }}'
EXPECTED RESULTS

Only the name of the server is printed out on screen

ACTUAL RESULTS
PLAY [TEST LOOP CONTROL LABEL] *********************************************************************

TASK [TEST LOOP CONTROL LABEL] *********************************************************************
ok: [localhost] => (item=server1) => {
    "item": {
        "disks": "3gb", 
        "name": "server1", 
        "network": {
            "nic01": "100Gb", 
            "nic02": "10Gb"
        }, 
        "ram": "15Gb"
    }, 
    "msg": "server1"
}
ok: [localhost] => (item=server2) => {
    "item": {
        "disks": "3gb", 
        "name": "server2", 
        "network": {
            "nic01": "100Gb", 
            "nic02": "10Gb"
        }, 
        "ram": "15Gb"
    }, 
    "msg": "server2"
}
ok: [localhost] => (item=server3) => {
    "item": {
        "disks": "3gb", 
        "name": "server3", 
        "network": {
            "nic01": "100Gb", 
            "nic02": "10Gb"
        }, 
        "ram": "15Gb"
    }, 
    "msg": "server3"
}

@valerio-gr valerio-gr changed the title debug with_items prints item even if using labels debug with_items prints the whole item even if using loop_control label Jan 30, 2018
@ansibot
Copy link
Contributor

ansibot commented Jan 30, 2018

Files identified in the description:

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Jan 30, 2018

@ansibot ansibot added affects_2.4 This issue/PR affects Ansible v2.4 bug_report module This issue/PR relates to a module. 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 Jan 30, 2018
@mkrizek
Copy link
Contributor

mkrizek commented Jan 30, 2018

@valerio-gr The whole item is added to the module result (which is a case for all modules, not just debug) whether loop label is set or not, the thing that changes with loop label set is this ok: [localhost] => (item=server1) => { - only item.name is listed here and not the whole item. I don't think this is a bug.

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Jan 30, 2018
@valerio-gr
Copy link
Author

@mkrizek, Isn't that a bug?
The result should just be the output of the msg variable and not include the item itself.
Note: this example is taken pretty much straight from Ansible documentation as an example to limit what is going to be displayed but I still have the whole item displayed.

@valerio-gr
Copy link
Author

@mkrizek,
if that's not considered a bug. Can I change this to a Feature Request?
Maybe have a debug or loop control option to set (e.g. quiet: yes) to avoid the printout on screen of the item.
Basically what is printed on screen, should just be the content of the msg variable and not the items through which the loop is done (that can be include in msg if required btw).

@bcoca
Copy link
Member

bcoca commented Feb 16, 2018

testing your playbook with PR above:


ok: [localhost] => (item=server1) => {
    "msg": "server1"
}
ok: [localhost] => (item=server1) => {
    "msg": "server2"
}
ok: [localhost] => (item=server1) => {
    "msg": "server3"
}

bcoca added a commit to bcoca/ansible that referenced this issue Feb 16, 2018
bcoca added a commit that referenced this issue Feb 20, 2018
fixes #35493

updated tests

(cherry picked from commit a79378f)
bcoca added a commit that referenced this issue Feb 20, 2018
@dagwieers
Copy link
Contributor

dagwieers commented Feb 20, 2018

Would be nice to see this in v2.5 though ;-)

Ah I noticed now it is in v2.5 !

@bcoca
Copy link
Member

bcoca commented Feb 20, 2018

merged 10mins ago

bcoca added a commit that referenced this issue Mar 1, 2018
fixes #35493

updated tests

(cherry picked from commit a79378f)
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@notonlyvandalzzz
Copy link

i have 2.6.3 and it still shows whole item instead of only one specified variable. maybe i need to specify any additional parameter?

 - hosts: localhost
  tasks:
  - name: obtain login credentials
    include_vars: /etc/ansible/secrets.yml

  - name: Look up for mac on major devices
    register: result
    findmac:
      mod_ip: "{{ hostvars[item]['ansible_host'] }}"
      mod_user: "{{ creds['username'] }}"
      mod_pass: "{{ creds['password'] }}"
      mod_mlookup: "{{ ex_addr }}"
    loop: "{{ groups['admmainswitch'] }}"

  - name: Look up for port on devices
    register: result2
    findport:
      mod_ip: "{{ hostvars[item]['ansible_host'] }}"
      mod_user: "{{ creds['username'] }}"
      mod_pass: "{{ creds['password'] }}"
      mod_lookup: "{{ result['results'][0]['meta']['cisco_msays'] }}"
    loop: "{{ groups['admswitch'] }}"

  - name: Output
    debug:
      msg: "{{ item['meta']['cisco_says'] }}"
    loop: "{{ result2['results'] }}"

@AdrianDutu
Copy link

AdrianDutu commented Sep 27, 2018

@bcoca I still have this problem in 2.7.14. Do i need to add any extra parameter to debug?

@pawankkamboj
Copy link

Any update on this, I am also getting same issue with 2.6.5

@Anthonyjmartin
Copy link

I am also having this issue with 2.6.4.

@AngryNorseMan
Copy link

ansible 2.7.0

I'm still seeing this huge item output:
ok: [node1] => (item={'virtualenv': '/path/to/app', 'changed': True, 'requirements': '/path/to/app/requirements.txt', 'name': None, 'stdout': "Collecting git+http://bitbucket/scm/lib/some_lib.git@v2 (from -r /path/to/app/requirements.txt (line 39))\n Cloning http://bitbucket/scm/lib/some_lib.git (to revision v2) to ./pip-req-build-j8fphy0v\nSwitched to a new branch 'v2'\nBranch v2 set up to track remote branch v2 from origin.\nCollecting git+http://bitbucket/scm/lib/some_lib.git@v3 (from -r /path/to/app/requirements.txt (line 40))\n Cloning http://bitbucket/scm/lib/some_lib.git (to revision v3) to ./pip-req-build-errntcx9\nSwitched to a new branch 'v3'\nBranch v3 set up to track remote branch v3 from origin.\nCollecting git+http://bitbucket/scm/lib/some_lib.git@v1 (from -r /path/to/app/requirements.txt (line 41))\n Cloning http://bitbucket/scm/lib/some_lib.git (to revision v1) to ./pip-req-build-m_xq_o34\nSwitched to a new branch 'v1'\nBranch v1 set up to track remote branch v1 from origin.\nCollecting git+http:/

when running this handler:
- name: Pip - requirements output debug: msg: - "{{ item.stdout.split('\n') }}" listen: requirements with_items: - "{{ requirements }}"

Not seeing any good method for cleaning this output up and it takes up many lines in stdout, is there a solution that already exists or is that part of this issue?

@tuxillo
Copy link
Contributor

tuxillo commented Oct 20, 2018

@AngryNorseMan I use:

no_log: True

But I think it should, by default, not show all that verbose output.

@deoren
Copy link

deoren commented Feb 5, 2019

@AngryNorseMan: ansible 2.7.0

I'm still seeing this huge item output:

Same here and it was driving me bonkers. I couldn't seem to figure out why the deeply nested variable structure was printing above just the value I wanted to see via the debug module.

Like your example, I'm still using with_items as that is what I've gotten used to.

According to #47679, it appears that a custom loop label can be used along with the loop keyword to control the level of output. This seems to take care of the specific problem noted on this issue.

More info here:

https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html

EDIT: I just realized that much of what I noted here is listed in the OP. I missed that when searching via Google and landing here.

@davidmnoriega
Copy link
Contributor

Adding a note for future visitors, make sure you use debug with msg and not var.

@ansible ansible locked and limited conversation to collaborators Apr 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.4 This issue/PR affects Ansible v2.4 bug This issue/PR relates to a bug. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.