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

On Thursday, December 25, 2014 10:43:10 PM UTC-8, Chris Church wrote:
>
> 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
>
>
>
> On Thu, Dec 25, 2014 at 8:59 PM, <[email protected] <javascript:>> 
> wrote:
>
>> 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
>>
>>
>> On Wednesday, December 24, 2014 7:14:08 PM UTC-6, Bevan Bennett wrote:
>>>
>>> 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?
>>>
>>  -- 
>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/57dcfa68-14e7-4c84-98d3-021236cc235d%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/ansible-project/57dcfa68-14e7-4c84-98d3-021236cc235d%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/315b1a09-6b29-456a-993a-7869ca148dd6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to