Skip task if variable isn't set?

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!”

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

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

sorry, then I don't understand the question.