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 Create File with Content: copy Module content Parameter — Video Tutorial
How to create files with content in Ansible using the copy module content parameter. Write text, YAML, JSON to files without templates.
What You'll Learn
- Ansible create a text file
- Parameters
- code
- Conclusion
- Advanced copy Module Examples
- Write multi-line content
- Write variable content to file
- Copy file from controller to remote
- Create file only if it doesn't exist
- Save command output to file
Full Tutorial Content
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 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.
## Playbook
Let's jump into a real-life playbook on how to create an empty file with Ansible.
code
- copy.yml
```yaml
---
- name: copy module Playbook
hosts: all
vars:
myfile: "{{ ~/example.txt }}"
tasks:
- name: create a text file
ansible.builtin.copy:
dest: "{{ myfile }}"
content: |
line 1
line 2
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/tree/master/create%20file)
Conclusion
Now you know better how to create a text file with Ansible.
Advanced copy Module Examples
Write multi-line content
```yaml
- name: Create config file with multiple lines
ansible.builtin.copy:
content: |
[database]
host = db.example.com
port = 5432
name = myapp
user = {{ db_user }}
password = {{ db_password }}
dest: /etc/myapp/database.conf
owner: appuser
group: appuser
mode: '0600'
become: true
```
Write variable content to file
```yaml
- name: Gather system info
ansible.builtin.setup:
gather_subset: network
- name: Save IP address to file
ansible.builtin.copy:
content: "{{ ansible_default_ipv4.address }}"
dest: /etc/myapp/server_ip.txt
mode: '0644'
```
Copy file from controller to remote
```yaml
- name: Copy local file to remote host
ansible.builtin.copy:
src: files/nginx.conf
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: '0644'
backup: true # Create backup of existing file
become: true
notify: restart nginx
```
Create file only if it doesn't exist
```yaml
- name: Create default config (don't overwrite)
ansible.builtin.copy:
content: |
# Default configuratio
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 7 min
- Category: linux-administration
Read the full written article: Ansible Create File with Content: copy Module content Parameter
Related Video Tutorials
- Ansible Create Symlink: file Module with state=link (Guide) — How to create symbolic links (symlinks) in Ansible with the file module state=link. Create, manage, remove symlinks and hard links.
- Ansible Create Empty File: Touch Files with file Module (Guide) — How to create empty files in Ansible with the file module state=touch. Create files, set permissions and ownership on new files.
- Ansible Rename File: Move & Rename Files with copy + file Modules — How to rename files and directories in Ansible using copy and file modules. Move files, rename with register, use command module.
- 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 Create Directory: file Module with state=directory (Guide) — How to create directories in Ansible with the file module state=directory. Set permissions, owner, group, recursive creation.
- Ansible Create Hard Link & Symlink: file Module Guide — How to create hard links and symbolic links with Ansible file module. Use state=hard and state=link with permissions, force, and practical examples.