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
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.
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.
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 -i virtualmachines/demo/inventory variables/write-file-copy.yml
PLAY [copy module Playbook] ***************************************************************************
TASK [Gathering Facts] ***************************************************************************