我需要传递可能包含特殊字符的密码,例如%、@、&...在我的ansible攻略中通过shell任务进行API调用。我使用引号‘’或\来转义特殊字符。但并不是所有的都在工作,例如,%....因此,如果我在字符串中找到这些字符,我就在我的剧本中使用' replace‘来根据https://www.w3schools.com/tags/ref_urlencode.ASP用ASCII码替换它们。THe攻略如下所示:
- hosts: localhost
vars:
my_password: "{{ my_pw | replace('%','%25')|replace('@','%40')|replace..... }}"这是可行的。但显然这不是最好的解决方案,因为我必须在这里列出所有特殊字符,以确保不会遗漏任何字符。我正在考虑创建jinja2模板来映射所有基于https://www.w3schools.com/tags/ref_urlencode.ASP的特殊字符。因此,当我需要这个模板时,我可以在我的剧本中调用它。我是jinja2的新手。我将如何实现这一点,或者这是否可能实现?我在使用ansible 2.4。
发布于 2020-12-23 05:45:07
你要找的东西是| urlencode
- hosts: localhost
tasks:
- debug:
msg: encoded password is {{ my_pw | urlencode }}
vars:
my_password: "alpha+beta/charlie@example.com"发射
alpha%2Bbeta/charlie%40example.com
但是,正如文档中提到的,如果需要对/进行编码,则需要在末尾添加| replace("/", "%2F")
https://stackoverflow.com/questions/65415527
复制相似问题