Ansible Conditionals: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 6: Line 6:


Tasks can be conditionally executed based on [[#Fact|facts]], [[#Registered_Variables|registered variables]] or playbook or [[#Inventory_File_Variables|inventory]] variables. The conditional is introduced by the task configuration keyword <code>when:</code>.
Tasks can be conditionally executed based on [[#Fact|facts]], [[#Registered_Variables|registered variables]] or playbook or [[#Inventory_File_Variables|inventory]] variables. The conditional is introduced by the task configuration keyword <code>when:</code>.
The variables must not be enclosed in <code>{{ ... }}</code> when used in <code>when:</code> expressions.
<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>
- name: Download Amazon Corretto
- name: Download Amazon Corretto
Line 23: Line 26:
     - Start Nginx
     - Start Nginx
</syntaxhighlight>
</syntaxhighlight>
=Logical Operators=
=Logical Operators=
Parentheses can be used in expressions.
Parentheses can be used in expressions.
==<code>and</code>==
==<code>and</code>==
==<code>or</code>==
==<code>or</code>==

Revision as of 07:26, 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

Logical Operators

Parentheses can be used in expressions.

and

or