Trending September 2023 # How Does Lineinfile Works In Ansible With Examples # Suggested October 2023 # Top 11 Popular | Lifecanntwaitvn.com

Trending September 2023 # How Does Lineinfile Works In Ansible With Examples # Suggested October 2023 # Top 11 Popular

You are reading the article How Does Lineinfile Works In Ansible With Examples updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Does Lineinfile Works In Ansible With Examples

Introduction to Ansible lineinfile

Ansible Lineinfile is a module of Ansible that is used to modify the particular line in a file. It is useful to add a new line, modify a line, replace a line, and remove an existing line in a file if it finds a specific text. We can use a regular expression to apply search conditions while working with the Lineinfile module. It has cool options that make our job easier. For example, if we have to insert a line in a file on multiple hosts and we are not sure that file is present on the hosts or not, we can use ‘create’ options and make it ‘yes’ to avoid errors that we are going to encounter if the file does not exist on the destination.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How lineinfile Works in Ansible?

Basically, Lineinfile module takes the file name using the ‘path’ parameter as it is the required option for this module and makes the changes in a file as per other given parameters like what action we want to perform on that file. The module can take a backup before making any changes if we have specified the ‘backup’ option to yes as it is an optional parameter, however, it is recommended to take a backup of the file using ‘backup’ so that we can get the original file back if the changes made to the file are incorrect.

Let’s learn about various operations we can perform using ‘Lineinfile’ module and how:

1. Inserting a lineinfile

We can insert a line in a file using the ‘lineinfile’ module. By default, It adds the line at the end of the file if does not exist. Here is a playbook for the same:

Code:

ansible test_group -m shell -a “ls /root/testing”

Explanation: In the above example, we got an error because the file does not exist at the destination and we have not specified the ‘create’ option. Let’s add the ‘create’ option and make it to yes.

Code:

backup: yes

cat lineinfile_insert.yml

Output:

Explanation: In the above example, we have added the ‘create’ option and re-ran the playbook and it ran without any error. Below command is used to verify that there was no file present named ‘test_file2’ before running the playbook:

ansible test_group -m shell -a "ls /root/testing"

As mentioned above, the ‘lineinfile’ module only adds the line at the end of the file by default, however, we can add line after and before a specific line using the ‘insertafter’ and ‘insertbefore’ options respectively. Here is the snapshot of the file test file that we are going to edit.

cat test_file

Output:

Below is the playbook to insert line after a specific line: –

Code:

backup: yes

ansible test_group -m shell -a "cat /root/testing"

Output:

Explanation: In the above example, we have added ‘ansible_client’ line after the line that matches ‘ServerName’. In the same way, we can use the ‘insertbefore’ option in place of the ‘insertafter’ option to add the line before the specific line if it finds the match. Example will be shown when we modify the line in the file.

2. Replacing the lineinfile

We can replace a line using the ‘lineinfile’ module however it will only replace one line in a file if there are multiple lines match the specified regular expression. ‘lineinfile’  module uses the ‘regexp’ option to match the criteria. It replaces the last line that matches the criteria by default. We can control it using the ‘firstmatch’ option to replace the first line that matches the criteria. Ansible has ‘replace’ module to replace all the occurrences in the file. We are going to use the same test file that is mentioned above to see it in action and if you have remembered we have added ‘ansible_client’ after the line that has ‘ServerName’ text in it.

Here is the playbook to replace the line in a file:

Code:

backup: yes

ansible-playbook lineinfile_replace.yml

Output:

Explanation: In the above example, we have replaced the line that matches ‘ansible_client’ text in it with ‘ansible_master’. We can see the difference if we compare the above snapshot with the last snapshot. In the last snapshot, the third line was ‘ansible_client’ that is replaced with ‘ansible_master’ as we see in the above snapshot.

Below command is used to search for ‘ansible_master’ in the ‘test_file’:

ansibletest_group -m shell -a "grep -in ansible_master /root/test_file"

3. Removing all Lines from a File that Matches the Criteria

We can search for the lines that match specific criteria using the ‘regexp’ option and remove those lines by changing the ‘state’ option to absent. Here is the playbook for the same:

Code:

ansible-playbook lineinfile_remove.yml

Output:

Explanation: In the above example, playbook searched for ‘ansible_client’ and removed those lines so when we ran the below command to check the ‘ansible_client’ text in the file, we got non-zero return code as it does not find any text ‘ansible_client’ in the file.

ansible test_group -m shell -a “grep -in asnible_client /root/test_file”

4. Modifying a lineinfile

Code:

backup: yes

ansible test_group -m shell -a "cat /root/test_file"

Output:

Conclusion

The ‘lineinfile’ module is very useful if we have to make changes to a configuration file on multiple hosts. It saves our time when we need to insert, modify, replace, or remove a line in a file on different machines. It has a limitation as it only works on a single line.

Recommended Article

This is a guide to Ansible lineinfile. Here we discuss Introduction and how lineinfile Works in Ansible along with its different examples and Code Implementation. You can also go through our other suggested articles to learn more –

You're reading How Does Lineinfile Works In Ansible With Examples

Update the detailed information about How Does Lineinfile Works In Ansible With Examples on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!