Trending September 2023 # How Does The Replace Module Works In Ansible? # Suggested October 2023 # Top 14 Popular | Dacvumuahe.com

Trending September 2023 # How Does The Replace Module Works In Ansible? # Suggested October 2023 # Top 14 Popular

You are reading the article How Does The Replace Module Works In Ansible? updated in September 2023 on the website Dacvumuahe.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 The Replace Module Works In Ansible?

Introduction to Ansible replace

Ansible replaces module is to replace all the instances of the matching string from the path of the file specified. It could be the multiple lines, or single words based on the regular expressions to match the pattern of the string in addition, it provides several options like backup before modifying the file, support replacing before and after line of the matching string, etc.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How does the replace module work in Ansible?

The Ansible replace module is part of the ansible-base and included in all the ansible installations. It replaces all the instances of a pattern within a file.

The Ansible replace module works with several parameters. We will see some basic but important parameters to work with the replace module.

Path: Path of the destination file. Before Ansible version 2.3, it was named as dest, destfile, and name.

Regexp: Regular expressions to look for the content of the file, and based on it, the file content is getting replaced.

Replace: Keyword or string to replace the content inside the file. If this parameter is not set and if the regular expression matches the content of the file, then the entire keyword will be removed in all the instances of the file.

Backup: Its default value is ‘no’. If the value is set ‘yes’, then it will take a backup of the file before modifying so that the existing file can be retained in case of wrong changes. This parameter backs up the file with the name of the file and the timestamp.

Owner: Name of the owner to own the file after replacement. It uses the chown command for the owner’s replacement.

Group: Name of the group to own the file after replacement.

Mode: It specifies the permission of the mentioned file should have.

Before: If mentioned, it replaces the contents before the match and can be combined with the after

After: If mentioned, it replaces the contents after the match and can be combined with the before

There are a few additional parameters that you can check on the ansible website.

To validate the regex expressions online, there are many websites available, and you can pick one from the google search.

Examples of Ansible replace

Given below are the examples of Ansible replace:

Example #1

Replacing the file content with string.

In this example, we will replace the file content on the remote Linux system with the string pattern and without using the regex pattern.

replace: ’That’

The above example will replace the ‘This’ keyword with the ‘That’ keyword in the file /tmp/myfile.txt.

Note: Make sure that the ansible user configured in the inventory file has the proper permission to replace the content of the file.

Output:

Output from the text file.

If we don’t use the replace parameter, then it will replace the matched regex pattern with null.

tasks:

Output from the text file.

If the regex content doesn’t match then, it won’t change the file.

Example #2

Using the regexp to replace the content.

The below example will use the regular expression to replace the file content. Here, we need to replace the host file content on all the Linux servers which have the host file string pattern is String-String (for example, “ansible-host” will be replaced with the ansiblehost string.)

backup: yes

Before making any changes to the original file, the backup parameter will take the backup of the file in the same folder.

Example #3

Replace the file content on the windows servers using the LineinFile module.

As discussed earlier, we can also use the LineInFile module for the file content replacement or adding/appending the new content to the file. We will use the windows module win_lineinfile because no replace module available for windows, so this can be a good module to work with the file content.

This example first inserts the line in the file (if it exists) and then replaces the line with the new line if the keyword is found.

Playbook execution:

ansible-playbook chúng tôi -vvv

Output:

Example #4

Ansible playbook to replace the string and change the file permission.

In this example, we will replace the string from the destination file and also change the user permission on the file with the replace module.

mode: 0644

This example will replace the file content ansible with the ansible-host keyword and change the file’s owner, group, and mode.

Conclusion

Ansible replaces operation is very powerful when we need to deal with the file contents. This is because it can replace the contents of the files on a very large scale. Examples like replacing the hosts’ file content in all windows or Unix servers, changing the web app configurations on all the web servers, etc.

Recommended Articles

This is a guide to Ansible replace. Here we discuss How does the replace module works in Ansible, along with the examples and outputs. You may also have a look at the following articles to learn more –

You're reading How Does The Replace Module Works In Ansible?

Update the detailed information about How Does The Replace Module Works In Ansible? on the Dacvumuahe.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!