File Manipulation Using Ansible Playbook

sangeetha arun
5 min readJan 2, 2024

--

Ansible is a modern configuration management tool that makes it easier to set up and maintain remote servers. It has a simple design that is intended to get users up and running quickly.

Ansible provisioning scripts are written by users in YAML, an approachable data serialisation standard that is not dependent on any specific programming language. Unlike other tools in the same category, this makes it easier for users to create complex provisioning scripts.

Ansible does not necessitate the installation of any additional software on the nodes that will be managed by this tool. Ansible software is installed on a control machine, which communicates with the nodes via standard SSH.

Ansible, as a configuration management tool and automation framework, encapsulates all of the common features found in other tools in the same category, while still focusing on simplicity and performance.

In this blog, we will look at file manipulation and the modules that are used for it, as it is widely used in configuration management.

The lineinfile module in Ansible is used to insert a line, modify, remove, or replace an existing line. Ansible lineinfile has a lot of parameters to help you get the job done quickly. You can also use the regular expressions to match the line before modifying or removing it.

For Ansible lab setup in AWS click on LAB SETUP for the detailed description.

Once the lab is set up, run the following command to test the server and client connectivity. We should get some green ping pong.

ansible all -m ping

In this file manipulation example, we use the lineinfile and blockinfile modules. This playbook contains six tasks; let us look at each one.

Task 1 — Create a file with the touch command and set its permissions.

- name: Create a File
file:
path: /tmp/testFile.txt
state: touch
mode: u=rw,g=r,o=r

Let us check in node1

Task 2 —Using the lineinfile module, add a line to the top of the file.

 - name: line insert at top of file
lineinfile:
path: /tmp/testFile.txt
line: 'Added Line 1'
insertbefore: BOF

Task 3 — Add a line to the end of a file.

- name: Insert a line at the end of a file.
lineinfile:
path: /tmp/testFile.txt
line: 'Added as last line'

Task 4 — After a pattern, insert a line.

- name: Inserting a line after a pattern
lineinfile:
path: /tmp/testFile.txt
line: alias ll='ls -lhA'
insertafter: alias.*

Task 5 — Add a line before the example.

 - name: Insert a line before example
lineinfile:
path: /tmp/testFile.txt
line: 'New line added before alias'
insertbefore: alias*

Task 6 — Using blockinfile, insert multiple lines.

- name: Insert multiple lines using blockinfile
blockinfile:
dest: /tmp/testFile.txt
block: |
These lines are added by blockinfile module
Check out the marker lines

Let us now combine all of the tasks in the playbook and execute it.

vim file.yaml
---
- name: yml script to check out file manipulations
hosts: all
tasks:
- name: Create a File
file:
path: /tmp/testFile.txt
state: touch
mode: u=rw,g=r,o=r
- name: line insert at top of file
lineinfile:
path: /tmp/testFile.txt
line: 'Added Line 1'
insertbefore: BOF
- name: Insert a line at the end of a file.
lineinfile:
path: /tmp/testFile.txt
line: 'Added as last line'
- name: Inserting a line after a pattern
lineinfile:
path: /tmp/testFile.txt
line: alias ll='ls -lhA'
insertafter: alias.*
- name: Insert a line before example
lineinfile:
path: /tmp/testFile.txt
line: 'New line added before alias'
insertbefore: alias*
- name: Insert multiple lines using blockinfile
blockinfile:
dest: /tmp/testFile.txt
block: |
These lines are added by blockinfile module
Check out the marker lines
backup: yes

We’re also making a backup of the file in the same tmp directory, as shown in the playbook below.

backup: yes

When we use the below command, it will ask for confirmation on each and every step.

ansible-playbook file.yaml --step

Using the playbook, we successfully modified the file. This file manipulation is mostly used in configuration management, where some configuration files must be changed and successfully deployed. We can do it for a hundred servers using Ansible playbooks.

Conclusion

Ansible is a simple IT automation tool with a low learning curve, thanks in part to its use of YAML for provisioning scripts. It includes a large number of built-in modules for abstracting tasks like installing packages and working with templates. Its simplified infrastructure requirements and easy-to-understand syntax may be ideal for those just getting started with configuration management.

Hopefully this article provides a starting point for file manipulation.

I’m filled with joy to present this article, eager to ignite your curiosity with fresh insights. Let your applause resonate by clicking that heartfelt 👏 button below, a gesture that means the world to us.

--

--