2

I have the following playbook:

---
- name: Find sudo mit PPID 1
  hosts: solaris
  become: true

  tasks:
    - shell: "pgrep -P1 -x sudo | xargs -n1 ptree"
      register: result
    - debug: var=result.stdout_lines

The command ansible-playbook quotes the standard output of the command in JSON syntax:

ok: [gggggggg] => {
    "result.stdout_lines": [
        "26982 sudo su -", 
        "  26983 -sh", 
        "    6628  zlogin NNNNNNNN", 
        "      6629  -sh"
    ]
}

This is pretty unreadable for me.

Is it possible to write stdout without JSON quoting as it is done by ad-hoc commands?

gggggggg | SUCCESS | rc=0 >>
17589 sudo su -
  17590 -sh
    17613 zlogin NNNNNNNN
      17614 -sh
        17646 bash
  • No, but you could instead write the output to a file. – larsks Sep 1 '16 at 13:50
  • @larsks Can I use file descriptor 2 as the file? – ceving Sep 2 '16 at 15:51
  • You can use results.stdout – Tim Fletcher Sep 20 '16 at 20:33
  • @TimFletcher No I can't. It is even less readable. – ceving Sep 21 '16 at 20:03
1

Try using the to_nice_json filter:

tasks:
- shell: "pgrep -P1 -x sudo | xargs -n1 ptree"
  register: result
- debug: var="{{ result.stdout_lines | to_nice_json }}"
| improve this answer | |

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.