提问人:Ashar 提问时间:12/8/2022 最后编辑:ZeitounatorAshar 更新时间:12/8/2022 访问量:218
如何在 Ansible 中将字符串括在单引号内
How to enclose a string within single quotes in Ansible
问:
下面是字符串
ORACLE_THIN PTEST1 my$pass myhost-SCA.mybank.com:1521/OLTP445
密码用单引号括起来的所需输出:
ORACLE_THIN PTEST1 'my$pass' myhost-SCA.mybank.com:1521/OLTP445
以下是我的 ansible 剧本:
- debug:
msg: "utils.dbping string is {{ item.split()[0] ~ ' ' ~ item.split()[1] ~ ' \'' ~ item.split()[2] ~ '\' ' ~ item.split()[3] | trim }}"
loop:
- ORACLE_THIN PTEST1 my$pass myhost-SCA.mybank.com:1521/OLTP445
但是,我在执行时出现语法错误:
违规行似乎是:
- debug:
msg: "utils.dbping string is {{ item.split()[0] ~ ' ' ~ item.split()[1] ~ ' \'' ~ item.split()[2] ~ '\' ' ~ item.split()[3] | trim }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
你能建议吗?
答:
1赞
U880D
12/8/2022
#1
由于没有用例描述,也没有给出任何解释,因此它看起来就像一个语法错误。您可以查看以下示例
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- name: Quote in input
debug:
msg: "utils.dbping string is {{ item.split()[0] ~ ' ' ~ item.split()[1] ~ ' ' ~ item.split()[2] ~ ' ' ~ item.split()[3] | trim }}"
loop:
- ORACLE_THIN PTEST1 'my$pass' myhost-SCA.mybank.com:1521/OLTP445
- name: Quote in output
debug:
msg: "utils.dbping string is {{ item.split()[0] ~ ' ' ~ item.split()[1] ~ ' ' ~ item.split()[2] | quote ~ ' ' ~ item.split()[3] | trim }}"
loop:
- ORACLE_THIN PTEST1 my$pass myhost-SCA.mybank.com:1521/OLTP445
导致输出
TASK [Quote in input] ************************************************************************
ok: [localhost] => (item=ORACLE_THIN PTEST1 'my$pass' myhost-SCA.mybank.com:1521/OLTP445) =>
msg: utils.dbping string is ORACLE_THIN PTEST1 'my$pass' myhost-SCA.mybank.com:1521/OLTP445
TASK [Quote in output] ************************************************************************
ok: [localhost] => (item=ORACLE_THIN PTEST1 my$pass myhost-SCA.mybank.com:1521/OLTP445) =>
msg: utils.dbping string is ORACLE_THIN PTEST1 'my$pass' myhost-SCA.mybank.com:1521/OLTP445
在这两种情况下,都是所需的。
更多文档
-
为 shell 用法添加引号
评论