0

I want to use ansible to know whether a docker stack is deployed. In the shell I'd do something like: docker stack services -q my_stack_name.

I could use the shell module, but then I have to manually handle errors and idempotence.

Is there a module for this? (I looked over the docs of docker_swarm_info but it isn't appropriate.)

lonix
  • 896
  • 10
  • 23
  • Are scripted local facts at all an option for you? – Felix Frank Nov 04 '19 at 23:33
  • @FelixFrank I just perform the work using the `shell` module, and it works. But it would be nice if there were an official approach that is less brittle. Maybe `shell` is the only option for now. – lonix Nov 05 '19 at 07:03
  • okay but that does not answer my question ;) I'll just build an answer, you can see if it applies to you. – Felix Frank Nov 08 '19 at 23:56

1 Answers1

1

You can supply a local fact that provides this information.

#!/bin/sh
# /etc/ansible/facts.d/docker_stack
echo "{\"info\": \"`docker stack services -q my_stack_name`\"}"

In your playbook, you can then use the ansible_local.docker_stack.info variable.

(Of course, it will likely more effective to make this a Python script that returns structured JSON data rather than a fugly, possibly multi-line string.)

Do note that this will not notice any changes that happen during a playbook run.

Felix Frank
  • 3,093
  • 1
  • 16
  • 22