正如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