Skip to content

Example 2

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

playbook.yml

- hosts: all
  tasks:
    - name: Jinja2 if/elif statement
      debug:
        msg: >
          {# This is a comment in Jinja2 -#}
          {% if ansible_distribution == "CentOS" -%}
            This machine runs on CentOS
          {% elif ansible_distribution == "Ubuntu" -%}
            This machine runs on Ubuntu
          {% endif %}

Tip

Thanks to Jinja2 if/else/elif statements can be incorporated directly into system calls.

Execute the following command

ansible-playbook playbook.yml