Ansible Pilot

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

It deals with whitespace and newlines. The quotes are important.

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

-  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

-  name: write to file
   ansible.builtin.template:
    src: "mytemplate.j2"
    dest: "/path/to/destination/file"
{{ 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.

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

---
- 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 }}"
{{ 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] ~]$

code with ❤️ in GitHub

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 and Twitter 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: 100+ Automation Examples For Linux and Windows System Administrator and DevOps

Want to keep this project going? Please donate

Trustpilot
Follow me

Subscribe not to miss any new releases

May 3, 2022

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress