In this section, we will explore how to use conditionals and loops in Ansible playbooks. These features allow you to create more dynamic and flexible automation scripts.
Conditionals
Conditionals in Ansible allow you to execute tasks based on certain conditions. This is useful when you want to perform actions only if specific criteria are met.
Basic Syntax
The basic syntax for conditionals in Ansible is to use the when keyword followed by a Jinja2 expression.
- name: Install Apache on Debian-based systems
apt:
name: apache2
state: present
when: ansible_os_family == "Debian"Practical Example
Let's consider a more detailed example where we install different packages based on the operating system.
- name: Install web server
hosts: all
tasks:
- name: Install Apache on Debian-based systems
apt:
name: apache2
state: present
when: ansible_os_family == "Debian"
- name: Install Apache on RedHat-based systems
yum:
name: httpd
state: present
when: ansible_os_family == "RedHat"Common Mistakes
- Incorrect Condition Syntax: Ensure that the condition is a valid Jinja2 expression.
- Indentation Errors: Make sure the
whenkeyword is correctly indented under the task.
Loops
Loops in Ansible allow you to repeat a task multiple times with different parameters. This is useful for tasks like installing multiple packages or creating multiple users.
Basic Syntax
The basic syntax for loops in Ansible is to use the loop keyword followed by a list of items.
- name: Install multiple packages
apt:
name: "{{ item }}"
state: present
loop:
- apache2
- mysql-server
- phpPractical Example
Let's consider a more detailed example where we create multiple users.
- name: Create multiple users
hosts: all
tasks:
- name: Add users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- { name: 'alice', groups: 'admin' }
- { name: 'bob', groups: 'users' }
- { name: 'charlie', groups: 'users' }Loop Control
You can also control the behavior of loops using loop_control.
- name: Install multiple packages with index
apt:
name: "{{ item }}"
state: present
loop:
- apache2
- mysql-server
- php
loop_control:
index_var: pkg_indexCommon Mistakes
- Incorrect Loop Syntax: Ensure that the loop is a valid list.
- Variable Scope: Be cautious about variable scope within loops.
Practical Exercise
Exercise
Write a playbook that performs the following tasks:
- Installs
nginxon Debian-based systems andhttpdon RedHat-based systems. - Creates three users:
user1,user2, anduser3, each belonging to theusersgroup.
Solution
- name: Conditional and Loop Exercise
hosts: all
tasks:
- name: Install web server on Debian-based systems
apt:
name: nginx
state: present
when: ansible_os_family == "Debian"
- name: Install web server on RedHat-based systems
yum:
name: httpd
state: present
when: ansible_os_family == "RedHat"
- name: Create multiple users
user:
name: "{{ item }}"
state: present
groups: users
loop:
- user1
- user2
- user3Summary
In this section, we covered:
- The use of conditionals with the
whenkeyword to execute tasks based on specific conditions. - The use of loops with the
loopkeyword to repeat tasks with different parameters. - Practical examples and common mistakes to avoid.
Understanding conditionals and loops will help you create more dynamic and flexible Ansible playbooks. In the next section, we will delve into roles, which allow you to organize your playbooks into reusable components.
Ansible: From Beginner to Advanced
Module 1: Introduction to Ansible
Module 2: Ansible Basics
Module 3: Playbooks
- Introduction to Playbooks
- Writing Your First Playbook
- Playbook Structure
- Variables and Facts
- Conditionals and Loops
Module 4: Roles
Module 5: Advanced Playbook Techniques
Module 6: Ansible Galaxy
Module 7: Ansible Tower
- Introduction to Ansible Tower
- Installing Ansible Tower
- Using Ansible Tower
- Managing Projects and Inventories
