replace or lineinfile ?

14 views
Skip to first unread message

Henrik Schuetze

unread,
Aug 10, 2018, 5:09:44 AM8/10/18
to Ansible Project
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

Kai Stian Olstad

unread,
Aug 10, 2018, 6:19:52 AM8/10/18
to ansible...@googlegroups.com
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 }}"

--
Kai Stian Olstad

Henrik Schuetze

unread,
Aug 10, 2018, 7:01:05 AM8/10/18
to Ansible Project
Thanx, i will give it a try.
Reply all
Reply to author
Forward
0 new messages