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 Write to File: Variable Content with copy & template Modules — Video Tutorial
How to write variables to files in Ansible. Compare copy content vs template module, write JSON/YAML, and generate dynamic config files with examples.
What You'll Learn
- How to write a variable to file with Ansible?
- Ansible modules: copy vs template
- Ansible module copy - Write Variable to a File
- Ansible module template - Write Variable to a File
- Ansible copy module code
- Ansible copy module execution
- Ansible copy module idempotency
- Ansible copy module before execution
- Ansible copy module after execution
- Ansible copy module code
Full Tutorial Content
How to write a variable to file with Ansible?
From a simple value or the result of complex command execution on the target node often we have the need to write the result to a file.
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 modules: copy vs template
- `ansible.builtin.copy`
It deals with whitespace and newlines.
The quotes are important.
- `ansible.builtin.template`
For advanced formatting or if the content contains a variable, use the `ansible.builtin.template` module.
The `copy` and `template` Ansible modules have the ability to write variables to a file.
Long story short: use the `template` module instead of the `copy` module.
Both modules write variables to a file but the template module is the safer way for advanced formatting or if the content contains a variable.
Preferred also by the early adopter of Ansible, the `copy` module that deals with whitespace and newlines but performs poorly with quotes and escapes contents.
On the other hand, the `template` module is the best option for advanced formatting or if the content contains a variable.
Ansible module copy - Write Variable to a File
- Ansible Playbook task:
```yaml
- name: write to file
ansible.builtin.copy:
content: "{{ fruit }}"
dest: "/path/to/destination/file"
```
The main advantage to use the Ansible copy module to Write Variable to a File is that you could write it all in one Ansible Playbook file.
In this example, the parameter `content` specifies the name of the `fruit` variable to be written to the `dest` parameter, the path of the destination file.
Ansible module template - Write Variable to a File
- Ansible Playbook task:
```yaml
- name: write to file
ansible.builtin.template:
src: "mytemplate.j2"
dest: "/path/to/destination/file"
```
- mytemplate.j2
```yaml
{{ fruit }}
```
On the other hand, the Ansible module `template` is the best option to write variables to a file.
Unfortunately, this module doesn't have any `content` parameter so you need to create a secondary template file to output the `fruit` variable.
As you could see in this simple example the writing to file process is started in the main Ansible Playbook file, triggering the `mytemplate.j2` Jinja2 template that only output the content of the `fruit` variable.
The result is written to the `dest`, the destination file defined in the Ansible Playbook.
## Playbook
How to write variables to a file with Ansible Playbook.
I'm going to show you how to write to file a simple `fruit` variable using the Ansible module copy and the Ansible module template.
Ansible copy module code
```yaml
---
- name: copy module Playbook
hosts: all
vars:
fruit: "banana"
output: "output.txt"
tasks:
- name: write to file
ansible.builtin.copy:
content: "{{ fruit }}"
dest: "{{ output }}"
```
Ansible copy module execution
```bash
ansible-pilot $ ansible-playbook
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 8 min
- Category: troubleshooting
Read the full written article: Ansible Write to File: Variable Content with copy & template Modules