Davinder Pal

Sep 3, 2020

3 min read

Ansible Tips and Tricks ( Part 2 )

Courtesy: revelwood.com

Trick 2: Ansible Loop Inside another Loop

If you are Ansible User then you might already know about Nested Loops.

Task 1: copy template x
Task 2: copy template y
Task 3: copy template z
.....
Task 1: copy template x ( task1.yml )
Task 2: copy template y ( task2.yml )
Task 3: copy template z ( task3.yml )
.....
Main File:Run Dynamic Task: copy template
parameter list:
template x
template y
.....
Dynamic Task:
Actual Copy of Template
---
- name: Creating kafka configurations | {{ kafkaConfigFile }}
template:
src: "{{ kafkaConfigFile }}"
dest: "{{ kafkaInstallDir }}/kafka_{{ kafkaScalaVersion }}-{{ kafkaVersion }}/config/{{ kafkaConfigFile }}"
register: configStatus
- name: Creating kafka configurations
include_role:
name: configure
tasks_from: dynamicConfigs.yml
vars:
- kafkaConfigFile: "{{ item }}"
loop:
- kafka-broker.properties
- log4j.properties
- hosts: clusterNodes:clusterAddNodes
gather_facts: true
serial: 1
tasks:
- include_role:
name: configure
tasks_from: dynamicConfigs
vars:
- kafkaConfigFile: kafka-broker.properties