Ansible Pilot

Search for a String in a File - Ansible module lineinfile

How to automate the search of a string “PasswordAuthentication no” in the “/etc/ssh/sshd_config” file using Ansible Playbook and lineinfile module.

July 7, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

How to Search for a String in a File with Ansible?

I’m going to show you some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible module lineinfile

Today we’re talking about the Ansible module lineinfile. The full name is ansible.builtin.lineinfile, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years and it supports a large variety of operating systems. You are able to insert, update and remove a single line of text in a file.

Parameters

This module has some parameters to perform any tasks. The only required is “path”, where you specify the filesystem path of the file you’re going to edit. “line” is the line of text we would like to insert in the file, easy! By default, the text is going to be inserted at the end of the file, but we could personalize it in a specific position with insertafter/insertbefore. If there is any tool to validate the file we could specify in the validate parameter, very useful for configuration files. If the file does not exist we could also “create” it! Usually, we would like to insert a text line but we could also remove using state in conjunction with parameter absent. Let me also highlight that we could also specify some permissions or SELinux properties.

The Best Resources For Ansible

Video Course

Books

demo

How to Search for a String in a File. How to search for a pattern in a file and return the result using only the Ansible built-in lineinfile module.

code

---
- name: search demo
  hosts: all
  vars:
    myfile: "/etc/ssh/sshd_config"
    myline: 'PasswordAuthentication no'
  become: true
  tasks:
    - name: string found
      ansible.builtin.lineinfile:
        name: "{{ myfile }}"
        line: "{{ myline }}"
        state: present
      check_mode: true
      register: conf
      failed_when: (conf is changed) or (conf is failed)

string present

$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] devops]# grep 'PasswordAuthentication no' /etc/ssh/sshd_config 
PasswordAuthentication no
[[email protected] devops]#
$ ansible-playbook -i virtualmachines/demo/inventory file_management/file_search.yml
PLAY [search demo] ********************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [string found] *******************************************************************************
ok: [demo.example.com]
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

string different

$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] devops]# vim /etc/ssh/sshd_config 
[[email protected] devops]# grep 'PasswordAuthentication' /etc/ssh/sshd_config 
PasswordAuthentication yes
[[email protected] devops]#
$ ansible-playbook -i virtualmachines/demo/inventory file_management/file_search.yml
PLAY [search demo] ********************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [string found] *******************************************************************************
fatal: [demo.example.com]: FAILED! => {"backup": "", "changed": true, "failed_when_result": true, "msg": "line added"}
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

file not present

$ ssh [email protected]
[[email protected] ~]$ sudo su
[[email protected] ssh]# ls -al /etc/ssh/sshd_config
ls: cannot access '/etc/ssh/sshd_config': No such file or directory
[[email protected] ssh]#
$ ansible-playbook -i virtualmachines/demo/inventory file_management/file_search.yml
PLAY [search demo] ********************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: [demo.example.com]
TASK [string found] *******************************************************************************
fatal: [demo.example.com]: FAILED! => {"changed": false, "failed_when_result": true, "msg": "Destination /etc/ssh/sshd_config does not exist !", "rc": 257}
PLAY RECAP ****************************************************************************************
demo.example.com           : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

code with ❤️ in GitHub

Recap

Now you know how to Search for a String in a File with Ansible and how you could use successfully in your Playbook. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 200+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Trustpilot
Follow me

Subscribe not to miss any new releases

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress