# How to get a server's identifier

**URL:** <https://forum.ansible.com/t/how-to-get-a-servers-identifier/17143>\
**Category:** Ansible Project\
**Created:** [December 25, 2014, 1:14am UTC](https://forum.ansible.com/t/how-to-get-a-servers-identifier/17143 "2014-12-25T01:14:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bevan\_Bennett](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Bevan\_Bennett](https://forum.ansible.com/u/Bevan_Bennett)\
**Post date:** [December 25, 2014, 1:14am UTC](https://forum.ansible.com/t/how-to-get-a-servers-identifier/17143/1 "2014-12-25T01:14:08Z")

</div>

We have a lot of config that wants to fill in templates based on a particular server’s “number” within a certain group.

For example, if we have an inventory file with:

`  
[fooservers]  
steve  
larry  
frank  
eleanor

`

…we then want to configure steve’s templates with “foo01”, larry with “foo02”, frank with “foo03”, etc.  
In most cases I’ve been using the following pattern inside of templates:

`{% for pt in groups.ptservers %} {% if inventory_hostname == pt %} special_identifier = foo{{ '%02d' % loop.index }} {% endif %} {% endfor %}`

This has worked well for limited use, but is there an easier way to store the server’s associated loop\_index for reuse at either the playbook or template level?  
We have reached a point where we have a template that needs to use this value 27 times, and I don’t want to have to clutter everything up with 27 loops…

I tried using {% set %} in the template inside of the loop. but set has local scope, so it’s unusable afterwards.

Has anyone else come up with a good solution for this pattern or do I just need to make a really really ugly template?

---

<div class="post-metadata">

**Author:** ![titleistfour](https://avatars.discourse-cdn.com/v4/letter/t/65b543/32.png) [@titleistfour](https://forum.ansible.com/u/titleistfour)\
**Post date:** [December 26, 2014, 1:59am UTC](https://forum.ansible.com/t/how-to-get-a-servers-identifier/17143/2 "2014-12-26T01:59:11Z")

</div>

Can you just use a host variable in your inventory and then reference that in your template?

[fooservers]  
steve special\_identifier=01  
larry special\_identifier=02  
frank special\_identifier=03  
eleanor special\_identifier=04

Jay

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [December 26, 2014, 6:42am UTC](https://forum.ansible.com/t/how-to-get-a-servers-identifier/17143/3 "2014-12-26T06:42:47Z")

</div>

Use set\_fact to initially define the variable for each host, then you can reference the variable name wherever you need it later:

- hosts: fooservers  
tasks:
- set\_fact:  
foo\_var: “{% for pt in groups.fooservers %}{% if inventory\_hostname == pt %}foo{{ ‘%02d’ % loop.index }}{% endif %}{% endfor %}”
- debug: var=foo\_var

---

<div class="post-metadata">

**Author:** ![Bevan\_Bennett](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@Bevan\_Bennett](https://forum.ansible.com/u/Bevan_Bennett)\
**Post date:** [December 31, 2014, 7:28pm UTC](https://forum.ansible.com/t/how-to-get-a-servers-identifier/17143/4 "2014-12-31T19:28:54Z")

</div>

Thanks Chris!  
That’s got it working!!!

@Jay: I was hoping to avoid that and just have servers magically renumber themselves as they get added and removed from inventory… it’s one fewer thing that might get misconfigured…
