command and shell: add unchanged=True

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

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

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

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.

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.

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.

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.

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.

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

regards,
G.

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.

Reading the subject again, here’s your answer:

  • shell: foo
    changed_when: False

perfect, thanks Michael