ansible search for string in file or check if string exists in file

The Objective of this post is to show how to search for a string in a file with ansible. ansible provides various ways to accomplish the same.  We will cover,  three major ways to search for a string in a file.

  • Lineinfile module
  • Using the Shell module and grep command
  • Using the Shell module and cat command

Both would work like a charm and the choice is yours.

Let us proceed.

 

Search for a String in a file using Ansible lineinfile module

Lineinfile module is built to validate whether or not a particular line(string) is available in the file but primarily designed to modify the file by adding or updating the line when the string is not found. It can also add or remove a line based on a condition.

With little changes and other parameters, you can tweak lineinfile to not to modify or make changes, but just search for a string and fail or get success based on the result.

lineinfile can only work with a single line and if more occurrences of a Search String found, It always go for the last matching String (or) the last occurrence. But this can be tweaked and modified using its own parameters like firstmatchIt also has provide other customizing options  insert after the matched string or insert beforethe match

It also supports regular expressions using regexp parameters

In order to use lineinfile to just search with no modification, we should use an option called check mode this is also known as "Dry run"

The Playbook to search a string using ansible lineinfile

---
  - name: Search String with lineinfile
    hosts: web

    tasks:
      - name: "Searching for a String"
        become: yes
        become_user: root
        tags: example1
        lineinfile:
          path: /etc/httpd/conf/httpd.conf
          line: "LogLevel debug"
          state: present
        check_mode: yes
        register: presence
        failed_when: presence is changed 

      - name: "sample task in case the String present in the file"
        debug: msg="DEBUG is enabled in Apache Log file"
        when: presence is not changed

The Playbook will fail when the string is not found and display some message when the String is present in the file.

Search for a String in a file using the Shell module grep command

We can easily Search for a String in a file using the basic unix commands like grep and cat, executed over the Shell module. and in this example, we are going to use the grep command

Based on the return code of grep command we are executing, the First task will either be changed or failed, the second task is designed only to GO ON when the first task is changed, which totally depends on the string availability.

When the string is present the first task will be changed it will eventually trigger the second task, which supposed to run only when the string exists in the file.

The Playbook

---
  - name: Search String with lineinfile
    hosts: web

    tasks:
      - name: "Searching for a String"
        become: yes
        become_user: root
        register: presence
        shell: "grep -i 'LogLevel debug' /etc/httpd/conf/httpd.conf"

      - name: "sample task in case the String present in the file"
        debug: msg="DEBUG is enabled in Apache Log file"
        when: presence is changed

Search for a String in a file using shell module cat command

In this example, we are going to use the cat command to display the content of the file it will then be saved into the register variable.

In the second task which supposed to run only when the string is found, on the when condition we are going to do the Logical validation of finding a string using a string in register

---
  - name: Search String with lineinfile
    hosts: web

    tasks:
      - name: "Searching for a String"
        become: yes
        become_user: root
        register: contentofhttpdconf
        shell: "cat /etc/httpd/conf/httpd.conf"

      - name: "sample task in case the String present in the file"
        when: '"LogLevel debug" in contentofhttpdconf.stdout'
        debug: msg="DEBUG is enabled in Apache Log file"

 

These are the different ways to search for a string in a file in Ansible. Hope it helps

Thanks,

SaravAK

Follow me on Linkedin My Profile
Follow DevopsJunction onFacebook orTwitter
For more practical videos and tutorials. Subscribe to our channel

Buy Me a Coffee at ko-fi.com

Signup for Exclusive "Subscriber-only" Content

Loading