1

I was trying to display a few commands with .stdout - the gives me the mixed output also with "item"

with item it gives me:

just the strings:

uname -a

uptime

but not the output of those commands

this is the play-book with commented stuff there that does not work:

    - name: To Display === UPTIME and KERNEL Version ===
#    - name: "To Display === {{ item }}"
      command: "{{ item }}"
      loop:
        - uname -n
        - uptime -p
        - uname -r


#        command: uname -n
#      shell: uptime -p; uname -r; rpm -qa --last kernel
#      register: OUTPUT
#    - debug: msg="{{ OUTPUT.stdout('\n') }}"
#    - debug: msg="{{OUTPUT.stdout}}"
#    - debug: var="{{OUTPUT.stdout}}"
#      shell: "{{item}}"
#      with_items: ['hostname', 'whoami']
#        - uptime -p
#
#        - uname -r
#        - rpm -qa --last kernel
...
2

If you just want to print a subset of the results of your commands on your terminal without using one or more -v flags, you can refactor your playbook to show just the standard output from each of the commands using a JSON query. For example:

---
- name: display info
  hosts: localhost
  tasks:
  - name: Display uptime and kernel info
    shell: "{{ item }}"
    with_items:
      - uptime
      - uname -n
      - uname -r
    register: output

  - debug: msg="{{ output.results | json_query('[].stdout[]') }}"

This will yield output similar to the following:

PLAY [display info] **********************************************************************************

TASK [Gathering Facts] *******************************************************************************
ok: [localhost]

TASK [Display uptime and kernel info] ****************************************************************
changed: [localhost] => (item=uptime)
changed: [localhost] => (item=uname -n)
changed: [localhost] => (item=uname -r)

TASK [debug] *****************************************************************************************
ok: [localhost] => {
    "msg": [
        "22:48  up 4 days,  3:41, 2 users, load averages: 1.26 1.39 1.42",
        "localhost.local",
        "20.3.0"
    ]
}

PLAY RECAP *******************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
1
  • Thank you very much for you great help. Michael. – Michael Feb 6 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.