If I check a remote dict item (inside a role/playbook), that may not be 
present like:

debug msg="JAVA_HOME= {{ ansible_env.JAVA_HOME }} "


Then I get the following exception:

> TASK: [java-noinstall | debug msg="JAVA_HOME= {{ ansible_env.JAVA_HOME }} 
> "] ***
> fatal: [vm] => One or more undefined variables: 'dict object' has no 
> attribute 'JAVA_HOME'
>
> FATAL: all hosts have already failed -- aborting
>

I have two workarounds:

1. create a computed variable in /vars/main.yaml:

> ---
> actual_java_home: "{{ ansible_env.JAVA_HOME if ('JAVA_HOME' in 
> ansible_env) else ''}}"
>

...and use that instead as a condition inside the role/playbook:

debug msg="JAVA_HOME= {{ actual_java_home }} "


2. check once and then register a new variable based on the result:

- name: First conditional task
>   when: {{ ansible_env.JAVA_HOME if ('JAVA_HOME' in ansible_env) else ''}}
>   register: change_it
>
  action: ... 
>
 
>
 - name: Second conditional task
>    when: not change_it|skipped
>
   action: ... 
>

Is there a better solution for that? Or those are the best practices?

-- 
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-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/00391449-407f-46b7-b855-0847fc3f599c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to