replace public ip from /etc/hosts file with private ip using Ansible

I need to replace public ip from /etc/hosts file with private ip in bulk cluster of systems.
Ansible gathered fact , may work but not sure which is for what.

Another way, I can have public and private ip register as variable and swap them.

What would be the best practice to do in ansible way?

Currently in /etc/hosts

Look at the “lineinfile” module.