Ansible return code ok or ko

Hey,

I want to know if possible that Ansible, when he finish deployment, he display return code.

I d’ont know the value of this return code but i want a return code.

For exemple :

0 → The deploiement it’s OK
1 → The deploiement is’ KO

I don’t know if exist one module for this problem …

Someone have idea please Community Ansible !!? :slight_smile:

Thanks very much !!! ^^

Regards,

If any of your tasks fail, you will get a non-zero return code when ansible-playbook runs.
So you can wrap your ansible-playbook in a bash script if you need to detect if the playbook ran ok

this one fails

jon@TENSY ~ $ ansible-playbook -i does_not_exist playbook_doesnt_exist.yml
ERROR! the playbook: playbook_doesnt_exist.yml could not be found
jon@TENSY ~ $ echo $?
1

this one works

jon@TENSY ~ $ ansible-playbook filetest.yml --list-tasks

playbook: filetest.yml

play #1 (10,): 10, TAGS:
tasks:
COPY FILE FOR CASE TAGS:
stat the file before test TAGS:
win file test TAGS:
stat the file after test TAGS:
jon@TENSY ~ $ echo $?
0

Hope this helps,

Jon

Hey Jon,

Thanks for your help but i don’t understand.
If my first question is so complexe, i want other solution.

I want get value of play_recap, when my playbook is finish, i want get value of “skipped=0” or “Changed=5” …

Do you understand my question ??

Thanks very much community ansible !!! :slight_smile:

Regards,

Karther

hey,

More exemple:

This task not works but i want this goal :

  • name: “Display state end of playbook”
    debug:
    msg: “{{ inventory_hostname.changed }}”

or inventory_hostname.skipped or inventory_hostname.failed

I want get vlue of play_recap !!!?

Someone can help me please guy !!

Thanks very much,

Regards,

Karther

So there’s probably several ways to do that.

You could parse the ansible output and look for the things you are interested in in a wrapper script.
You might find that is fragile though. What will happen when you have multiple plays in a playbook, or if you need to run more than one playbook to acheive something.

Another way would be to write your own callback plugin. See https://docs.ansible.com/ansible/latest/plugins/callback.html This would let you run some code whenever the play statistics are generated.

Another way to solve this would be to use one of the many existing tools that are intended for runnning ansible and reacting to the run state of playbooks. Ansible Tower being the commercial option, but depending on your needs you might use something else. Things I have heard of are ARA (Ansible Run Analysis), Semaphore, AWX. There are also existing tools like Jenkins and Rundeck which have some integration with ansible that might suit you.

Hope this helps,

Jon