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

**URL:** <https://forum.ansible.com/t/replace-public-ip-from-etc-hosts-file-with-private-ip-using-ansible/22402>\
**Category:** Ansible Project\
**Created:** [August 26, 2016, 6:18pm UTC](https://forum.ansible.com/t/replace-public-ip-from-etc-hosts-file-with-private-ip-using-ansible/22402 "2016-08-26T18:18:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Auto-mate](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@Auto-mate](https://forum.ansible.com/u/Auto-mate)\
**Post date:** [August 26, 2016, 6:18pm UTC](https://forum.ansible.com/t/replace-public-ip-from-etc-hosts-file-with-private-ip-using-ansible/22402/1 "2016-08-26T18:18:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [August 29, 2016, 6:02pm UTC](https://forum.ansible.com/t/replace-public-ip-from-etc-hosts-file-with-private-ip-using-ansible/22402/2 "2016-08-29T18:02:15Z")

</div>

Look at the “lineinfile” module.
