在 ansible 中连接变量以将它们作为部署的 helm 标志传递

concatenate variables in ansible to pass them as helm flags for deployments

目标是将变量从 ansible 传递到 helm --set key=value。 ansible 的以下输出结构可用

apps:
- name: proxy
  properties:
  - key: proxy.externalIP
    value: 192.168.178.1
  - key: proxy.service.Type
    value: LoadBalancer
- name: proxylived
  properties:
  - key: proxylived.externalIP
    value: 192.168.178.1
  - key: proxylived.port
    value: 31443

ansible角色应该执行以下命令

$ helm install proxy . --set proxy.externalIP=192.168.178.1 --set proxy.service.Type=LoadBalancer 
$ helm install proxylived . --set proxy.externalIP=192.168.178.1 --set proxylived.port=31443

我的问题是,我不知道如何遍历对象。我尝试了以下方法:

main.yml

---
- name: deploy applications
  include_tasks: apps.yml
  loop: "{{ apps }}"
  loop_control:
    loop_var: app

apps.yml

---
- name: deploy application {{ app.name }}
  ansible.builtin.command:
    argv:
    - /usr/bin/helm
    - install
    - {{ app.name }}
    - {{ how to pass here a list of the key value attributes? }}

简而言之,彻底测试:

apps.yml

---
- name: create the list of values to set
  set_fact:
    kvs: "{{ kvs | default([]) + ['--set', item.key ~ '=' ~ item.value] }}"
  loop: "{{ app.properties }}"

- name: deploy application {{ app.name }}
  vars:
    base_cmd:
      - "/usr/bin/helm"
      - "install"
      - "{{ app.name }}"
      - "."
  ansible.builtin.command:
    argv: "{{ base_cmd + kvs }}"