# return code from dict ?

**URL:** <https://forum.ansible.com/t/return-code-from-dict/18125>\
**Category:** Ansible Project\
**Created:** [April 29, 2015, 3:06pm UTC](https://forum.ansible.com/t/return-code-from-dict/18125 "2015-04-29T15:06:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [April 29, 2015, 3:06pm UTC](https://forum.ansible.com/t/return-code-from-dict/18125/1 "2015-04-29T15:06:33Z")

</div>

Hi, I have a plabyook that checks if my python app has been installed and only run pip install if the packages have not been installed.

Unfortunately because I’m doing a check using with\_items the register value is a dictionary causing an error on the next when: statement when evaluating the conditional..

# CODE:

- name: check if {{ item }} have been previously installed  
shell: python -c “import {{ item }}”  
register: python\_result  
ignore\_errors: True  
with\_items:

- app1

- app2

- name: pip install {{ item }}  
pip: requirements=/packages/{{ item }}/requirements.txt  
sudo\_user: deploy  
with\_items:

- app1

- app2  
when: python\_result.rc == 1

# Error output running with full verbosity (-vvvvvvv) :

TASK: [pip install {{ item }}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [[server.example.co](http://server.example.co)] =\> error while evaluating conditional: python\_result.rc == 1

Any help please ?

Regards,  
N.

---

<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 29, 2015, 9:50pm UTC](https://forum.ansible.com/t/return-code-from-dict/18125/2 "2015-04-29T21:50:10Z")

</div>

try:  
when: python\_result.rc == "1"

The result might have been stringified.

---

<div class="post-metadata">

**Author:** ![Matt\_Davis](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matt\_Davis](https://forum.ansible.com/u/Matt_Davis)\
**Post date:** [April 29, 2015, 10:27pm UTC](https://forum.ansible.com/t/return-code-from-dict/18125/3 "2015-04-29T22:27:58Z")

</div>

The pip module is smart- it shouldn’t re-run the installation if the requested packages are already there. So you shouldn’t need the extra task that checks first at all.

If for some reason you still want to do it this way, the reason your conditional is failing is because you’re running the previous item in a loop using with\_items- the registered result variable looks different (as it contains the results from each loop iteration). See [http://docs.ansible.com/playbooks\_loops.html#using-register-with-a-loop](http://docs.ansible.com/playbooks_loops.html#using-register-with-a-loop) for details.

To get the behavior you want, you need to find the appropriate result in the list and conditionally run the pip module based on its return code. Since you’re using the same items in both lists, and Ansible keeps the original items correlated with the results, might I suggest:

- name: pip install {{ item }}  
pip: requirements=/packages/{{ item.item }}/requirements.txt # this uses the “item” value that got stored alongside the result from your first task  
sudo\_user: deploy  
with\_items: python\_result.results # this loops over the registered results from your first task  
when: item.rc == 1 # this uses the correlated stored return code from your first task

- Matt

---

<div class="post-metadata">

**Author:** ![Nicolas\_G](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@Nicolas\_G](https://forum.ansible.com/u/Nicolas_G)\
**Post date:** [April 30, 2015, 3:27pm UTC](https://forum.ansible.com/t/return-code-from-dict/18125/4 "2015-04-30T15:27:58Z")

</div>

Brian/Matt, thank you for the help.

Unfortunately in my case the pip module is not that smart to know if the application is installed but maybe it’s because how our developer have written the code.

Regards,  
N.
