Create a text file - Ansible module copy
How to create a simple text file with two linex of content in Ansible.


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 create a text file
Today we’re talking about Ansible module copy
.
The full name is ansible.builtin.copy
which means is part of the collection of modules “builtin” with ansible and shipped with it.
This module is pretty stable and out for years.
The purpose is to copy files to remote locations but it’s also capable to create some simple text files.
If you need a more complex configuration it’s better to rely on the template module.
Parameters
- dest path - destination file
- content string - text
- mode/owner/group - permission
- setype/seuser/selevel - SELinux
The parameter list is pretty wide but I’ll summarize the most useful for the use case.
The only required parameter is “dest” which specifies the remote absolute path destination.
The content
parameter sets the contents of a file directly to the specified value. It works only when dest
is a file/
Please note that if you use a variable in the content
parameter will result in unpredictable output.
For advanced formatting or if the content contains a variable, use the ansible.builtin.template
module.
Let me also highlight that we could also specify the permissions and SELinux properties.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
- Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
- 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 Containers and Kubernetes By Examples: 20+ Automation Examples To Automate Containers, Kubernetes and OpenShift
- 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
demo
Let’s jump into a real-life playbook on how to create an empty file with Ansible.
code
- copy.yml
---
- name: copy module demo
hosts: all
vars:
myfile: "{{ ~/example.txt }}"
tasks:
- name: create a text file
ansible.builtin.copy:
dest: "{{ myfile }}"
content: |
line 1
line 2
Recap
Now you know better how to create a text file with Ansible. 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