Ansible Conditional Programming with Playbooks

Example 1 - Using When Condition

# note that Ansible facts and vars like ansible_os_family can be used
    # directly in conditionals without double curly braces
    tasks:
      - name: "shut down Debian flavored systems"
        command: /sbin/shutdown -t now
        when: ansible_os_family == "Debian"

Example 2 - Using When Condition

# You can also use parentheses to group conditions:
    tasks:
      - name: "shut down CentOS 6 and Debian 7 systems"
        command: /sbin/shutdown -t now
        when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
              (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")

Example 3 - Using When Condition

# Multiple conditions that all need to be true (a logical �and�) can also be specified as a list:
    tasks:
      - name: "shut down CentOS 6 systems"
        command: touch /opt/verizon.txt
        when:
          - ansible_distribution == "RedHat"
          - ansible_distribution_major_version == "7.5"

Example 4 - Using When Condition

#If a required variable has not been set, you can skip or fail using Jinja2�s defined test. For example:
    tasks:
        - shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
          when: foo is defined
    
        - fail: msg="Bailing out. this play requires 'bar'"
          when: bar is undefined

Example 5 - Using When Condition

# Following is the simplest example of checking whether the value of a variable. We have created a variable test1 and checking if the value is �Hello World� using the when statement.  
    - hosts: all
      vars:
        test1: "Hello World"
      tasks:
      - name: Ansible when variable equals example
        debug:
          msg: "Equals"
        when: test1 == "Hello World"

Example 6 - Using When Condition

# We can also set the reverse, i.e. if the variable is not equal to another value.
    - hosts: all
      vars:
        test1: "Bye World"
      tasks:
      - name: Ansible when variable not equals example
        debug:
          msg: "Not Equals"
        when: test1 != "Hello World"

Example 7 - Using When Conditio

# We can also make a conditional statement based on whether the variable contains a particular string. 
    - hosts: all
      vars:
        test1: "Bye World"
      tasks:
      - name: Ansible when variable contains string example example
        debug:
          msg: "Equals"
        when: test1.find("World") != -1

Example 8 - Using When Condition

#If the variable value is registered from a shell command you may have to use stdout.find to check the content.
    - hosts: all
      tasks:
      - shell: cat /etc/temp.txt
        register: output
      - name: Ansible when variable contains string example example
        debug:
          msg: "Equals"
        when: output.stdout.find("World") != -1

Example 9 - Using When Condition

# We can also check if a variable is empty using similar manner.
    - hosts: all
      tasks:
      - shell: cat /etc/temp.txt
        register: output
      - name: Ansible when variable is empty example
        debug:
          msg: "empty"
        when: output.stdout == ""

Example 9 - Using When Condition

---
# this is a demo of conditional executions using 'when' statements, which can skip
# certain tasks on machines/platforms/etc where they do not apply.

- hosts: all
  remote_user: root

  vars:
     favcolor: "red"
     dog: "fido"
     cat: "whiskers"
     ssn: 8675309

  tasks:

     - name: "do this if my favcolor is blue, and my dog is named fido"
       shell: /bin/false
       when: favcolor == 'blue' and dog == 'fido'

     - name: "do this if my favcolor is not blue, and my dog is named fido"
       shell: /bin/true
       when: favcolor != 'blue' and dog == 'fido'

     - name: "do this if my SSN is over 9000"
       shell: /bin/true
       when: ssn > 9000

     - name: "do this if I have one of these SSNs"
       shell: /bin/true
       when:  ssn in [ 8675309, 8675310, 8675311 ]

     - name: "do this if a variable named hippo is NOT defined"
       shell: /bin/true
       when: hippo is not defined

     - name: "do this if a variable named hippo is defined"
       shell: /bin/true
       when: hippo is defined



	

Avail Rajesh Kumar as trainer at 50% Discount
Puppet Online Training
Puppet Classroom TrainingEnroll Now