ansible regexp replace | ansible replace module to replace strings between double quote to mask or remove PI - personals information

ansible regular expression, ansible use regexp, ansible regex replace, ansible regex. ansible regex example, ansible regex search examples, ansible replace examples, ansible lineinfile to replace, ansible replace, ansible replace multiple lines, ansible lineinfile newline, ansible remove special characters from string, ansible lineinfile, ansible replace module, replace module, ansible, ansible tutorial, ansible certification, ansible tags, ansible tasks, ansible modules
Ansible regexp - ansible replace module 
Tags:ansible regular expression, ansible use regexp, ansible regex replace, ansible regex. ansible regex example, ansible regex search examples, ansible replace examples, ansible lineinfile to replace, ansible replace, ansible replace multiple lines, ansible lineinfile newline, ansible remove special characters from string, ansible lineinfile, ansible replace module, replace module, ansible, ansible tutorial, ansible certification, ansible tags, ansible tasks, ansible modules,

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 

We have some PI Field in above logs file and we remove it, like

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/ansible
Create a file with yml extension, file name whatever you want
vi mask.yml
Full Content of file 
---
- hosts: 10.0.2.15

  tasks:

  - name: copy file in tmp directory
    copy:
      src: /var/log/event.log
      dest: /tmp/event.log
      remote_src: yes

  - name: mask PI data
    replace:
      path: /tmp/event.log
      regexp: '"{{ 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 directory
    copy:
      src: /var/log/event.log
      dest: /tmp/event.log
      remote_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 data
    replace:
      path: /tmp/event.log
      regexp: '"{{ 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 


  1. Adding Remote Hosts in Ansible Inventory with Authenticate Using 3 Different Method - Ansible Tutorial
  2. Ansible Conditionals When File or Directory Exist | How to Use Condition in Ansible
  3. Ansible Installation in CentOS 7 | Ansible Tutorial in Hindi
  4. Ansible Playbook - My First Ansible Playbook | Define Hosts and Tasks in Ansible Playbook
  5. Ansible Playbook for Get Hosts Information | Ansible Playbook Tutorial
  6. Ansible Tutorial - Ansible Jinja2 Templating
  7. Ansible Tutorial - Ansible Modules Ping Setup Command Shell Copy Find
  8. Ansible Tutorial - How To Define a Ansible Register Variable in Playbook
  9. Ansible Tutorial - How to Use Ansible User and Group Module | Ansible Ad-Hoc Command
  10. Ansible Tutorial - Package Management | Ansible Module YUM | APT
  11. Ansible Tutorial - Service Management Modules | Service Module | Systemd Module
  12. Ansible Tutorial | How to Use Loop in Playbook | Ansible With_item


Thanks you !! 
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.

No comments

Powered by Blogger.