Ansible Create File with Content: Write Text to Files (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: database-automation
How to create files with content in Ansible. Use copy module content parameter, template module with Jinja2, lineinfile for single lines. Practical YAML playbook examples.
Ansible Create File with Content: Write Text to Files (Complete Guide)
Creating files with specific content is one of the most common Ansible tasks. Whether you need to write a configuration file, create a script, or generate a text file with dynamic content, this guide covers every approach.
Method 1: copy Module with content Parameter (Simplest)
The fastest way to create a file with content on a remote host:
Multi-Line Content
Use YAML literal block scalar (|) for multi-line content:
Create a Shell Script
Method 2: template Module with Jinja2 (Dynamic Content)
For files with variables, loops, and conditionals:
Template file (templates/app.conf.j2):
Playbook:
Method 3: lineinfile for Single Lines
Add or modify a single line in an existing file:
Method 4: blockinfile for Text Blocks
Insert a managed block of text:
Method 5: Using Variables in copy content
Embed Ansible variables directly in the content parameter:
Method 6: Create JSON or YAML Files
Comparison: Which Method to Use
| Method | Best For | Dynamic Content | Idempotent | |--------|----------|----------------|------------| | copy + content | Simple text, scripts | ✅ Variables only | ✅ Yes | | template | Complex configs, loops | ✅ Full Jinja2 | ✅ Yes | | lineinfile | Single line changes | ✅ Regex + backrefs | ✅ Yes | | blockinfile | Managed text blocks | ✅ Variables | ✅ Yes |
Common Mistakes
1. Missing Newline at End of File
2. Not Creating Parent Directories
3. Forgetting to Set Permissions
Always specify owner, group, and mode for security:
FAQ
How do I create a file with content in Ansible?
Use ansible.builtin.copy with the content parameter. Set content to your text string and dest to the file path. Use YAML literal block scalar (|) for multi-line content.
What is the difference between copy content and template?
The copy module with content parameter is best for simple text with basic variable substitution. The template module uses Jinja2 and supports loops, conditionals, filters, and complex logic — use it for dynamic configuration files.
How do I write a variable to a file in Ansible?
Use ansible.builtin.copy with content: "{{ my_variable }}". For structured data, use content: "{{ my_dict | to_nice_json }}" or content: "{{ my_dict | to_nice_yaml }}".
Can I create a file only if it doesn't exist?
Use the force: false parameter with ansible.builtin.copy: force: false will not overwrite existing files. Alternatively, check with ansible.builtin.stat first.
How do I append content to an existing file?
Use ansible.builtin.lineinfile to add a single line, or ansible.builtin.blockinfile to add a managed block. The copy module always overwrites the entire file.
Conclusion
The copy module with content parameter is the simplest way to create files with content in Ansible. Use template for complex dynamic content, lineinfile for surgical single-line changes, and blockinfile for managed text blocks.
Related Articles • Ansible Create File: Touch & Create Empty Files with file Module • Ansible lineinfile Module: Add, Replace & Delete Lines in Files • Ansible blockinfile Module: Insert & Manage Multi-Line Text Blocks • ansible.builtin.template Module: Jinja2 Templates Complete Guide • Ansible Move File: Relocate Files Between Directories
Category: database-automation