How to Create a File in Ansible

October 7, 2020

Introduction

Ansible is an Infrastructure as Code tool that allows you to use a single central location (Ansible control node) to monitor and control a large number of remote servers (hosts).

Use Ansible to set up a number of tasks that the remote hosts can perform, including creating new files and directories.

This tutorial covers different ways you can use Ansible to create files on remote hosts.

How to create a file in Ansible

Prerequisites

Creating an Ansible Playbook

Ansible playbooks are lists of tasks you want your remote hosts to perform. They can be saved and reused, allowing you to automate complex processes.

Every command you want to issue to a remote host, including creating files and folders, is a task you must include in a playbook. Once you execute a playbook, your remote host performs all the tasks defined in it.

Note: In this tutorial, we use the Nano editor to add commands to our playbook. You can also use any other Linux text editor.

To create an Ansible playbook, use the command:

sudo nano /etc/ansible/playbook.yaml

This command creates a .yaml file called ‘playbook’ in the Ansible install folder. We will use tasks in the playbook.yaml file to create files and folders on our remote hosts.

Using Ansible’s dry run feature enables users to execute a playbook without making changes to the servers. It uses the built-in check mode to proof a playbook for errors before execution.

Creating an Empty File

The fastest way to create an empty file is by using Ansible’s file module.

Add the following configuration to your Ansible playbook:

---

- hosts: all
  tasks:
  - name: Creating an empty file
    file:
      path: "/your path"
      state: touch
Creating an empty file in Ansible

The file above has the following components:

  • hosts: Defines on which remote hosts from your Ansible inventory file you want to execute the task. All means that every host receives the command, but you can also enter the name of a host category or one individual host.
  • tasks: Announces that the remote host needs to perform a task.
  • name: Lets you define a name for the task. The name is for your reference only and has no influence on the task itself.
  • file: Engages Ansible’s file module to create a new file.
  • path: Defines the path for the new file on the remote host’s hard drive.
  • state: Similar to the touch command in the Ubuntu terminal, entering touch creates an empty file in the location you chose.

To execute the playbook, use:

ansible-playbook /etc/ansible/playbook.yaml
Executing an Ansible playbook

Creating a File With Content

If you want to create a new file with content, you can first use the method above to create an empty file, and then use the blockinfile or lineinfile module to insert content.

A quicker way is to use the copy module. Even though this module is used to copy a file from the control node to the remote host, you can include the content parameter to instantly add content to an empty file.

Use this configuration in your playbook:

---

- hosts: all
  tasks:
  - name: Creating a file with content
    copy:
      dest: "/your path"
      content: |
        line 01
        line 02

In the file, we used:

  • copy: Engages Ansible’s copy module.
  • dest: Defines the path for your new file.
  • content: This parameter will add line 01 and line 02 as the content of the new file.

Creating Multiple Files

You can create multiple files by using a single task in an Ansible playbook.

Use the following configuration to create multiple files:

---

- hosts: all
  tasks:
  - name: Create multiple files
    file: 
      path: "{{ item }}"
      state: touch
    with_items:
    - test01.txt
    - test02.txt
    - test03.txt
    - test04.txt

In the configuration file above, we defined:

  • path: The "{{ item }}" value means that Ansible will create a separate path for each respective file. By default, these files go in the Home folder of the remote host. Define a different path by using /your_folder_path/"{{ item }}".
  • with_items: This parameter is used to start a list of files to create. List as many files as you want. In our example, we created a list of four files titled test.

Creating a Directory

Creating a new directory uses the same configuration as when creating an empty file. The only difference is that under the state parameter, you enter directory as the value:

---

- hosts: all
  tasks:
  - name: Creating a new directory
    file:
      path: "/your path"
      state: directory

Removing Files

Ansible playbooks can also remove existing files. To do this, set the state parameter to absent:

---

- hosts: all
  tasks:
  - name: Removing a file
    file:
      path: "/your path"
      state: absent

If the file is already removed, this command does nothing.

Setting File Permissions

With all of the examples above, you can also set the permission for new files and folders. For this, you need to use the mode parameter.

There are two ways to do this:

  • Using octal mode format: You can use octal numbers, like 0644 or 0777. Don’t forget the leading 0, as leaving it out can lead to unexpected results.
  • Using symbolic mode format: You can use values like u=rwx, g=rx, or o=rx, where u stands for owner, g stands for group, and o stands for others. The permissions are defined as r for read, w for write, and x for execute.

For example, you can use the octal value 0755, while defining the owner:

---

- hosts: all
  tasks:
  - name: Create a new file with permissions
    file:
      path: "/your path"
      state: touch
      mode: 0755
      owner: test

You can also use the symbolic equivalent to 0755:

---

- hosts: all
  tasks:
  - name: Create a new file with permissions
    file:
      path: "/your path"
      state: touch
      mode: u=rwx,g=rx,o=rx
      owner: test

Conclusion

After following this tutorial, you should understand the basics of setting up Ansible playbooks and using them to create new files and directories on remote hosts.

You can build from here by adding more tasks and creating complex playbooks, which will make adding large numbers of files quick and easy. Playbooks can be used to define a variety of tasks for the remote hosts to perform, including checking if files and folders exist.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
How to Install and Configure Ansible on Windows
May 30, 2024

Ansible is a Linux-based app used for monitoring and managing remote severs. There are several ways you can...
Read more
How to Install and Configure Ansible on Ubuntu 20.04
October 7, 2020

Ansible is a management system that helps you manage a large number of servers without the need for any...
Read more
How to Install phoenixNAP BMC Ansible Module
July 24, 2020

Learn how to successfully install the Bare Metal Cloud Ansible module on your control machine. Take a look at...
Read more
What is Bare Metal Cloud
May 20, 2020

This article provides answers to everything you wanted to know about Bare Metal Cloud and how it compares to...
Read more