debug statement not working

30 views
Skip to first unread message

Jerry Seven

unread,
Mar 22, 2019, 3:51:38 PM3/22/19
to Ansible Project
I cant figure out why

---
- hosts: misc
  become: false
  gather_facts: yes
  tasks:
    - name: get package version
      shell: rpm -q {{ item }}
      with_items:
        - "package1"
        - "package2"
        - "package3"
      register: pkg

    - debug:
        msg: "{{ pkg.stdout }}"


FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'stdout'\n\nThe error appears to have been in '/opt/ansible/foo.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - debug:\n      ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: 'dict object' has no attribute 'stdout'"}

Jonathan Lozada De La Matta

unread,
Mar 22, 2019, 3:58:08 PM3/22/19
to ansible...@googlegroups.com
use quotes inside the shell "rpm -q {{ item }}" also you can list for for rpms using the yum module.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6670d74d-dcb1-4120-8e26-e14718d329bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Jonathan lozada de la matta

AUTOMATION PRACTICE



 

Matt Martz

unread,
Mar 22, 2019, 4:02:15 PM3/22/19
to ansible...@googlegroups.com
When you do a loop on a task the registered variable structure changes. Try just debugging `pkg` to see the structure.  You will effectively have `pkg.results`

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6670d74d-dcb1-4120-8e26-e14718d329bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Matt Martz
@sivel
sivel.net

Felix Fontein

unread,
Mar 22, 2019, 4:03:24 PM3/22/19
to ansible...@googlegroups.com
Hi,
note that you're using a loop for the "get package version" task. The
structure of pkg is different because of that; in particular, pkg has
no attribute stdout (as the error message tells you). You could output
the whole content of pkg with debug to see that.

Cheers,
Felix

Jerry Seven

unread,
Mar 25, 2019, 1:12:39 PM3/25/19
to ansible...@googlegroups.com
So I update debug to:

      register: pkg

    - debug:
        msg: "{{ pkg.results }}"


and stdout is printed:

ok: [hostname] => {
    "msg": [
        {
            "_ansible_ignore_errors": null, 
            "_ansible_item_result": true, 
            "_ansible_no_log": false, 
            "_ansible_parsed": true, 
            "changed": true, 
            "cmd": "rpm -q TaniumClient", 
            "delta": "0:00:00.024756", 
            "end": "2019-03-25 13:10:34.751439", 
            "failed": false, 
            "invocation": {
                "module_args": {
                    "_raw_params": "rpm -q TaniumClient", 
                    "_uses_shell": true, 
                    "chdir": null, 
                    "creates": null, 
                    "executable": null, 
                    "removes": null, 
                    "stdin": null, 
                    "warn": true
                }
            }, 
            "item": "TaniumClient", 
            "rc": 0, 
            "start": "2019-03-25 13:10:34.726683", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "TaniumClient-6.0.314.1442-1.rhe7.x86_64", 
            "stdout_lines": [
                "TaniumClient-6.0.314.1442-1.rhe7.x86_64"
            ]
        }, 
     

but

    - debug:
        msg: "{{ pkg.results.stdout }}"

fails.  Seems like I should be able to print that.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Jerry Seven

unread,
Mar 25, 2019, 1:20:57 PM3/25/19
to ansible...@googlegroups.com
I found this link which works:


I just dont understand why it works:

- debug:
  msg: "{{ echo.results|map(attribute='stdout_lines')|list }}"

 

Sebastian Meyer

unread,
Mar 26, 2019, 11:31:01 AM3/26/19
to ansible...@googlegroups.com, Jerry Seven
Hi Jerry,

On 25.03.19 18:12, Jerry Seven wrote:
> ok: [hostname] => {
> "msg": [

^ the [ tells you this is an array. This happens because you call
register on a task with a loop. For each item of the loop, a new dict
gets added to this array.

> {
You could print {{ pkg.results.0.stdout }} for the first element of the
loop, or {{ pkg.results.1.stdout }} for the second element of the loop.


> I just dont understand why it works:
>
> - debug:
> msg: "{{ echo.results|map(attribute='stdout_lines')|list
> }}"

What this does is, take the array of dictionaries, and look in each
dictionary for the key stdout_lines. Then take the value of that key and
make a list out of it.

HTH
Sebastian

--
Sebastian Meyer
Linux Consultant & Trainer
Mail: me...@b1-systems.de

B1 Systems GmbH
Osterfeldstraße 7 / 85088 Vohburg / http://www.b1-systems.de
GF: Ralph Dehner / Unternehmenssitz: Vohburg / AG: Ingolstadt,HRB 3537
Reply all
Reply to author
Forward
0 new messages