# Ansible return code ok or ko

**URL:** <https://forum.ansible.com/t/ansible-return-code-ok-or-ko/31416>\
**Category:** Ansible Project\
**Created:** [October 3, 2019, 3:06pm UTC](https://forum.ansible.com/t/ansible-return-code-ok-or-ko/31416 "2019-10-03T15:06:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karther](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@Karther](https://forum.ansible.com/u/Karther)\
**Post date:** [October 3, 2019, 3:06pm UTC](https://forum.ansible.com/t/ansible-return-code-ok-or-ko/31416/1 "2019-10-03T15:06:38Z")

</div>

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 !!? 🙂

Thanks very much !!! ^^

Regards,

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 3, 2019, 4:53pm UTC](https://forum.ansible.com/t/ansible-return-code-ok-or-ko/31416/2 "2019-10-03T16:53:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Karther](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@Karther](https://forum.ansible.com/u/Karther)\
**Post date:** [October 4, 2019, 8:50am UTC](https://forum.ansible.com/t/ansible-return-code-ok-or-ko/31416/3 "2019-10-04T08:50:24Z")

</div>

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 !!! 🙂

Regards,

Karther

---

<div class="post-metadata">

**Author:** ![Karther](https://avatars.discourse-cdn.com/v4/letter/k/3ec8ea/32.png) [@Karther](https://forum.ansible.com/u/Karther)\
**Post date:** [October 4, 2019, 9:27am UTC](https://forum.ansible.com/t/ansible-return-code-ok-or-ko/31416/4 "2019-10-04T09:27:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [October 5, 2019, 8:35am UTC](https://forum.ansible.com/t/ansible-return-code-ok-or-ko/31416/5 "2019-10-05T08:35:08Z")

</div>

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](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
