convert int index to alphabet letter

18 views
Skip to first unread message

Quentin Aveno

unread,
Jun 5, 2020, 12:15:12 PM6/5/20
to Ansible Project
Hi everyone,
(It's more a jinja quesion but...)

Is there a way to convert a int to an alphabet letter (1 -> a, 2 -> b...)

More aesthetic than (with a filter and less useless variables):


letters: 'abcdefghijklmnopqrstuvwxyz'
index: "{{ groups['group1'].index(inventory_hostname) }}"
index_letter: "{{ letters[index] }}"

Thanks

Dick Visser

unread,
Jun 5, 2020, 2:05:35 PM6/5/20
to ansible...@googlegroups.com
There is no existing filter that does this, but nothing prevents you
from writing one...
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6ae3c9ee-c197-487f-89d6-9673143952f6o%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Dick Visser

unread,
Jun 5, 2020, 4:56:59 PM6/5/20
to ansible...@googlegroups.com
Something like this in your filters file:

def num2let(arg):
return 'abcdefghijklmnopqrstuvwxyz'[arg]
def num2let0(arg):
return 'abcdefghijklmnopqrstuvwxyz'[arg-1]
class FilterModule(object):
def filters(self):
return {
'num2let': num2let,
'num2let0': num2let0,
}


Then it's:

index: "{{ groups['group1'].index(inventory_hostname) | num2let0 }}"
Reply all
Reply to author
Forward
0 new messages