Skip task if variable isn't set?

87 views
Skip to first unread message

Samnang Sen

unread,
Jun 26, 2015, 11:49:14 AM6/26/15
to ansible...@googlegroups.com
I have a task that installs PHP and Perl modules against a group of servers. Servers w/ the word "batch" in their hostname run both Perl and PHP tasks (remove Perl tasks for simplicity). It seems the "Set PHP status success" task runs against machines w/ the word "batch" in them. ANy way I can skip it?

- name: Install PHP modules
  command: sudo rpm -Uvh {{ rpm_repository }}/{{ item.key }}-{{ item.value.svn_tag }}.rpm --force
  with_dict: deploy_modules_php
  register: php_command_result
  when: "'batch' in ansible_hostname"
  ignore_errors: True

- name: set PHP status fail
  when: php_command_result|failed
  set_fact:
    color="red"
    msg="Unable to install PHP RPMs!"

- name: Set PHP status success
  when: php_command_result|success
  set_fact:
    color="green"
    msg="Successfully installed PHP RPMs!"

Brian Coca

unread,
Jun 26, 2015, 12:18:16 PM6/26/15
to ansible...@googlegroups.com
when runs for EACH item from with_items, so you cannot use it to avoid
undefined with_items, but you can do this:

with_dict: deploy_modules_php|default({})

which will return an empty dict, which will also skip the loops
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, 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/7d4e229d-bfa6-408f-a796-577e381a59d3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Samnang Sen

unread,
Jun 26, 2015, 12:53:33 PM6/26/15
to ansible...@googlegroups.com
But the dictionary isn't empty. I populate it for hosts w/ the word "batch."

When it does run against a server w/o the word batch, "Install PHP modules" is skipped because of the when.

- name: Install PHP modules
  command: sudo rpm -Uvh {{ rpm_repository }}/aria-{{ item.key }}-{{ item.value.svn_tag }}.rpm --force
  with_dict: deploy_modules_php
  register: php_command_result
  when: "'batch' in ansible_hostname"
  ignore_errors: True

Brian Coca

unread,
Jun 26, 2015, 1:23:58 PM6/26/15
to ansible...@googlegroups.com
sorry, then I don't understand the question.


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages