# Variable to copy ansible inventory file

**URL:** <https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287>\
**Category:** Ansible Project\
**Created:** [February 3, 2013, 9:37pm UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287 "2013-02-03T21:37:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 3, 2013, 9:37pm UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/1 "2013-02-03T21:37:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 3, 2013, 9:41pm UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/2 "2013-02-03T21:41:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jpmens1](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jpmens1](https://forum.ansible.com/u/jpmens1)\
**Post date:** [February 4, 2013, 7:08am UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/3 "2013-02-04T07:08:58Z")

</div>

> 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:

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

with the template (hostfile.in) containing

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for k, v in hostvars.iteritems() -%}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ hostvars[k]['ansible\_default\_ipv4']['address'] }} {{ k }} {{ v['ansible\_hostname'] }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 4, 2013, 9:30pm UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/4 "2013-02-04T21:30:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 4, 2013, 9:32pm UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/5 "2013-02-04T21:32:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 4, 2013, 9:52pm UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/6 "2013-02-04T21:52:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 5, 2013, 12:44am UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/7 "2013-02-05T00:44:32Z")

</div>

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

- action: template src=${anside\_something} dest=/tmp/hostfile

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 5, 2013, 1:05am UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/8 "2013-02-05T01:05:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [February 5, 2013, 1:10am UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/9 "2013-02-05T01:10:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [February 5, 2013, 11:51am UTC](https://forum.ansible.com/t/variable-to-copy-ansible-inventory-file/12287/10 "2013-02-05T11:51:35Z")

</div>

there is not.
