Ansible: Create File With Content

Ansible has built-in modules that can be used to create an empty file or a file with a content.

These common tasks can be easily done using the Ansible’s file module (or win_file for Windows targets) and copy module (or win_copy for Windows targets).

In this short note i will show the examples of the Ansible tasks for creating an empty file or a file with a content.

Cool Tip: How to “cat” a remote file using Ansible! Read more →

Create File With Content using Ansible

To create an empty file we can use the Ansible’s file module (or win_file):

- name: "Ansible | Creating an empty file"
  file:
    path: "<filePath>"
    state: touch

To create a file with a content, use the Ansible’s copy module (or win_copy):

- name: "Ansible | Creating a file with content"
  copy:
    dest: "<filePath>"
    content: |
      <line1>
      <line2>

Cool Tip: How to run a shell command on a remote host using Ansible! Read more →

Leave a Reply