AhmFM
AhmFM

Reputation: 1782

ansible command module result code is non zero for a success response

Team,

unable to catch what is the issue and i have tried command as well as shell module. actual command executes fine but am not sure why it says stderr?

task


      - name: "List out PROC Stats on DGX/GPU Nodes Nodes"
        shell: "cat {{ proc_stats }} | grep FS"
        ignore_errors: no
        register: proc_stats
        delegate_to: "{{ item }}"
        with_items: "{{ groups['gpu_node'] }}"
        failed_when: proc_stats.rc != 0

output

 TASK [services-pre-install-checks : List out PROC Stats on DGX/GPU Nodes Nodes] ***
16:21:57  Wednesday 06 November 2019  00:21:57 +0000 (0:00:03.628)       0:00:19.398 **** 
16:21:59  failed: [localhost -> hostA.test.metal.net] (item=hostA) => {"ansible_loop_var": "item", "changed": true, "cmd": ["cat", "/proc/fs/fscache/stats", "|", "grep", "FS"], "delta": "0:00:00.106478", "end": "2019-11-06 00:21:59.372137", "failed_when_result": true, "item": "hostA", "msg": "non-zero return code", "rc": 1, "start": "2019-11-06 00:21:59.265659", "stderr": "cat: '|': No such file or directory\ncat: grep: No such file or directory\ncat: FS: No such file or directory", "stderr_lines": ["cat: '|': No such file or directory", "cat: grep: No such file or directory", "cat: FS: No such file or directory"], "stdout": "FS-Cache statistics(ver:1.0)\nCookies: idx=613 dat=520 spc=0\nObjects: alc=3 nal=0 avl=3 ded=4\nChkAux : non=0 ok=1 upd=0 obs=0\nPages  : mrk=1 unc=1\nAcquire: n=1133 nul=0 noc=0 ok=1133 nbf=0 oom=0\nLookups: n=3 neg=2 pos=1 crt=2 tmo=0\nInvals : n=0 run=0\nUpdates: n=0 nul=0 run=0\nRelinqs: n=1133 nul=0 wcr=0 rtr=0\nAttrChg: n=0 ok=0 nbf=0 oom=0 run=0\nAllocs : n=0 ok=0 wt=0 nbf=0 int=0\nAllocs : ops=0 owt=0 abt=0\nRetrvls: n=520 ok=0 wt=1 nod=1 nbf=519 int=0 oom=0\nRetrvls: ops=1 owt=1 abt=0\nStores : n=1 ok=1 agn=0 nbf=0 oom=0 wrxd=0 sol=0\nStores : ops=1 run=2 pgs=1 rxd=1 irxd=0 olm=0 ipp=0\nVmScan : nos=0 gon=0 bsy=0 can=0 wt=0\nOps    : pend=1 run=2 enq=2 can=0 rej=0\nOps    : ini=2 dfr=0 rel=2 gc=0\nCacheOp: alo=0 luo=0 luc=0 gro=0\nCacheOp: inv=0 upo=0 dro=0 pto=0 atc=0 syn=0\nCacheOp: rap=0 ras=0 alp=0 als=0 wrp=0 ucp=0 dsp=0\nCacheEv: nsp=0 stl=0 rtr=0 cul=0", "stdout_lines": ["FS-Cache statistics(ver:1.0)", "Cookies: idx=613 dat=520 spc=0", "Objects: alc=3 nal=0 avl=3 ded=4", "ChkAux : non=0 ok=1 upd=0 obs=0", "Pages  : mrk=1 unc=1", "Acquire: n=1133 nul=0 noc=0 ok=1133 nbf=0 oom=0", "Lookups: n=3 neg=2 pos=1 crt=2 tmo=0", "Invals : n=0 run=0", "Updates: n=0 nul=0 run=0", "Relinqs: n=1133 nul=0 wcr=0 rtr=0", "AttrChg: n=0 ok=0 nbf=0 oom=0 run=0", "Allocs : n=0 ok=0 wt=0 nbf=0 int=0", "Allocs : ops=0 owt=0 abt=0", "Retrvls: n=520 ok=0 wt=1 nod=1 nbf=519 int=0 oom=0", "Retrvls: ops=1 owt=1 abt=0", "Stores : n=1 ok=1 agn=0 nbf=0 oom=0 wrxd=0 sol=0", "Stores : ops=1 run=2 pgs=1 rxd=1 irxd=0 olm=0 ipp=0", "VmScan : nos=0 gon=0 bsy=0 can=0 wt=0", "Ops    : pend=1 run=2 enq=2 can=0 rej=0", "Ops    : ini=2 dfr=0 rel=2 gc=0", "CacheOp: alo=0 luo=0 luc=0 gro=0", "CacheOp: inv=0 upo=0 dro=0 pto=0 atc=0 syn=0", "CacheOp: rap=0 ras=0 alp=0 als=0 wrp=0 ucp=0 dsp=0", "CacheEv: nsp=0 stl=0 rtr=0 cul=0"]}

Upvotes: 0

Views: 3598

Answers (2)

sorin
sorin

Reputation: 170478

When using shell you should always use bash strict mode which also sets the pipefail flag.

set -euxo pipefail

if you would have run ansible-lint on your code, you would have figured it out as it spots this common mistake.

Upvotes: 0

Matt P
Matt P

Reputation: 2625

Firstly, your output matches the use of the command module, which does indeed fail when trying to pipe command output. The solution is to use the shell module. I can see that in your question, so I can only assume you grabbed the wrong output.

The issue you will see when using the shell module is that you are assigning the result of the task to the same variable you are using as input to the shell command (proc_stats). You will need to use a different variable name for the registered output.

By the way, the failed_when condition is unnecessary. The task will fail anyway if the command's return code is not 0

Upvotes: 1

Related Questions