failed_when with a service vs a command


I want to ensure a list of services is disabled if they exist on a system. By default, many of these services won’t exist, but in cases where the service is installed, I want it disabled.

If a service doesn’t exist, ansible throws an error. While I can continue the task with ignore_error, I’m not comfortable with that because it not only masks a real problem, but the error is still shown in the ansible output, and I’d prefer the team not get in the habit of ignoring errors.

I’ve tried using failed_when, but I don’t seem to be able to get it working with a service, and all the examples use command, not service. Here’s the task–disable_services is a list declared elsewhere.

It fails with the following output:

I have tried registering the variable service_output, and checking to see that “service not loaded” not in service_output.stderr, but have the same error.

Is it possible to used failed_when on a service, and if so, what Layer 8 problem am I experiencing here?


There are a few problems:

  1. It’s state, not status.
  2. You need to use register to bind the result to variable, and then use that variable in failed_when.
  3. Minor thing: you only check for one kind of failure (it should be OK in this instance, but it’s something to keep in mind).

This should work:

And a note about how I got this code working:
I’ve used ignore_errors and debug: var=stop_disable_services to find what service returns on failure. The most interesting part were the items of stop_disabled_services.results list, as that’s the value of stop_disable_services the failed_when will see.
From there it was just some Jinja2 to make sure only some failures get ignored.

Leave a Reply