command and shell: add unchanged=True

2,661 views
Skip to first unread message

Blair Zajac

unread,
Feb 10, 2013, 12:01:57 AM2/10/13
to ansible...@googlegroups.com
I have a task to clean up unneeded Debian packages after installing a
system:

- name: apt-get clean
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

Blair Zajac

unread,
Feb 10, 2013, 1:19:34 AM2/10/13
to ansible...@googlegroups.com
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:

# Need to set DEBIAN_FRONTEND=noninteractive otherwise whiptail
# can ask post-install questions that block the upgrade waiting
# for shell input, e.g. libc asking if it should restart services
# that depend upon it.
- name: DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
action: shell DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
register: apt_get_contents
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

Seth Vidal

unread,
Feb 10, 2013, 1:45:20 AM2/10/13
to ansible...@googlegroups.com


On Sat, 9 Feb 2013, Blair Zajac wrote:

>
> 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:
>
> # Need to set DEBIAN_FRONTEND=noninteractive otherwise whiptail
> # can ask post-install questions that block the upgrade waiting
> # for shell input, e.g. libc asking if it should restart services
> # that depend upon it.
> - name: DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
> action: shell DEBIAN_FRONTEND=noninteractive apt-get -y dist-upgrade
> register: apt_get_contents
> changed_if: '"""${apt_get_contents.stdout}""".find("0 upgraded") == -1'
>


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:
1. counting on screen scraping
2. faking idempotence

-sv

Michael DeHaan

unread,
Feb 10, 2013, 9:45:07 AM2/10/13
to ansible...@googlegroups.com
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.

Blair Zajac

unread,
Feb 11, 2013, 2:02:08 AM2/11/13
to ansible...@googlegroups.com

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.

Blair Zajac

unread,
Feb 11, 2013, 2:02:57 AM2/11/13
to ansible...@googlegroups.com

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.
 

Michael DeHaan

unread,
Feb 11, 2013, 8:08:51 AM2/11/13
to ansible...@googlegroups.com
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.
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Yves Dorfsman

unread,
Feb 12, 2013, 2:30:06 PM2/12/13
to ansible...@googlegroups.com
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:

- name: get user id
command: whoami
register: whoami

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


--
Yves. http://www.SollerS.ca/
Unix/Linux and Python specialist in Calgary.
http://blog.zioup.org/

Guido Serra

unread,
Feb 18, 2014, 7:00:00 PM2/18/14
to ansible...@googlegroups.com


On Tuesday, February 12, 2013 11:30:06 AM UTC-8, Yves Dorfsman wrote:

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:

     - name: get user id
       command: whoami
       register: whoami

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

regards,
G. 

Michael DeHaan

unread,
Feb 18, 2014, 8:24:59 PM2/18/14
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
Feb 18, 2014, 8:25:24 PM2/18/14
to ansible...@googlegroups.com
Reading the subject again, here's your answer:

- shell: foo
  changed_when: False


Guido Serra

unread,
Feb 18, 2014, 9:03:00 PM2/18/14
to ansible...@googlegroups.com
perfect, thanks Michael

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/N7bmBaAbcOA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages