AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible Concatenate Files: Merge Multiple Files in Order (Guide) — Video Tutorial
How to concatenate multiple files in a specific order with Ansible. Use template, assemble, and shell modules to merge configs and files with examples.
What You'll Learn
- How to use Concatenate multiple files in a specific order using Ansible?
- Ansible Concatenate multiple files in a specific order
- Parameters
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
- Using assemble Module
Full Tutorial Content
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 Playbook 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](/articles/create-a-text-file-ansible-module-copy) or for [empty file Ansible `ansible.builtin.file` module](/articles/create-an-empty-file-ansible-module-file).
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.
## Playbook
How to concatenate multiple files in a specific order with the Ansible module template and YAML.
code
- a.txt
```txt
A content
```
- b.txt
```txt
B content
```
- includes.yaml
```yaml
input-files:
- concatenate/b.txt
- concatenate/a.txt
```
- concatenate.yml
```yaml
---
- name: concatenate Playbook
hosts: "{{ HOSTS }}"
become: false
gather_facts: true
vars:
myinput: "concatenate/in
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 7 min
- Category: installation
Read the full written article: Ansible Concatenate Files: Merge Multiple Files in Order (Guide)
Related Video Tutorials
- ansible.builtin.template: Deploy Jinja2 Templates with Ansible (Guide) — Learn to create and use templates in Ansible Playbooks, including an example of deploying an HTML placeholder using Jinja2 templates in your Nginx webserver.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.
- Ansible Check If Directory Exists: stat Module Guide — How to check if files and directories exist with Ansible stat module. Use stat results in conditionals, check permissions, size, and timestamps.
- Ansible win_stat: Check if File or Directory Exists on Windows (Examples) — How to check if a file or directory exists on Windows using Ansible win_stat module. Conditional tasks, file properties, checksum verification with practical.
- Ansible Check if File Exists: stat Module with when Conditional (Guide) — How to check if a file exists in Ansible with the stat module and when conditional. Verify file existence, check directories, test before actions.
- Ansible win_copy Module: Copy Files to Windows Hosts (ansible.windows.win_copy) — How to copy files to Windows remote hosts with Ansible win_copy module (ansible.windows.win_copy). Transfer files, directories, set permissions.