AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1059481
Accepted
Lethargos
Lethargos
Asked: 2021-04-06 23:48:22 +0800 CST2021-04-06 23:48:22 +0800 CST 2021-04-06 23:48:22 +0800 CST

ansible - docker 容器中环境变量的模板 json

  • 772

我正在尝试将单行 json 字符串分配给 docker 容器中的环境变量。这是json的样子:

{"ip_access": {"IP_whitelist": {"192.168.99.19/32": "grafana/status (Provider)"}}, "vhosts": {"prometheus1": {"dns_names": ["prometheus1.company.internal"], "add_lines_443": ["include IP_whitelist;", "set $prometheus http://prometheus:9090;", "location / { proxy_pass $prometheus; }"], "options": {"cert_path": "/etc/ssl/certs/prometheus1.crt", "key_path": "/etc/ssl/private/prometheus1.key"}}}}

因此,["prometheus1.company.internal"]我不想拥有["{{ inventory_hostname }} .company.internal"](以及其他prometheus1.

我正在使用 docker_container 如下:

- name: create nginx reverse proxy container
  docker_container:
    image: registry.company.com/devops/nginx-reverseproxy:{{ nginx_version }}
    name: nginx-reverseproxy
    labels:
      role=metrics
    volumes:
      - /etc/ssl/certs/{{ inventory_hostname }}.crt:/etc/ssl/certs/{{ inventory_hostname }}.crt
      - /etc/ssl/private/{{ inventory_hostname }}.key:/etc/ssl/private/{{ inventory_hostname }}.key
    container_default_behavior: compatibility
    networks_cli_compatible: yes
    network_mode: default
    purge_networks: yes
    networks:
      - name: metrics-net
      - name: proxy-net
    env:
      STAGING_ENVIRONMENT: 'production'
      NGINX_VHOSTS_JSON: '{{ lookup("template", "rproxy/nginx_vhosts_prometheus_develop.j2") }}'

不幸的是,我不断得到:

TASK [prometheus : create nginx reverse proxy container] **********************************************
fatal: [prometheus_vag]: FAILED! => {"changed": false, "msg": "Non-string value found for env option. Ambiguous env options must be wrapped in quotes to avoid them being interpreted. Key: NGINX_VHOSTS_JSON"}

奇怪的是,如果我只是使用模板模块,它会按预期工作:

  template:
    src: rproxy/nginx_vhosts_prometheus_develop.j2
    dest: /tmp/tempo.json
  when: "prometheus_host in inventory_hostname"
  tags:
    - copytmp

inventory_hostname将被库存中的实际价值取代,我得到了正确的东西。此外,如果我将这个确切的结果作为 yaml playbook 中 NGINX_VHOSTS_JSON 的值粘贴到 playbook 中,它也可以正常工作。

但是查找模板似乎没有提供预期的字符串。

有什么想法可以解决这个问题吗?

json ansible jinja2
  • 2 2 个回答
  • 811 Views

2 个回答

  • Voted
  1. Best Answer
    flowerysong
    2021-04-07T06:20:00+08:002021-04-07T06:20:00+08:00

    当它们看起来像结构的字符串表示时,Ansible 的模板通常会自动将它们转换为数据结构。

    在这种情况下,您可以通过在需要时将其显式转换为 JSON 来避免这种情况:

    NGINX_VHOSTS_JSON: '{{ lookup("template", "rproxy/nginx_vhosts_prometheus_develop.j2") | to_json }}'
    

    就个人而言,我会以更人性化的表示形式(例如 YAML)对其进行模板化,然后将其转换为 JSON:

    test.j2:

    ip_access:
      IP_whitelist:
        192.168.99.19/32: grafana/status (Provider)
    vhosts:
      {{ inventory_hostname }}.company.internal:
        add_lines_443:
        - include IP_whitelist;
        - set $prometheus http://prometheus:9090;
        - location / { proxy_pass $prometheus; }
        dns_names:
        - {{ inventory_hostname }}.company.internal
        options:
          cert_path: /etc/ssl/certs/{{ inventory_hostname }}.company.internal.crt
          key_path: /etc/ssl/private/{{ inventory_hostname }}.company.internal.key
    

    test.yml:

    - hosts: localhost
      gather_facts: false
      tasks:
        - debug:
            msg: "{{ lookup('template', 'test.j2') | from_yaml | to_json }}"
    

    结果:

    ok: [localhost] => {
        "msg": "{\"ip_access\": {\"IP_whitelist\": {\"192.168.99.19/32\": \"grafana/status (Provider)\"}}, \"vhosts\": {\"localhost.company.internal\": {\"add_lines_443\": [\"include IP_whitelist;\", \"set $prometheus http://prometheus:9090;\", \"location / { proxy_pass $prometheus; }\"], \"dns_names\": [\"localhost.company.internal\"], \"options\": {\"cert_path\": \"/etc/ssl/certs/localhost.company.internal.crt\", \"key_path\": \"/etc/ssl/private/localhost.company.internal.key\"}}}}"
    }
    
    • 1
  2. Lethargos
    2021-04-07T06:07:19+08:002021-04-07T06:07:19+08:00

    我最终能够通过将其转换为 json 来解决它,如下所示:

    NGINX_VHOSTS_JSON: '{{ lookup("template", "rproxy/nginx_vhosts_prometheus_develop.j2") | to_json }}'
    

    我不需要改变任何其他东西。

    • 0

相关问题

  • 错误“缺少 json 扩展名。请检查您的 PHP 配置。” 安装 phpmyadmin 后显示

  • 包 org.json 不存在

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve