Hi@MD,
You can use lineinfile module in Ansible. This module has the capability to support regex expression. So you can create a regular expression and do your task as shown below.
- name: Replace a localhost entry with our own
lineinfile:
path: /etc/hosts
regexp: '^127\.0\.0\.1'
line: 127.0.0.1 localhost