Variable to copy ansible inventory file

Following the DRY principle, I'd like to copy ansible's host file to managed hosts so tools on there can find critical servers, e.g.

[zookeepers]
192.168.2.2
192.168.2.3
192.168.2.4

And then tools can parse this for the list of zookeepers when they start up to know who to talk to.

I looked for a variable that I could use for the copy module but didn't find any. I use use a template for this, but that seems like extra work.

Thanks,
Blair

Are you saying you want to use ansible's host file to generate
/etc/hosts on the remotes?

Following the DRY principle, I'd like to copy ansible's host file to
managed hosts so tools on there can find critical servers, e.g.

[zookeepers]
192.168.2.2
192.168.2.3
192.168.2.4

And then tools can parse this for the list of zookeepers when they
start up to know who to talk to.

I don't believe that's possible and even so, I think you're better off
creating a special file on your target servers. I do this with something
like this:

            - action: template src=hostfile.in dest=/tmp/hostfile

with the template (hostfile.in) containing

        {% for k, v in hostvars.iteritems() -%}
        {{ hostvars[k]['ansible_default_ipv4']['address'] }} {{ k }} {{ v['ansible_hostname'] }}
        {% endfor %}

Check for the exact syntax to get your 'zookeepers' group.

        -JP

I don’t want to replace /etc/hosts, but to make a file named /opt/orcaware/etc/hosts.ini, and all the tools in /opt/orcaware would read it.

Blair

This file would be picked up by other scripts to find the zookeeper hostname/IP addresses, so it doesn’t need to be in zookeeper format.

Even in this case, having a path to hostfile.in that comes from the env or the -i command line argument would be easier.

I agree with what you’re saying though, if you have a single file and you add another, say PostgreSQL host, then you would want to bounce or kill -HUP the processes using the Zookeeper IPs, even though those haven’t changed, so splitting up each set of services into a separate file lets you use Ansible changed feature to avoid spurious bounces.

Blair

You are able to traverse groups in a Jinja2 for loop and access
variables via hostvars.

But you're going to want to build a template.

I think this is fine, really.

But is there a variable to use even to pass to src= for a template?

- action: template src=${anside_something} dest=/tmp/hostfile

You can absolutely pass variables to anything in an action line.

I know, I use them :slight_smile: But I couldn't find one that points to the hosts file that ansible uses, is there one?

there is not.