Ansible Conditionals: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#loops-and-conditionals
=Internal=
=Internal=
* [[Ansible Concepts#when|Ansible Concepts]]
* [[Ansible Concepts#when|Ansible Concepts]]
Line 29: Line 31:
=Expressions=
=Expressions=
==Undefined Variables==
==Undefined Variables==
{{Internal|Ansible_Concepts#Undefined_Variables|Undefined Variables}}
Use <code>is defined</code>, <code>is not defined</code>. Do not attempt to dereference with "&#123;{ ... }}" until it is established the variable is defined:
<syntaxhighlight lang='yaml'>
- name: Test whether a variable is not defined
  debug:
    msg: "some_var is not defined"
  when: some_var is not defined
</syntaxhighlight>
<syntaxhighlight lang='yaml'>
- name: Test whether a variable is defined
  debug:
    msg: "some_var is defined"
  when: some_var is defined
</syntaxhighlight>
==Logical Operators==
==Logical Operators==
Parentheses can be used in expressions.
Parentheses can be used in expressions.
Line 45: Line 62:
===<tt>>=</tt>===
===<tt>>=</tt>===
===<tt>==</tt>===
===<tt>==</tt>===
<syntaxhighlight lang='yaml'>
- name: "info"
  when: java.vendor == "Amazon" and java.version == 11
  debug:
    msg: "Java vendor is Amazon, Java version is 11"
</syntaxhighlight>

Latest revision as of 00:27, 17 December 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

Undefined Variables

Use is defined, is not defined. Do not attempt to dereference with "{{ ... }}" until it is established the variable is defined:

- name: Test whether a variable is not defined
  debug:
    msg: "some_var is not defined"
  when: some_var is not defined
- name: Test whether a variable is 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"

<

<=

>

>=

==

- name: "info"
  when: java.vendor == "Amazon" and java.version == 11
  debug:
    msg: "Java vendor is Amazon, Java version is 11"