Lineinfile: path: /etc/httpd/conf/nfīackup: yes check_mode: yes register: example1out Tasks: - name: "Example1: Validate if a String or line is present in the file" become: yes become_user: root This is being done with the help of checkmode=yes - name: Examples of lineinfile In this example we are going to check if the LogLevel is Debug and print the message If it is there or not and take no action. There would be no action taken whatsoever The Example given below is to find whether or not the String “LogLevel debug” is found in the remote apache web server’s nf file.Īs mentioned earlier.
this is just like running the quick find command It will not modify the file irrespective of whatsoever the result is. This is just to validate if a line is present in the file or not. validate the changes are correct before savingĮxample 1: Validate if a line is present in the file without any modification.Insert after the matching line using insertafter parameter.Insert before a matching line using insertbefore parameter.Remove a line from the file if it is found – All the instances.Replace a Line in a file If it is found with ansible lineinfile.
Validate if a line is present in the file and add if it does not exist.Validate if a line is present without any modification.you can choose to read all or any specific example. These are examples we have covered in this post. We have gathered various examples of ansible lineinfile here. If you want to explore more about the Ansible Replace module you can refer this post.Īnsible replace line in file – Ansible Replace Examples | Devops Junction If you want to replace mutiple lines try replace module or use blockinfile if you want to insert/update/remove a block of lines in a file. Before we proceed with the examples Something to be highlighted.Īnsible Lineinfile can be used only for working a single line in a file. Remove a username from /etc/passwd file using regex.
Upgrade the package version when the installed version is matching your regular expression.Disable the SSL when the SSL is enabled.Change the port number in the configuration file.Add a line when it is not already present.You can reuse and modify the matched line using the back reference parameter.Ĭonsider yourself having any of these following requirement You can also use conditions to match the line before modifying or removing using the regular expressions. Example 7: Validate the Changes Before Saving/CommittingĪnsible lineinfile module is helpful when you want to add, remove, modify a single line in a file.Example6: Insert Before a matching line using insertbefore parameter.Example5: Insert After a matching line using insertafter parameter.Example 4: Remove a line from the file, if it is found ( All the Instances ).Would it Replace all the Matching Lines? What if there are More than one Matches.Example3: Replace a line in a file with ansible lineinfile.Example2: Validate if a String or line is present in the file and add if it does not exist.Example 1: Validate if a line is present in the file without any modification.