# replace or lineinfile ?

**URL:** <https://forum.ansible.com/t/replace-or-lineinfile/28307>\
**Category:** Ansible Project\
**Created:** [August 10, 2018, 9:09am UTC](https://forum.ansible.com/t/replace-or-lineinfile/28307 "2018-08-10T09:09:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_Schuetze](https://avatars.discourse-cdn.com/v4/letter/h/e9bcb4/32.png) [@Henrik\_Schuetze](https://forum.ansible.com/u/Henrik_Schuetze)\
**Post date:** [August 10, 2018, 9:09am UTC](https://forum.ansible.com/t/replace-or-lineinfile/28307/1 "2018-08-10T09:09:44Z")

</div>

For a replication project i have to dynamic create an .sql for execute with mariadb.  
i have two hosts (A and B) in my host variables, both with name, ip and ID.

i think about create a local file, copy it to the destination hosts and then ?  
i need to put the ip address from Server A in the file for Server B.  
And the Adress from Server B in the file for Server A.

Does anyone has an idea how to do this quickly?

greetings  
Henrik

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 10, 2018, 10:19am UTC](https://forum.ansible.com/t/replace-or-lineinfile/28307/2 "2018-08-10T10:19:41Z")

</div>

There is many ways to do this, it all comes down to your environment.  
In any case you should probably use the template module.

If this is a play that only has two hosts A and B, you could use this to find the other host.

&nbsp;&nbsp;&nbsp;- debug: msg="{{ (ansible\_play\_hosts | reject('equalto', inventory\_hostname) | list)[0] }}"

And to get the IP from Ansible fact on the other host that would be

&nbsp;&nbsp;&nbsp;- debug: msg="{{ hostvars[(ansible\_play\_hosts | reject('equalto', inventory\_hostname) | list)[0]].ansible\_default\_ipv4.address }}"

---

<div class="post-metadata">

**Author:** ![Henrik\_Schuetze](https://avatars.discourse-cdn.com/v4/letter/h/e9bcb4/32.png) [@Henrik\_Schuetze](https://forum.ansible.com/u/Henrik_Schuetze)\
**Post date:** [August 10, 2018, 11:01am UTC](https://forum.ansible.com/t/replace-or-lineinfile/28307/3 "2018-08-10T11:01:04Z")

</div>

Thanx, i will give it a try.
