# Vars from hosts?

**URL:** <https://forum.ansible.com/t/vars-from-hosts/18446>\
**Category:** Ansible Project\
**Created:** [June 9, 2015, 3:29pm UTC](https://forum.ansible.com/t/vars-from-hosts/18446 "2015-06-09T15:29:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard\_kappler](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@richard\_kappler](https://forum.ansible.com/u/richard_kappler)\
**Post date:** [June 9, 2015, 3:29pm UTC](https://forum.ansible.com/t/vars-from-hosts/18446/1 "2015-06-09T15:29:32Z")

</div>

Not sure the best way to do this, have read the documentation but still a wee bit befuddled.

We’re ramping up to be able to deploy to several hundred servers. Each server will have an assigned name. Some of the files that will be deployed will need that name, whereas as deployed they have a place holder. I think I have a pretty firm handle on using the replace module. What I’m not sure about is how best to apply the names to the servers and then pull each name out as a var, based on server being presently deployed to, so I can use it in the replace function.

SO, let’s say I have three servers each in Dallas and Baltimore. If I set up my inventory file as follows:

[Dallas]  
server01 ansible\_ssh\_host=192.174.38.12  
server02 ansible\_ssh\_host=someOtherIP  
server03 ansible\_ssh\_host=yetAnotherIP

[Baltimore]  
server04 ansible\_ssh\_host=aFourthIP  
server05 ansible\_ssh\_host=aFifthIP  
server06 ansible\_ssh\_host=LastIP

I have now named each server with a unique name, right? But in the inventory docs it says: “The preferred practice in Ansible is actually not to store variables in the main inventory file.” and I agree. At some point I will want to add more variables.  
So how would I set up a variable file such that it provides a name, based on the IP being deployed to? and how do I reference that file in the playbook so the plays know where to get the vars from? Sorry, very new at all this.

Once I’ve done that, I’m thinking the play to replace the place holder would look something like this?

```auto
- replace:
    dest=abc.conf
    regexp='placeholder'
    replace={{ server_name }}
    backup=yes

```

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [June 9, 2015, 4:25pm UTC](https://forum.ansible.com/t/vars-from-hosts/18446/2 "2015-06-09T16:25:22Z")

</div>

I think you should try using the inventory\_hostname variable for that.  
It'll return those serverXX names you defined.

If you need more vars per host, I would use host\_vars.

[https://docs.ansible.com/playbooks\_best\_practices.html#group-and-host-variables](https://docs.ansible.com/playbooks_best_practices.html#group-and-host-variables)

Giovanni

---

<div class="post-metadata">

**Author:** ![richard\_kappler](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@richard\_kappler](https://forum.ansible.com/u/richard_kappler)\
**Post date:** [June 9, 2015, 6:42pm UTC](https://forum.ansible.com/t/vars-from-hosts/18446/3 "2015-06-09T18:42:53Z")

</div>

I see what you’re saying. Tried to put it into play, but I’m obviously still missing some fundamental knowledge. How do I call the variable?

I tried:

- replace:  
dest=  
regexp=‘placeholder’  
replace={{ inventory\_hostname }}  
backup =no

and it wouldn’t work. I can get the file to work, as an experiment in place of {{ inventory\_hostname }} I put {{ ansible\_eth0.ipv4.address }} and the replace worked, putting the IP address of the target system where ‘placeholder’ used to be, so I’ve got that part down, but calling variables I haven’t got a handle on yet.

Off to do more reading. Thanks for the help Giovanni!

regards, Richard

---

<div class="post-metadata">

**Author:** ![richard\_kappler](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@richard\_kappler](https://forum.ansible.com/u/richard_kappler)\
**Post date:** [June 9, 2015, 6:48pm UTC](https://forum.ansible.com/t/vars-from-hosts/18446/4 "2015-06-09T18:48:09Z")

</div>

Actually, I’m reading Ansible: Up and Running, and under inventory\_hostname it says I did it right, yet when I ran the playbook, I got an error saying inventory\_hostname wasn’t defined. I think maybe I should start a new thread.

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [June 9, 2015, 7:29pm UTC](https://forum.ansible.com/t/vars-from-hosts/18446/5 "2015-06-09T19:29:30Z")

</div>

Try to put quotes around that ("{{ inventory\_hostname }}") so it's  
probably interpreted by Jinja2.

A working example:

---

<div class="post-metadata">

**Author:** ![richard\_kappler](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@richard\_kappler](https://forum.ansible.com/u/richard_kappler)\
**Post date:** [June 9, 2015, 7:43pm UTC](https://forum.ansible.com/t/vars-from-hosts/18446/6 "2015-06-09T19:43:37Z")

</div>

Thanks again for the reply Giovanni.

I tried that, it’s still putting the IP address instead of the server name. Not sure what I’m missing.

regards, Richard
