Ansible Conditionals: Difference between revisions
Jump to navigation
Jump to search
Line 30: | Line 30: | ||
==Undefined Variables== | ==Undefined Variables== | ||
{{Internal|Ansible_Concepts#Undefined_Variables|Undefined Variables}} | {{Internal|Ansible_Concepts#Undefined_Variables|Undefined Variables}} | ||
Do not attempt to dereference with "{{ ... }}" until it is established the variable is defined: | |||
<syntaxhighlight lang='yaml'> | |||
- fail: msg="'some_var' is not defined" | |||
when: some_var is not defined | |||
- debug: msg="'some_var' is defined" | |||
when: some_var is defined | |||
</syntaxhighlight> | |||
==Logical Operators== | ==Logical Operators== |
Revision as of 20:30, 5 July 2021
External
Internal
Overview
Tasks can be conditionally executed based on facts, registered variables or playbook or inventory variables. The conditional is introduced by the task configuration keyword when:
.
The variables must not be enclosed in Template:...
when used in when:
expressions.
- name: Download Amazon Corretto
get_url:
url: https://corretto.aws/downloads/latest/amazon-corretto-11-x64-macos-jdk.pkg
dest: /tmp
when: java_vendor == "Amazon"
- name: Install Nginx
apt:
pkg: nginx
state: installed
update_cache: true
when: ppastable|success
notify:
- Start Nginx
Expressions
Undefined Variables
Do not attempt to dereference with "Template:..." until it is established the variable is defined:
- fail: msg="'some_var' is not defined"
when: some_var is not defined
- debug: msg="'some_var' is defined"
when: some_var is defined
Logical Operators
Parentheses can be used in expressions.
and
or
not
- name: "info"
when: not java_updated
debug:
msg: "Java was NOT updated"