首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何配置需要循环中变量的字典?

如何配置需要循环中变量的字典?
EN

Stack Overflow用户
提问于 2021-10-25 01:40:19
回答 1查看 49关注 0票数 0

我有一个docker_container,我想为多个用户部署它,并以用户的名字命名traefik路由。但我搞不懂我怎么能做到这一点。

以下是我所拥有的:

代码语言:javascript
复制
- name: Run syncthing
  docker_container:
    name: "{{ item.name }}-syncthing"
    image: "lscr.io/linuxserver/syncthing"
    state: started
    restart_policy: "always"
    env:
      PUID: "1000"
      PGID: "1000"
    volumes:
      - "{{ item.data_dir }}:/data"
... other volumes
    labels:
      traefik.enable: true
      "traefik.http.routers.{{ item.name }}-syncthing.entrypoints": websecure
      "traefik.http.routers.{{ item.name }}-syncthing.rule": Host(`{{ item.name }}.{{ fqdn_real }}`)
      "traefik.http.routers.{{ item.name }}-syncthing.tls": true
      "traefik.http.routers.{{ item.name }}-syncthing.tls.certresolver": le 
      "traefik.http.routers.{{ item.name }}-syncthing.service": "{{ item.name }}-syncthing"
      "traefik.http.routers.{{ item.name }}-syncthing.middlewares": "{{ item.name }}-basicauth"
      "traefik.http.services.{{ item.name }}-syncthing.loadbalancer.server.port": 8080
      "traefik.http.middlewares.{{ item.name }}-syncthing-basicauth.basicauth.users": "{{ item.auth }}"
  with_items: "{{ syncthing_containers_info }}"

像这样的syncthing_config_info

代码语言:javascript
复制
syncthing_containers_info:
  - { name: "c1", data_dir: "/mnt/data/c1/data", auth: "..." }
  - { name: "c2", data_dir: "/mnt/data/c2/data", auth: "..." }
  - { name: "c3", data_dir: "/mnt/data/c3/data", auth: "..." }

这个片段不起作用,因为ansible不喜欢语法,所以我用this尝试了一个with_nested,但是在尝试set_fact时,我遇到了一个类似的问题,因为标签集依赖于syncthing_containers_info。有更好的方法让我这么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-25 03:34:06

听起来您需要labels:作为一个实际的dict,因为yaml键不受jinja2内插的限制。

代码语言:javascript
复制
    labels: >-
      {%- set key_prefix = "traefik.http.routers." ~ item.name ~"-syncthing" -%}
      {{ {
      "traefik.enable": True,
      key_prefix ~ ".entrypoints": "websecure",
      key_prefix ~ ".rule": "Host(`" ~ item.name ~"."~ fqdn_real ~"`)",
      key_prefix ~ ".tls": True,
      key_prefix ~ ".tls.certresolver": "le",
      key_prefix ~ ".service": item.name ~ "-syncthing",
      key_prefix ~ ".middlewares": item.name ~ "-basicauth",
      "traefik.http.services." ~ item.name ~ "-syncthing.loadbalancer.server.port": 8080,
      "traefik.http.middlewares." ~ item.name ~"-syncthing-basicauth.basicauth.users": item.auth,
      } }}

(请注意,我没有测试这个,只是从你的问题上看了一下,但这是总的想法)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69702053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档