How to skip recursive role dependencies parsing

Hi,
I encountered a problem and cannot figure it out. I have a Ansible task to install a web app. This web app task has a role dependency to install tomcat while tomcat has a role dependency to install java. In the java task I created two files under /role/java/var to define the different java version on Solaris and Linux. I used the following code to include the yml files under /role/java/var:

  • name: Include variables for the appropriate OS family
    include_vars: “{{ ansible_os_family }}.yml”

After the code above the java task will include linux.yml and solaris.yml based on the OS:

  • name: Include RedHat-specific tasks
    include: RedHat.yml
    when: ansible_os_family == “RedHat”

  • name: Include Solaris-specific tasks
    include: Solaris.yml
    when: ansible_os_family == “Solaris”

Inside the RedHat.yml there are series tasks and one of those is looking for the java version to install:

  • name: Install Oracle JDK (RedHat)
    yum: name=“{{ item }}” state=present enablerepo=kdm
    with_items: “jdkversions[ jdkversion ][‘pkgs’]”
    register: yum_install
    ignore_errors: yes

The “jdkversions” is defined in the yml file under /role/java/var. Everything works fine if tomcat and java are installed.

If I gave a condition to the web app task not to run role dependency “tomcat” then problem is coming: it still goes to Java task and skips all the other tasks but “- name: Install Oracle JDK (RedHat)”; and then throws out error complaining that “with_items expects a list or a set” because the yml files under /role/java/var were not loaded and thus no “jdkversions” is defined.

Can anyone please tell me the rule that which task will be skipped and which one will not be skipped if the role dependencies are told not to execute? I understand that Ansible will parse all tasks regardless even I told the role dependencies not to run but if some tasks are skipped while the others are not then how to resolve it?

Thanks in advance.

Hi,

I think my first post was not clear enough. I’m simplifying the issue here:
The role “webApp” has a dependency to install java. Java installation is required only when the parameter “is_required” sets to “true”. In the java task I created two files under /role/java/vars which are conditionally included based on the target operating system (RedHat or Solaris).

Role webApp:

meta/main.yml:

  • { role: java, when: is_required }

Role Java:

tasks/main.yml:

  • name: Include variables for the appropriate OS family
    include_vars: “{{ ansible_os_family }}.yml” ---- the variable “jdkversions” is defined in the .yml (RedHat.yml and Solaris.yml)

  • name: Install Oracle JDK (RedHat)
    yum: name=“{{ item }}” state=present enablerepo=kdm
    with_items: “jdkversions[ jdkversion ][‘pkgs’]”
    register: yum_install
    ignore_errors: yes

If I set “is_required” to “false” in the web app task the problem is coming: it skipped the step to include yml files under /role/java/vars (include_vars: “{{ ansible_os_family }}.yml”) but still parsed the variable jdkversions in the task “- name: Install Oracle JDK (RedHat)” and then this task threw out error:
fatal: with_items expects a list or a set

The reason that the variable “jdkversions” is defined in vars/main.yml other than defaults/main.yml is because we don’t want it to be overwritten and the values of this variable is different based on operating systems.

Can someone please tell me how to prevent it from parsing the variables that were skipped previously?

Thanks.