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:
-  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.

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

---
- 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

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory variables/write-file-copy.yml
PLAY [copy module Playbook] ***************************************************************************
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 Playbook] ***************************************************************************
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]
[devops@demo ~]$ ls
[devops@demo ~]$

Ansible copy module after execution

ansible-pilot $ ssh [email protected]
[devops@demo ~]$ ls
output.txt
[devops@demo ~]$ cat output.txt 
banana
[devops@demo ~]$

Ansible copy module code

  • write-file-template.yml
---
- name: template module Playbook
  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 Playbook] ***********************************************************************
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 Playbook] ***********************************************************************
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]
[devops@demo ~]$ ls
[devops@demo ~]$

Ansible copy module after execution

ansible-pilot $ ssh [email protected]
[devops@demo ~]$ cat output.txt 
apple
[devops@demo ~]$

code with ❤️ in GitHub

Conclusion

Now you know how to write variables to a file with Ansible using the copy and template modules. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza