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:
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 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
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.