我正在使用 Ansible 创建一个 Azure 存储帐户,该帐户的最大名称大小必须为 24 个字符。我正在查看 Jinja truncate()
方法,但传递给此方法的参数会删除该数量的字符,而不是将字符串限制为该数量的字符。
是否有不同的方法来实现字符串变量的最大长度?
我是否需要结合 Jinja 的 truncate
和 length
过滤器?
最佳答案
您可以使用 Python's slicing notation 来实现此目的。
Slice objects are also generated when extended indexing syntax is used. For example:
a[start:stop:step]
ora[start:stop, i]
.
文档中的更多内容:https://docs.python.org/3/library/functions.html?highlight=slice#slice
也值得一读:https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.html
给定:
- debug:
msg: "{{ str[:24] }}"
vars:
str: abcdefghijklmnopqrstuvwxyz0123456789
这应该给你:
abcdefghijklmnopqrstuvwx
关于azure - 设置字符串变量的最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64242535/