Juliano DeCarvalho
Juliano DeCarvalho

Reputation: 23

Setting exit code from an Ansible callback plugin

I am writing an Ansible 2.x callback plugin, and I would like to be able to fail the current playbook with an non-zero exit code based on some conditions in the v2_playbook_on_stats function.

I have tried to raise AnsibleError(), but this is caught somewhere up the chain and treated as a warning, which allows Ansible to finish with a zero exit code.

I have also tried using self._display.error(), but seems to do nothing but display an error message, and again Ansible finishes with a zero exit code.

Is there any way to do what I require? Or is a callback plugin never meant to allow the developer to change the status of a playbook to a failure?

Thank you for your time.

Upvotes: 2

Views: 788

Answers (2)

Doreen Dodoo
Doreen Dodoo

Reputation: 11

Also faced the same problem and i found out that i could use python's sys.exit(x) (x being the various exit codes) to stop or fail the playbook.

Upvotes: 1

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68339

You can't do this with callback, strategy plugin is your choice.

Subclass required plugin (e.g. linear), extend run method to return non-zero value based on your criteria, it will be translated by PlaybookExecutor and cli as program exit code.

Upvotes: 0

Related Questions