# Getting past "non-zero return code"

**URL:** <https://forum.ansible.com/t/getting-past-non-zero-return-code/38724>\
**Category:** Ansible Project\
**Created:** [March 21, 2024, 2:00pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724 "2024-03-21T14:00:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 21, 2024, 2:00pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/1 "2024-03-21T14:00:35Z")

</div>

Good day.

I want tp parse root’s history to return the last occurrence of yum update that includes the string “disablerepo”. I would have thought that this would work:

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 21, 2024, 2:51pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/2 "2024-03-21T14:51:37Z")

</div>

You can control the failure handling from the task using  
'failed\_when', for example:

failed\_when: out is failed and not out.rc == 1

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 21, 2024, 3:11pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/3 "2024-03-21T15:11:37Z")

</div>

Thank you, Brian. Now, I want to print that output via debug, but am again running into “non-zero return code”.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 21, 2024, 4:01pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/4 "2024-03-21T16:01:24Z")

</div>

when asking for help it is useful to show the full task and -vvv  
output that includes the error (like your first post). It is hard to  
debug from just a partial message out of context.

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 21, 2024, 5:01pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/5 "2024-03-21T17:01:13Z")

</div>

Apologies.

The playbook:

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 21, 2024, 5:15pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/6 "2024-03-21T17:15:29Z")

</div>

he, you'll need the same condition for debug (or use a block) since  
debug the way you set it up returns `rc: 1` and fools it into thinking  
there is an error.  
You can also bypass the top level rc with msg={{ 'out: ' ~ out }}

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 21, 2024, 6:01pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/7 "2024-03-21T18:01:28Z")

</div>

Playbook:

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 21, 2024, 6:08pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/8 "2024-03-21T18:08:35Z")

</div>

I should have mentioned that this is the expected output, for example:

**[dyioulos@myhost ~]$ history | grep -m 1 disablerepo**  
**87 dnf install --disablerepo=somerepo [https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm](https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm)**

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 21, 2024, 6:28pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/9 "2024-03-21T18:28:35Z")

</div>

Your problem isn’t anything to do with Ansible. It’s a problem is an interaction between how history is implemented and privilege escalation via become.

To make your task work, you’ll need to change your shell script like this:

```auto
    - name: Get last yum update which includes "disablerepo"
      shell: |
         HISTFILE=/root/.bash_history
         history -n
         history | tac | grep -m 1 disablerepo
      register: out

```

Normally, HISTFILE isn’t set. The docs say it defaults to “~/.bash\_history”, and it sort of does – for a broad enough definition of who “~” references. Anyway, in my testing, it’s necessary to explicitly set it as shown above.

Simply pointing HISTFILE at the right file isn’t enough. The shell normally reads in the HISTFILE early on, but we’re well past that by line 2 of the shell script. The “history -n” command reads “_all history lines not already read from the history file and append[s] them to the history list_”. The “history list” is not the same as the history file; it’s an in-memory construct that the shell manipulates until you close the shell. It’s then that the history list is written to the file. (Or when you invoke “history -a” or “history -w”.)

Now that you’ve got the history loaded, you can do your grep. The failure that was being reported before was the result of trying to construct the pipe between an uninitialized history list and your grep. A bare history command at that spot just does nothing, but as part of a pipeline it would fail.

However, you stated you want the last yum update which includes “disablerepo”. What you would have gotten - had it not been for the failure to build a pipeline with an uninitialized history - was the _first_ command in the history which included “disablerepo”. Inserting tac in the pipeline reverses the order so your grep will match on the last occurrence.

Cheers,

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [March 21, 2024, 6:52pm UTC](https://forum.ansible.com/t/getting-past-non-zero-return-code/38724/10 "2024-03-21T18:52:06Z")

</div>

Thank you, Todd! This works:
