# Return value from Bash to Ansible

**URL:** <https://forum.ansible.com/t/return-value-from-bash-to-ansible/18052>\
**Category:** Ansible Project\
**Created:** [April 22, 2015, 2:49pm UTC](https://forum.ansible.com/t/return-value-from-bash-to-ansible/18052 "2015-04-22T14:49:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zorggy01](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zorggy01](https://forum.ansible.com/u/zorggy01)\
**Post date:** [April 22, 2015, 2:49pm UTC](https://forum.ansible.com/t/return-value-from-bash-to-ansible/18052/1 "2015-04-22T14:49:05Z")

</div>

Hello,

I’m using Ansible 1.7.2 and I’ve written a Bash module to use at a very early stage of Linux installation (Python not fully available yet). It works correctly except that the return value is seen as stdout and is not interpreted as a special value (changed, failed, skipped, ok).

**my\_playbook.yml** :

`  
[…]

- name: my action  
shell: /path/to/my\_action.sh {{ my\_arg }}  
when: my\_arg is defined  
[…]

`

**my\_action.sh** :

`  
[…]  
echo ‘ok: true’  
exit 0  
[…]

`

**Execution** :

`  
[root@my\_host] # ansible-playbook -v -l $(hostname -s) -c local /path/to/my\_playbook.yml  
[…]  
TASK: [my action] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [my\_host] =\> {“changed”: true, “cmd”: “/path/to/my\_action.sh my\_arg”, “delta”: “0:00:00.255756”, “end”: “2015-04-22 15:54:57.077314”, “rc”: 0, “start”: “2015-04-22 15:54:56.821558”, “stderr”: “”, “stdout”: “ok: true”}  
[…]

`

In the execution, you can read “ok: true” in stdout. I would like to get it instead of “changed”: true. I tried many different syntaxes to tell Ansible I had no change in the script, without any success:

`  
echo ‘{“ok”: “true”}’  
echo ‘{ok=True}’  
echo ‘ok: True’  
echo ‘ok=True’  
echo ‘ok=true’  
…

`

What did I do wrong? Thanks in advance for your explanations.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 22, 2015, 2:52pm UTC](https://forum.ansible.com/t/return-value-from-bash-to-ansible/18052/2 "2015-04-22T14:52:40Z")

</div>

shell/command/raw actions by default are always changed, you want to  
use the changed\_when clause. The return of ok/changed=true/false only  
works for modules written in shell, not for scripts called from shell  
module (FYI, there is also script module).

---

<div class="post-metadata">

**Author:** ![zorggy01](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zorggy01](https://forum.ansible.com/u/zorggy01)\
**Post date:** [April 22, 2015, 3:36pm UTC](https://forum.ansible.com/t/return-value-from-bash-to-ansible/18052/3 "2015-04-22T15:36:38Z")

</div>

Guy, you saved my day!

You are right, my script is not a module, it is just called by the **shell** module. Then using the **changed\_when** clause did the job. Here is the code.

**my\_playbook.yml** :

`  
[…]

- name: my action  
shell: /path/to/my\_action.sh {{ my\_arg }}  
register: script  
changed\_when: script.rc == “changed”  
when: my\_arg is defined  
[…]

`

**my\_action.sh** :

`  
[…]  
echo “rc=changed”  
exit 0  
[…]

`

**Execution** :

`  
[root@my\_host] # ansible-playbook -v -l $(hostname -s) -c local /path/to/my\_playbook.yml  
[…]  
TASK: [my action] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [my\_host] =\> {“changed”: false, “cmd”: “/path/to/my\_action.sh 2015-04-05”, “delta”: “0:00:00.213521”, “end”: “2015-04-22 17:17:54.159018”, “rc”: 0, “start”: “2015-04-22 17:17:53.945497”, “stderr”: “”, “stdout”: “”, “stdout\_lines”: }

`

Thank you very much, Brian!

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [April 22, 2015, 3:52pm UTC](https://forum.ansible.com/t/return-value-from-bash-to-ansible/18052/4 "2015-04-22T15:52:20Z")

</div>

I don't think this is right, script.rc reflects the return code, which is an integer in the range 0 and 255. In your case this is 0 because you did a "exit 0". So it can never be "changed".

Why not make your script into an Bash ansible module and have it return "changed": true ? Here is a simple example of how to achieve this:

The bash module:
