我有一个ansible剧本,它接受一个变量,并在其中传递一个带引号的变量(需要它),该变量将用于查询数据库
剧本
- name: Execute clear script
script: scripts/clear-documents.sh {{ids}}
命令
ansible-playbook playbooks/maintenance.yml -i hosts -t clear -e ids=["foo", "bar"]
在此过程脚本中,输入为[foo,bar]而不是[“ foo”,“ bar”]
我尝试使用反斜杠转义,但这无济于事
ansible-playbook playbooks/maintenance.yml -i hosts -t clear -e ids=[\"foo\", \"bar\"]
在剧本中添加双引号,使输入类似“ [foo,bar]”,而不是[“ foo”,“ bar”]
script: scripts/clear-documents.sh "{{ids}}"
我进行了很多搜索,但没有找到适当的解决方案,有没有办法解决这个问题
注意: ansible版本-2.2.3.0
答案 0 :(得分:1)
您要查找的是quote,再加上@JGK正确使用-e ids='["foo", "bar"]'
的原因,因为您没有在进入的方式中引用它们,然后您没有在shell:
任务
- shell: scripts/clear-documents.sh {{ ids | quote }}
答案 1 :(得分:0)
如How to escape backslash and double quote in Ansible (script module)中所述,您需要在shell命令中将{{ ids }}
括在' '
中。
#!/usr/bin/env ansible-playbook
- hosts: localhost
gather_facts: false
become: false
tasks:
- name: Escape characters for fun and profit
vars:
string_list: '["one", "two"]'
shell: "echo '{{ string_list }}'"
register: output1
- name: Print it out
debug:
msg: "{{ output1 }}"
- name: Don't escape characters
vars:
string_list: '["one", "two"]'
shell: "echo {{ string_list }}"
register: output2
- name: Print it out
debug:
msg: "{{ output2 }}"
PLAY [localhost] ************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
TASK [Escape characters for fun and profit] *********************************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Print it out] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "echo '[\"one\", \"two\"]'",
"delta": "0:00:00.003400",
"end": "2019-05-07 12:02:32.897856",
"failed": false,
"rc": 0,
"start": "2019-05-07 12:02:32.894456",
"stderr": "",
"stderr_lines": [],
"stdout": "[\"one\", \"two\"]",
"stdout_lines": [
"[\"one\", \"two\"]"
]
}
}
TASK [Don't escape characters] **********************************************************************************************************************************************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [Print it out] *********************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "echo [\"one\", \"two\"]",
"delta": "0:00:00.002990",
"end": "2019-05-07 12:02:33.192049",
"failed": false,
"rc": 0,
"start": "2019-05-07 12:02:33.189059",
"stderr": "",
"stderr_lines": [],
"stdout": "[one, two]",
"stdout_lines": [
"[one, two]"
]
}
}
PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0
答案 2 :(得分:0)
我有一个带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": []
}