Do you think IIT Guwahati certified course can help you in your career?
No
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 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
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
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
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.