我有一个带kubectl命令的playbook,当我想运行这个命令时,它不能避开引号并理解这个目录不存在。
---
-
hosts: localhost
vars_files:
- vars/main.yaml
tasks:
-
shell:
cmd: |
kubectl exec -it -n {{ namespace }} {{ pod_name }} -- bash -c \"clickhouse-client --query "INSERT INTO customer FORMAT CSV" --user=test --password=test < /mnt/azure/azure/test/test.tbl\"
register: output2
这就是错误。
fatal: [127.0.0.1]: FAILED! => {
"changed": true,
"cmd": "kubectl exec -it -n ch-test04 chi-test-dashboard-sharded1-dashboard03-3-0-0 -- bash -c \\\"clickhouse-client --query \"INSERT INTO customer FORMAT CSV\" --user=test --password=test < mnt/azure/azure/test/test.tbl\\\"\n",
"delta": "0:00:00.002088",
"end": "2020-04-23 13:30:00.456263",
"invocation": {
"module_args": {
"_raw_params": "kubectl exec -it -n ch-test04 chi-test-dashboard-sharded1-dashboard03-3-0-0 -- bash -c \\\"clickhouse-client --query \"INSERT INTO customer FORMAT CSV\" --user=test --password=test < mnt/azure/azure/test/test.tbl\\\"\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"msg": "non-zero return code",
"rc": 2,
"start": "2020-04-23 13:30:00.454175",
"stderr": "/bin/sh: 1: cannot open mnt/azure/azure/test/test.tbl\": No such file",
"stderr_lines": [
"/bin/sh: 1: cannot open mnt/azure/azure/test/test.tbl\": No such file"
],
"stdout": "",
"stdout_lines": []
}
所以当我把这个命令放在python脚本中时 ansible还是会打引号,并得到同样的错误。我已经尝试过escapequote,但我认为问题是当我使用'<‘字符后查询从插入数据和ansible不能理解整个命令尚未完成。但我不知道如何才能用正确的方法来判断,谢谢。
解决方案:
你引用了错误的字符,你要的是 室内 引号要避开,或者避开那整个乱七八糟的东西,从内到外用备用字。
- shell: |
kubectl exec -i -n {{ namespace }} {{ pod_name }} -- bash -c 'clickhouse-client --query "INSERT INTO customer FORMAT CSV" --user=test --password=test < /mnt/azure/azure/test/test.tbl'