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.

Watch Video

Watch "Ansible Create File with Content: copy Module content Parameter" on YouTube

What You'll Learn

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

Read the full written article: Ansible Create File with Content: copy Module content Parameter

Topics Covered

Related Video Tutorials