replace or lineinfile ?

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

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.

   - 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

   - debug: msg="{{ hostvars[(ansible_play_hosts | reject('equalto', inventory_hostname) | list)[0]].ansible_default_ipv4.address }}"

Thanx, i will give it a try.