【问题标题】:How to escape JSON in Ansible playbook如何在 Ansible 剧本中转义 JSON
【发布时间】:2021-10-31 04:40:59
【问题描述】:

我有以下 YAML Ansible 剧本文件,我打算用它来从我的 docker 容器中捕获一些信息:

---
# Syntax check:  /usr/bin/ansible-playbook --syntax-check --inventory data/config/host_inventory.yaml data/config/ansible/docker_containers.yaml
- hosts: hosts
  gather_facts: no
  tasks:
  - name: Docker ps output - identify running containers
    shell: "/usr/bin/docker ps --format '{\"ID\":\"{{ .ID }}\", \"Image\": \"{{ .Image }}\", \"Names\":\"{{ .Names }}\"}'"
    register: docker_ps_output
  - name: Show content of docker_ps_output
    debug:
       msg: docker_ps_output.stdout_lines

但是转义不起作用,当我尝试运行 playbook 时,Ansible 给了我中指:

PLAY [hosts] ***********************************************************************************************************************************************************

TASK [Docker ps output - identify running containers] **********************************************************************************************************************************************
fatal: [myhost.com]: FAILED! => {"msg": "template error while templating string: unexpected '.'. String: /usr/bin/docker ps --format ''{\"ID\":\"{{ .ID }}\", \"Image\": \"{{ .Image }}\", \"Names\":\"{{ .Names }}\"}''"}
        to retry, use: --limit @/tmp/docker_containers.retry

PLAY RECAP *****************************************************************************************************************************************************************************************
myhost.com : ok=0    changed=0    unreachable=0    failed=1

我尝试运行的原始命令:

/usr/bin/docker ps --format '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}"}'

【问题讨论】:

    标签: docker ansible yaml escaping


    【解决方案1】:

    我建议使用块标量。您的问题是 {{ .ID }} 等被 Ansible 的 Jinja 模板引擎处理时,它不应该。可能最易读的方法是:

    ---
    # Syntax check:  /usr/bin/ansible-playbook --syntax-check --inventory data/config/host_inventory.yaml data/config/ansible/docker_containers.yaml
    - hosts: hosts
      gather_facts: no
      tasks:
      - name: Docker ps output - identify running containers
        shell: !unsafe >-
          /usr/bin/docker ps --format
          '{"ID":"{{ .ID }}", "Image": "{{ .Image }}", "Names":"{{ .Names }}"}'
        register: docker_ps_output
      - name: Show content of docker_ps_output
        debug:
           msg: docker_ps_output.stdout_lines
    

    >- 开始一个折叠块标量,您不需要在其中转义任何内容,并且换行符被折叠成空格。标签!unsafe 阻止使用Jinja 处理该值。

    【讨论】:

    • 这很棒,很干净。感谢分享!
    【解决方案2】:

    如果你想避免模板化,你需要用另一个双括号覆盖双括号:

    {{ thmthng }}
    

    应该看起来像:

    {{ '{{' }} thmthng {{ '}}' }}
    

    你的剧本:

    ---
    - hosts: hosts
      gather_facts: no
    
      tasks:
      - name: Docker ps output - identify running containers
        shell: "docker ps -a --format '{\"ID\": \"{{ '{{' }} .ID {{ '}}' }}\", \"Image\": \"{{ '{{' }} .Image {{ '}}' }}\", \"Names\" : \"{{ '{{' }} .Names {{ '}}' }}}\"'"
        register: docker_ps_output
    
      - name: Show content of docker_ps_output
        debug:
           var: docker_ps_output.stdout_lines
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 2018-09-21
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2020-01-18
      相关资源
      最近更新 更多