Sunday, March 18, 2018

Ansible How to Hide Expected Stderr Output


   There are some cases the SHELL scripts we run via ansible, the return code (rc) is not 0
   And it is expected to return non-zero.

   Ansible always output stderr for you which can be annoyed.  ie

TASK [run the script  to get the result] ************************************************************************************************
fatal: []: FAILED! => {"changed": true, "cmd": ". /tmp/ --batch text", "delta": "0:00:01.688319", "end": "2018-03-19 00:36:12.670522", "failed": true, "msg": "non-zero return code", "rc": 2, "start": "2018-03-19 00:36:10.982203", "stderr": 

 We try to hide them


   Use  ignore_errors to continue the ansible yml file without disruption by the failed error

   Use  failed_when to be set as false. It will force the task never to be failed
   Example codes like

- name: run the script  to get the result
     shell: |
           . /tmp/ --batch text
     register: output
     failed_when: false
     ignore_errors: True

Before set  failed_when
"changed": true,
    "cmd": ". /tmp/ --batch text",
    "delta": "0:00:01.724031",
    "end": "2018-03-19 00:55:37.677224",
    "failed": true

After set  failed_when
"changed": true,
    "cmd": ". /tmp/ --batch text",
    "delta": "0:00:01.966887",
    "end": "2018-03-19 00:53:10.673414",
    "failed": false,
    "failed_when_result": false, 

No comments: