Skip to main content

Loops

Updated Jan 08, 2021 ·

Overview

This lab practices Ansible loops to repeat tasks over lists of values.

Task 1

The playbook currently runs an echo command to print a fruit name. Apply a loop directive (with_items) to the task to print all fruits defined in the fruits variable.

Task 2

To a more realistic use case. We are attempting to install multiple packages using yum module.The current playbook installs only a single package.

Solutions

Solution 1
---

- name: 'Print list of fruits'
hosts: localhost
vars:
fruits:
- Apple
- Banana
- Grapes
- Orange
tasks:
- command: 'echo "{{ item }}"'
with_items: '{{ fruits }}'
Solution 2
---
- name: 'Install required packages'
hosts: localhost
vars:
packages:
- httpd
- binutils
- glibc
- ksh
- libaio
- libXext
- gcc
- make
- sysstat
- unixODBC
- mongodb
- nodejs
- grunt
tasks:
- yum:
name: '{{ item }}'
state: 'present'
with_items: '{{ packages }}'