2

I am using Ansible to create an Azure storage account which must have a max name size of 24 characters. I am looking at the Jinja truncate() method but the parameter passed to this method removes that number of characters rather than limiting the string to that number of characters.

Is there a different way of implementing a max length of string variable?
Do I need to combine truncate and length filters from Jinja?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Johnny
  • 51
  • 4

1 Answers1

3

You could use Python's slicing notation for this.

Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i].

More in the documentation: https://docs.python.org/3/library/functions.html?highlight=slice#slice
Also a good read: https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.html

Given:

- debug:
    msg: "{{ str[:24] }}"
  vars:
    str: abcdefghijklmnopqrstuvwxyz0123456789

This should give you:

abcdefghijklmnopqrstuvwx
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83