Table of contents
1.
Introduction
2.
Ansible File
3.
Ansible File Module Works
4.
Creating a File in Remote Server
5.
Deleting a File in Remote Server
6.
Creating a File with Permissions
7.
Creating Multiple Files
8.
Deleting Multiple Files
9.
Create a directory
10.
Frequently Asked Questions
10.1.
What are the two important files in Ansible?
10.2.
How many nodes can Ansible manage?
10.3.
What does Ansible's inventory file mean?
10.4.
How do you check Ansible syntax?
11.
Conclusion
Last Updated: Mar 27, 2024

Ansible File

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Ansible can be used to provide the underlying infrastructure of your environment. Virtualized hosts and hypervisors, network devices, and bare metal servers. It can also install services, and add compute hosts. Provision of resources, services, and applications inside of your cloud. In this article, the reader will learn about the ansible file. How the ansible file module works, creating a file on a remote server, deleting a file on a remote server. Creating a file with permissions, creating multiple files. Deleting multiple files, and creating a directory.

Ansible

Ansible File

Ansible is a fantastic automation tool that includes a tonne of tools. Functionality for managing remote hosts. It operates by putting in place modules. To carry out particular actions and activities.

 

The file module in Ansible is one helpful module. This module is in charge of carrying out operations. Including adding and changing file and directory permissions. Generating soft and hard symbolic links, deleting files and directories, and more.

Ansible File Module Works

module

The Ansible.builtin.file module is a component of Ansible's core installation by default. To prevent conflicts with modules with similar names, Ansible advises using the "Fully Qualified Name" rather than the short module name.
 

A selection of pre-defined settings for managing files is included in the file module. These settings are used to customize the operations carried out on the remote host.

You can use the following crucial parameters:

Owner: Name of the user who will be the owner of the newly created file and directory
 

Path: The directory or file to be managed's path.

 

Mode: The mode of the file or directory's permissions to be set. Put octal notation between two single quotation marks.
 

Group: Specifies the group that has ownership of a file or directory.

 

Force: If the source file is not yet available (but will be added later) or the destination symlink already exists, the Boolean value force can be used to force the formation of symlinks.

 

Follow: Follow any filesystem linkages that are present.

 

Attribute: Sets attributes for the specified file or directory using the attribute command. Similar to Linux's built-in chattr function.

Creating a File in Remote Server

Our Ansible file module has various parameters. In every file module, the path and state arguments are required. We will specify the path of the file on the remote server in the file parameter. Only the file will be created on this path.

At Path: The location of the file on the remote server is mentioned there.

At state: It makes reference to touch, which creates files exactly as a Linux command would.

Then, a new, empty file with the name devops.txt will be created. Therefore, add the filename to the path. Therefore, we shall mention touch to generate the file in the state.
 

- name: create the file in a remote server  
  file:  
   path: /path/to/file/in/remote/server/devops.txt  
   state: touch 

Deleting a File in Remote Server

Delete any command from the remote server if you choose. Therefore, specify the path of the file you wish to remove in the path option.

At Path: Specify the file's path on the remote server at the path.

At State: Delete the file by mentioning your absence.

To create the file, we will use touch, and to erase it, we will use absence.

- name:  delete the file in a remote server  
  file:  
   path: /etc/abcd.conf  
   state: absent  

Creating a File with Permissions

Using the file module, we can also create a file with permissions.
 

We have four digits at the mode parameter. Always start with zero, and the following digits will represent your file permissions.

Mention the file's owner in the owner parameter.

tasks:    
  - name: Ansible file module to create a new file with permissions.       
    file:        
    path: /path/to/cretae/file/devops.txt      
    state: touch        
    mode: 0421        
    owner: devops

This file's newly created permission will be set.

file:      
path: /path/to/cretae/file/devops.txt     
state: touch      
mode: "u=rw,g=w,o=e"      
owner: devops  

Both codes function as intended. However, we use the 0421 corresponding symbolic modes in the other code.

Creating Multiple Files

A path parameter: by using "item," we can use a loop to produce several files.

At with_items parameters: Put the file names you want to create in the with items parameter.

The "item" and the "with items" parameters allow us to build loops or multiple files.

tasks:    
- name: Ansible file module to create multiple files      
  file:         
   path: "{{ item }}"        
   state: touch       
   mode: 0421      
  with_items:      
  - devops1.txt      
  - devops2.txt      
  - devops3.txt  

Deleting Multiple Files

With a minor update to the state argument, the code will be the same for creating multiple files and deleting files.

State parameter: Tap the plus sign to add files and the minus sign to remove them.

- name: Ansible file module to delete multiple files   
  file:                    
   path: "{{ item }}"      
   state: absent    
  with_items:      
  - devops1.txt      
  - devops2.txt      
  - devops3.txt  

Create a directory

The Ansible file module's playbook for constructing a directory is similar to that for producing a blank file. However, as demonstrated below, we changed the status from "file" to "directory":

---
- hosts: all
  tasks:
  - name: createadirectory
    file:
      path: $HOME/ansible-dir
      state: directory
Change directory permissions recursively
Use the recurse argument, as shown in the playbook below, to update a directory's permissions in a recursive manner.


---
- hosts: all
  become: true
  tasks:
  - name: modifydirpermissionsrecursively
    file:
      path: /var/log/
      state: directory
      owner: root
      group: root
      mode: 0755
      recurse: true

Frequently Asked Questions

faqs

What are the two important files in Ansible?

You should think about the following three key files for Ansible are ansible.cfg file, the main file, and the host/inventory file.

How many nodes can Ansible manage?

Ansible-applicability pulls vary depending on the use case, but in general, topologies with fewer than 500 nodes nearly never require it, whereas topologies with more than 2000 nodes frequently do.

What does Ansible's inventory file mean?

The hosts and groups of hosts that the commands, modules, and tasks in a playbook depend on are specified in the Ansible inventory file.

How do you check Ansible syntax?

We can use  $ ansible-playbook <playbook. yml> --syntax-check command to check the playbook for syntax errors.

Conclusion

We covered the Ansible file in this article. We hope this article helps you to learn something new. And if you're interested in learning more, see our posts on Ansible Interview Questions Part 1Ansible Interview Questions Part 212 Best DevOps Tools To Get Acquainted With, and DevOps Interview Questions.

Visit our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.! Feel free to upvote and share this article if it has been helpful for you.

 

Live masterclass