# command and shell: add unchanged=True

**URL:** <https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325>\
**Category:** Ansible Project\
**Created:** [February 10, 2013, 5:01am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325 "2013-02-10T05:01:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 10, 2013, 5:01am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/1 "2013-02-10T05:01:57Z")

</div>

I have a task to clean up unneeded Debian packages after installing a system:

- name: apt-get clean  
&nbsp;&nbsp;&nbsp;action: command apt-get clean

I don't need this task to report as changing the system, and if ansible doesn't report it as changed, then I could get my recap to show 0, which would be nice, since if its non-zero, then I know something real was changed.

So what about adding some new option to command and shell that would have the task appear as no change? What would it be called, ignore\_changes, never\_changes, unchanged???

Blair

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 10, 2013, 6:19am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/2 "2013-02-10T06:19:34Z")

</div>

Or better yet, provide a regex to determine if it changed. Let's say for an apt-get dist-upgrade task to see if any packages were added:

&nbsp;&nbsp;&nbsp;# Need to set DEBIAN\_FRONTEND=noninteractive otherwise whiptail  
&nbsp;&nbsp;&nbsp;# can ask post-install questions that block the upgrade waiting  
&nbsp;&nbsp;&nbsp;# for shell input, e.g. libc asking if it should restart services  
&nbsp;&nbsp;&nbsp;# that depend upon it.  
&nbsp;&nbsp;&nbsp;- name: DEBIAN\_FRONTEND=noninteractive apt-get -y dist-upgrade  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: shell DEBIAN\_FRONTEND=noninteractive apt-get -y dist-upgrade  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: apt\_get\_contents  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed\_if: '"""${apt\_get\_contents.stdout}""".find("0 upgraded") == -1'

I'm using register here just to show conceptually how this would work, but the stdout and stderr could be provided using pre-determined variables.

Blair

---

<div class="post-metadata">

**Author:** ![Seth\_Vidal](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@Seth\_Vidal](https://forum.ansible.com/u/Seth_Vidal)\
**Post date:** [February 10, 2013, 6:45am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/3 "2013-02-10T06:45:20Z")

</div>

this feels like the wrong way to solve this problem.

If you want dist-upgrade to work - then write that functionality into the apt module. By doing it with regexes you are:  
&nbsp;&nbsp;1. counting on screen scraping  
&nbsp;&nbsp;2. faking idempotence

-sv

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 10, 2013, 2:45pm UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/4 "2013-02-10T14:45:07Z")

</div>

People have asked to be able to specify a list of error codes that  
would be treated as change values, which I'd accept,but yeah, I'm not  
comfortable with a regex approach here.

It seems you want to just use "ignore\_errors: True"

not sure what you are processing that needs the changed bit.

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 11, 2013, 7:02am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/5 "2013-02-11T07:02:08Z")

</div>

Yes, in this case, adding a ‘dist-upgrade’ option that would set the changed flag would be useful. I would be nice to also find out if a reboot is required, which the module could report.

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 11, 2013, 7:02am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/6 "2013-02-11T07:02:57Z")

</div>

dist-upgrade doesn’t return a failure if there’s nothing to upgrade. I just want to know if the OS was changed.

After thinking about it more, having a more general changed\_if that would default to ‘True’, and then people could use any code they like, e.g. ‘False’ to ignore changes caused by ‘apt-get clean’ or to find if ‘apt-get dist-upgrade’ changed something.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 11, 2013, 1:08pm UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/7 "2013-02-11T13:08:51Z")

</div>

We've talked about changed\_if and so on.

I want to avoid that for now, as I think we're injecting a bit too  
much syntax, and logic like that usually belongs in a module to avoid  
extra work.

We are currently talking about being able to specify  
success/failure/changed return codes to the command module.

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [February 12, 2013, 7:30pm UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/8 "2013-02-12T19:30:06Z")

</div>

I like this. The other place where it would be useful is whenever you run a shell or a command to gather some data, for example I have one task to capture the userid of who is running the playbook:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: get user id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: whoami  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: whoami

It'd be great if there was a way to force it to never show as changed.

---

<div class="post-metadata">

**Author:** ![Guido\_Serra](https://avatars.discourse-cdn.com/v4/letter/g/f4b2a3/32.png) [@Guido\_Serra](https://forum.ansible.com/u/Guido_Serra)\
**Post date:** [February 19, 2014, 12:00am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/9 "2014-02-19T00:00:00Z")

</div>

Hi Yves,  
was there any follow up on this thread?

regards,  
G.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 19, 2014, 1:24am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/10 "2014-02-19T01:24:59Z")

</div>

There’s only a snippet of text here so I’m not sure what you are referring to.

Are you asking if the register command is implemented now?

It’s been there for quite some time.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [February 19, 2014, 1:25am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/11 "2014-02-19T01:25:24Z")

</div>

Reading the subject again, here’s your answer:

- shell: foo  
changed\_when: False

---

<div class="post-metadata">

**Author:** ![Guido\_Serra](https://avatars.discourse-cdn.com/v4/letter/g/f4b2a3/32.png) [@Guido\_Serra](https://forum.ansible.com/u/Guido_Serra)\
**Post date:** [February 19, 2014, 2:03am UTC](https://forum.ansible.com/t/command-and-shell-add-unchanged-true/12325/12 "2014-02-19T02:03:00Z")

</div>

perfect, thanks Michael
