Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see stdout,stderr of Ansible commands so as to be human readable?

Tags:

stdout

ansible

I have created an Ansible task for deploying apache on my remote Ubuntu machine.The task is working fine.But i want to ensure that the stdout/err of the commands that ansible is running in the remote host is captured and printed on the stdout of when running ansible script.I have tried something like this

 - name: a2dissite 000-default
   command: a2dissite 000-default
   register: out
 - debug: var=out.stdout_lines
 - debug: var=out.stderr_lines

Given below is what i got after running my ansible playbook

TASK [apache : a2dissite 000-default] ************************************************************************************************
changed: [ec2-host]

TASK [apache : debug] ****************************************************************************************************************
ok: [ec2-host] => {
    "out.stdout_lines": [
        "Site 000-default already disabled"
    ]
}

TASK [apache : debug] ****************************************************************************************************************
ok: [ec2-host] => {
    "out.stderr_lines": []
}

i can see the stdout and stderr for my commands .But its look weird, is there a way to just get the stdout and print it as is ?

like image 294
Pratheesh Avatar asked Dec 05 '25 16:12

Pratheesh


1 Answers

If you want a real experience for humans, you should IMO focus on error reporting and idempotency which are core principles of ansible:

  • report "changed" when something effectively changed
  • report "ok" when no changes where made
  • give a meaningful error when it happens.

Not bulletproof but just to give you the idea (wrote on spot, not tested).

---
- name: Idempotency with command demo
  hosts: localhost

  tasks:
    - block:

        - name: Disable apache default site
          command: a2dissite 000-default
          register: dissitecmd
          changed_when: >-
            "already disabled" not in dissitecmd.stdout

      rescue:

        - name: Fail nicely with error
          fail:
            msg: "The default site could not be disabled. Error: {{ dissitecmd.stderr }}
like image 142
Zeitounator Avatar answered Dec 07 '25 22:12

Zeitounator