Ansible regexp - ansible replace module |
This is a automation staff using ansible, we used replace module of ansible and replace some value using a regular expression with loop so in this topic we will cover below points
1 - how to use regex and replace particular fields from files ?
2 - how to use ansible replace module and remove information from file ?
3 - how to ansible regex search
4 - ansible replace module with regex and remove.
To learn more, visit our previews topic regarding the regular expression
Regular expression Part 1 - Print number/digit, character, alphanumeric and special character
Regular expression Part 2 - Print all between double quote / print wildcard or all character between
Ansible modules
1 - replace
2 - copy
Logs file path
/var/logs/event.logs
Value we have to replace
accountNumber
operator
custName
custID
cardHolder
card
Phone
Address
Watch complete video of this topic :
To check ansible version
ansible --version
To Create a playbook ( To know more about playbook creation click here )
Go to the ansible directory
cd /etc/ansibleCreate a file with yml extension, file name whatever you want
vi mask.yml
Full Content of file
---- hosts: 10.0.2.15tasks:- name: copy file in tmp directorycopy:src: /var/log/event.logdest: /tmp/event.logremote_src: yes- name: mask PI datareplace:path: /tmp/event.logregexp: '"{{ item }}":"[^"]*"'replace: '"{{ item }}":"REMOVED"'loop:- accountNumber- Address- operator- custName- cardHolder- card- Phone- custID
Explanations :
First tags we define a hosts, whew we want to execute the job and mask data
---- hosts: 10.0.2.15
How to setup hosts / inventory - Click here
Second tags we defined the tasks for playbook, our first task should be create a backup before doing any configuration, so we used copy module to copy a file from actual location to /tmp for masking the data.
tasks:- name: copy file in tmp directorycopy:src: /var/log/event.logdest: /tmp/event.logremote_src: yes
In second plays we used replace module with loop because we have multiple values in the file
How to use loop in ansible - Click Here
- name: mask PI datareplace:path: /tmp/event.logregexp: '"{{ item }}":"[^"]*"'replace: '"{{ item }}":"REMOVED"'loop:- accountNumber- Address- operator- custName- cardHolder- card- Phone- custID
To execute above playbook
ansible-playbook mask.yml
So we successfully changed all the fields, nothing skipped
- Adding Remote Hosts in Ansible Inventory with Authenticate Using 3 Different Method - Ansible Tutorial
- Ansible Conditionals When File or Directory Exist | How to Use Condition in Ansible
- Ansible Installation in CentOS 7 | Ansible Tutorial in Hindi
- Ansible Playbook - My First Ansible Playbook | Define Hosts and Tasks in Ansible Playbook
- Ansible Playbook for Get Hosts Information | Ansible Playbook Tutorial
- Ansible Tutorial - Ansible Jinja2 Templating
- Ansible Tutorial - Ansible Modules Ping Setup Command Shell Copy Find
- Ansible Tutorial - How To Define a Ansible Register Variable in Playbook
- Ansible Tutorial - How to Use Ansible User and Group Module | Ansible Ad-Hoc Command
- Ansible Tutorial - Package Management | Ansible Module YUM | APT
- Ansible Tutorial - Service Management Modules | Service Module | Systemd Module
- Ansible Tutorial | How to Use Loop in Playbook | Ansible With_item
I hope this topic gave you all the information you needed. If you have any further questions or would like more detailed directions feel free to contact us using any of the following sources. We look forward to talking to you.