return code from dict ?

60 views
Skip to first unread message

Nicolas G

unread,
Apr 29, 2015, 11:06:33 AM4/29/15
to ansible...@googlegroups.com
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] => error while evaluating conditional: python_result.rc == 1


Any help please ? 

Regards,
N.

Brian Coca

unread,
Apr 29, 2015, 5:50:14 PM4/29/15
to ansible...@googlegroups.com
try:
when: python_result.rc == "1"

The result might have been stringified.

--
Brian Coca

Matt Davis

unread,
Apr 29, 2015, 6:27:58 PM4/29/15
to ansible...@googlegroups.com
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

- Matt 

Nicolas G.

unread,
Apr 30, 2015, 11:28:23 AM4/30/15
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/aoVjz1FxyLQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e1cfa327-b6b5-4ef6-ba2f-f4d0f0de0e83%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages