【问题标题】:How to escape double-quotes in ansible variable如何在ansible变量中转义双引号
【发布时间】:2019-05-07 15:11:17
【问题描述】:

我有一个 ansible playbook,它接受一个变量,传递一个带引号的变量(它需要),该变量将用于查询 DB

剧本

- 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\"]

在 playbook 中添加双引号,使输入类似于 "[foo,bar]" 而不是 ["foo", "bar"]

script: scripts/clear-documents.sh "{{ids}}"

我搜索了很多但没有得到任何合适的解决方案,有没有办法处理这个

注意: ansible 版本 - 2.2.3.0

【问题讨论】:

标签: ansible


【解决方案1】:

您正在寻找的是quote,结合@JGK 对-e ids='["foo", "bar"]' 的正确用法,因为您没有在进入 ansible 的过程中引用它们,然后您没有引用他们在shell: 任务中退出 ansible

- shell: scripts/clear-documents.sh {{ ids | quote }}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我有带有 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": []
      }
      

      【讨论】:

        猜你喜欢
        • 2021-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-28
        • 1970-01-01
        • 1970-01-01
        • 2011-04-19
        相关资源
        最近更新 更多