Write a Variable to a File - Ansible module copy vs template
How to save the value of a variable to file using the Ansible copy vs template modules. Included the comparison and a step-by-step example.


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 demo 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:
- 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:
- name: write to file
ansible.builtin.template:
src: "mytemplate.j2"
dest: "/path/to/destination/file"
- mytemplate.j2
{{ 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.
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
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
---
- name: copy module demo
hosts: all
vars:
fruit: "banana"
output: "output.txt"
tasks:
- name: write to file
ansible.builtin.copy:
content: "{{ fruit }}"
dest: "{{ output }}"
Ansible copy module execution
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/write-file-copy.yml
PLAY [copy module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [write to file] ******************************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
Ansible copy module idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/write-file-copy.yml
PLAY [copy module demo] ***************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [write to file] ******************************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
Ansible copy module before execution
ansible-pilot $ ssh [email protected]
[[email protected] ~]$ ls
[[email protected] ~]$
Ansible copy module after execution
ansible-pilot $ ssh [email protected]
[[email protected] ~]$ ls
output.txt
[[email protected] ~]$ cat output.txt
banana
[[email protected] ~]$
Ansible copy module code
- write-file-template.yml
---
- name: template module demo
hosts: all
vars:
fruit: "apple"
output: "output.txt"
tasks:
- name: write to file
ansible.builtin.template:
src: "mytemplate.j2"
dest: "{{ output }}"
- mytemplate.j2
{{ fruit }}
Ansible copy module execution
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/write-file-template.yml
PLAY [template module demo] ***********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [write to file] ******************************************************************************
changed: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
Ansible copy module idempotency
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/write-file-template.yml
PLAY [template module demo] ***********************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [write to file] ******************************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
Ansible copy module before execution
ansible-pilot $ ssh [email protected]
[[email protected] ~]$ ls
[[email protected] ~]$
Ansible copy module after execution
ansible-pilot $ ssh [email protected]
[[email protected] ~]$ cat output.txt
apple
[[email protected] ~]$
Recap
Now you know how to write variables to a file with Ansible using the copy and template modules. 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