Member-only story
Write a Variable to 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.