Score:0

Want to Use Ansible Lineinfile Module to Insert Private IP Addresses from Hosts in the zookeeperservers Inventory Group

kw flag

I need to modify a line in a configuration file with the private IP addresses of the hosts in the zookeeperservers inventory group.

The config file is an .xml file.

The key word in the config file is "Connect String">.

There are 3 hosts in the zookeeperservers inventory group i.e., zoo1, zoo2, and zoo3.

I would like to insert a line immediately after "Connect String"> (i.e., immediately after the ">" symbol, and what I would like to insert is private ip address of zoo1:2181,private ip address of zoo2:2181, private ip address of zoo3:2181.

So the line to be added to this line immediately after the > would look like this:

10.42.239.2:2181,10.42.227.227:2181,10.42.237.9:2181.

The values above :2181 would be a string. I would like to use values from ansible to populate the private ip addresses.

Can someone provide guidance on how I can do this?

HBruijn avatar
in flag
[Server Fault is a site for information technology professionals](http://serverfault.com/help/on-topic) -- as such we have certain professional expectations when people ask a question here, and one of those expectations is that your question shows you did some **research**, found and **read the vendor documentation** and **tried a solution** before asking the internet for help. You're not expected to solve everything yourself, but then at least you can [write a great question](http://meta.serverfault.com/a/3609/37681) that we can help you answer. - Or: *What have you tried that isn't working?*
Score:1
in flag

In your zookeeper configuration template you need to loop over your zookeepers host group. Like so:

{% for host in groups['zookeeperservers'] %}
   {{ hostvars[host]['ansible_default_ipv4']['address'] }}:2181{% if not loop.last %},{% endif %}
{% endfor %}

The if not loop.last %},{% endif %} will add a comma delimiter between the host entries and skip the last comma.

David S avatar
kw flag
Thank you Henrik! I ran the following command on the resultant output file to get rid of the spaces: cat zoostring.txt | tr -d "\t\n\r" > zoostring2.txt.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.