Member-only story

Loop in file template — Ansible module template — Generate hosts file

How to use for loop in Ansible module template to generate/etc/myhosts file with IP address, hostname, and short name from Ansible inventory. Ansible Playbook, Jinja2 template, and with Magic Variables included.

Luca Berton
5 min readNov 24, 2021

How to use a loop in a file template to the target host with Ansible?

This is extremely useful for service configuration files, placeholder web pages, reports, and so much more use cases.

I’m going to show you a live demo with some simple Ansible code.

I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible loop in file template

  • ansible.builtin.template
  • Template a file out to a target host
  • ansible_managed, template_host, template_uid, template_path, template_fullpath, template_destpath, and template_run_date

Today we’re talking about the Ansible module template.

--

--

Luca Berton
Luca Berton

Written by Luca Berton

I help creative Automation DevOps, Cloud Engineer, System Administrator, and IT Professional to succeed with Ansible Technology to automate more things everyday

No responses yet