Concatenate multiple files in a specific order - Ansible module template and YAML
How to automate the concatenation of multiple files and format using a list in the YAML and Ansible module template with Jinja2 language. Included example with "a.txt", "b.txt" and "includes.yaml" files for Pandoc.


How to use Concatenate multiple files in a specific order using Ansible?
This is extremely useful for service configuration files, reports, and so much more use cases. I personally use this code for markdown documents for Pandoc. 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 Concatenate multiple files in a specific order
- 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
Let’s talk about the Ansible module template
.
The full name is ansible.builtin.template
, it’s part of ansible-core
and is included in all Ansible installations.
It templates a file out to a target host. Templates are processed by the Jinja2 templating language.
Also you could use also some special variables in your templates: ansible_managed
, template_host
, template_uid
, template_path
, template_fullpath
, template_destpath
, and template_run_date
.
It supports a large variety of Operating Systems.
For basic text formatting, use the Ansible ansible.builtin.copy
module or for empty file Ansible ansible.builtin.file
module.
For Windows, use the ansible.windows.win_template
module instead.
Parameters
- src path - template (“templates/” dir)
- dest path - target location
- validate string - validation command before ("%s")
- backup boolean - no/yes
- mode/owner/group - permission
- setype/seuser/selevel - SELinux
Let me highlight the most useful parameters for the template
module.
The only required parameters are “src” and “dest”.
The “src” parameter specifies the template file name. Templates usually are stored under “templates” directories with the “.j2” file extension.
The “dest” parameter specifies the path where to render the template on the remote machine.
The “validate” parameters allow you to specify the validation command to run before copying it into place. It’s very useful with configuration files for services.
Please note that the special escape sequence “%s” is going to be expanded by Ansible with the destination path.
If the “backup” parameter is enabled Ansible creates a backup file including the timestamp information before copying it to the destination.
Let me also highlight that we could also specify the permissions and SELinux properties.
The Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
How to concatenate multiple files in a specific order with the Ansible module template and YAML.
code
- a.txt
A content
- b.txt
B content
- includes.yaml
input-files:
- concatenate/b.txt
- concatenate/a.txt
- concatenate.yml
---
- name: concatenate demo
hosts: "{{ HOSTS }}"
become: false
gather_facts: true
vars:
myinput: "concatenate/includes.yaml"
myoutput: "concatenate/output.txt"
tasks:
- name: include file list
include_vars:
file: "{{ myinput }}"
name: files
- name: concatenate
ansible.builtin.template:
src: templates/concatenate.j2
dest: "{{ myoutput }}"
- concatenate.j2
{% for i in files["input-files"] %}
{{ lookup('file', i) }}
{% endfor %}
execution
ansible-pilot $ ansible-playbook -e "HOSTS=localhost" concatenate.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [concatenate demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [localhost]
TASK [include file list] **************************************************************************
ok: [localhost]
TASK [concatenate] ********************************************************************************
changed: [localhost]
PLAY RECAP ****************************************************************************************
localhost : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
idempotency
ansible-pilot $ ansible-playbook -e "HOSTS=localhost" concatenate.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit
localhost does not match 'all'
PLAY [concatenate demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [localhost]
TASK [include file list] **************************************************************************
ok: [localhost]
TASK [concatenate] ********************************************************************************
ok: [localhost]
PLAY RECAP ****************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
before execution
ansible-pilot $ ls -al concatenate
total 24
drwxr-xr-x 5 lberton staff 160 Feb 1 15:40 .
drwxr-xr-x 9 lberton staff 288 Feb 1 15:08 ..
-rw-r--r-- 1 lberton staff 9 Feb 1 15:18 a.txt
-rw-r--r-- 1 lberton staff 9 Feb 1 15:18 b.txt
-rw-r--r-- 1 lberton staff 56 Feb 1 15:48 includes.yaml
ansible-pilot $ cat concatenate/a.txt
A content%
ansible-pilot $ cat concatenate/b.txt
B content%
ansible-pilot $ cat concatenate/includes.yaml
input-files:
- concatenate/b.txt
- concatenate/a.txt
ansible-pilot $
after execution
ansible-pilot $ ls -al concatenate
total 32
drwxr-xr-x 6 lberton staff 192 Feb 1 15:50 .
drwxr-xr-x 9 lberton staff 288 Feb 1 15:08 ..
-rw-r--r-- 1 lberton staff 9 Feb 1 15:18 a.txt
-rw-r--r-- 1 lberton staff 9 Feb 1 15:18 b.txt
-rw-r--r-- 1 lberton staff 56 Feb 1 15:48 includes.yaml
-rw-r--r-- 1 lberton staff 20 Feb 1 15:49 output.txt
ansible-pilot $ cat concatenate/output.txt
B content
A content
ansible-pilot $
Recap
Now you know how to concatenate multiple files in a specific order with the Ansible module template and YAML. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate