使用kubectl或其他类似命令、脚本的易解析模块在pod中运行shell命令



我有一个kubectl命令的剧本,当我想运行这个命令时,它无法避免引号,也无法理解这个目录不存在

--- 
- 
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仍然手动引用,并得到了同样的错误。我已经尝试过转义/引号,但我认为问题是当我使用"&lt从插入数据到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'

最新更新