Javatpoint Logo
Javatpoint Logo

Ansible File

Ansible file module is used to creating and deleting the file or multiple files in the remote server. You can also create and delete the directories and change the permissions of the data.

You can also create and delete the soft links (symlinks) as well as hard links. With the help of the Ansible file module, you can set the permission of the files.

Creating a File in Remote Server

In the Ansible file module, we have different parameters. We are using path and state parameters that are must in every file module. In the file parameter, we will mention the path of the file in the remote server. On this path, only the file will be created.

At path: It mentions the path of the file in the remote server.

At state: It mentions touch, and touch will create file exact like Linux command.

Then, it will create a new empty file with the name devops.txt. So mention filename in the path. So in the state: we will mention touch to create the file.

Deleting a File in Remote Server

If you want to delete any command in the remote server. So at path parameter, mention the path of the file which you want to delete.

At path: Mention the path of the file in the remote server.

At state: Mention absent to delete the file.

So in the state: we will use touch to create the file, absent to delete the e file.

Creating a File with Permissions

We can also create the file with permission by using the file module.

At the mode parameter: we have 4 digits. Always mention zero at the starting, and remaining digits will be your file permissions.

At owner parameter: mention the owner of the file.

This permission will be set to that newly created file.

Both the codes work the same, but in the other code, we are using the symbolic mode, which is equivalent to 0421.

Creating Multiple Files

A path parameter: we can create a loop to create multiple files by using "{{item}}".

At with_items parameter: mention file names which you want to create.

By using "{{item}}" and with_items parameter, we can create loop or multiple files.

Deleting Multiple Files

The code will be the same to create multiple files and to delete files but a small change in the state parameter.

State parameter: Touch the create files and absent to delete files.

Next TopicAnsible Vault

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA