我正在尝试将单行 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 中,它也可以正常工作。
但是查找模板似乎没有提供预期的字符串。
有什么想法可以解决这个问题吗?