Vars from hosts?

37 views
Skip to first unread message

richard kappler

unread,
Jun 9, 2015, 11:29:32 AM6/9/15
to ansible...@googlegroups.com
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?

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

Giovanni Tirloni

unread,
Jun 9, 2015, 12:25:30 PM6/9/15
to ansible...@googlegroups.com
On Tue, 09 Jun 2015 08:29 -0700, richard kappler <richk...@gmail.com>
wrote:
> 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.

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

Giovanni

richard kappler

unread,
Jun 9, 2015, 2:42:53 PM6/9/15
to ansible...@googlegroups.com
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=<filename>
    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

richard kappler

unread,
Jun 9, 2015, 2:48:10 PM6/9/15
to ansible...@googlegroups.com
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.

Giovanni Tirloni

unread,
Jun 9, 2015, 3:29:39 PM6/9/15
to ansible...@googlegroups.com
Try to put quotes around that ("{{ inventory_hostname }}") so it's
probably interpreted by Jinja2.

A working example:

---
- hosts: localhost
gather_facts: no
tasks:
- debug:
msg: "{{ inventory_hostname }}"


$ ansible-playbook -i "localhost," play.yml

PLAY [localhost]
**************************************************************

TASK: [debug ]
****************************************************************
ok: [localhost] => {
"msg": "localhost"
}

PLAY RECAP
********************************************************************
localhost : ok=1 changed=0 unreachable=0
failed=0


On Tue, 09 Jun 2015 11:48 -0700, richard kappler <richk...@gmail.com>
wrote:
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/a7383975-1dcc-4fac-9ee1-e8ad0b9c62d4%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

richard kappler

unread,
Jun 9, 2015, 3:43:41 PM6/9/15
to ansible...@googlegroups.com
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

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/9GaoX_RcG6w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Windows assumes you are an idiot…Linux demands proof.

Reply all
Reply to author
Forward
0 new messages