【问题标题】:Ansible K8s Module - Apply Multiple Yaml Files at OnceAnsible K8s 模块 - 一次应用多个 Yaml 文件
【发布时间】:2021-09-02 01:07:45
【问题描述】:

编写一个 Ansible 剧本,我们从 GIT 存储库中提取项目,然后将所有提取的 yaml 应用到 Kubernetes 集群。

我只看到一个将单个 yaml 文件应用到 Kubernetes 集群的示例,而不是同时应用多个。例如:

- name: Apply metrics-server manifest to the cluster.
  community.kubernetes.k8s:
    state: present
    src: ~/metrics-server.yaml

有没有办法应用多个 yaml 文件?比如:

- name: Apply Services to the cluster.
  community.kubernetes.k8s:
    state: present
    src: ~/svc-*.yaml

或者:

- name: Apply Ingresses to the cluster.
  community.kubernetes.k8s:
    state: present
    dir: ~/ing/

是否还有另一个我应该研究的 Ansible K8s 模块?

或者我们应该直接在 Ansible 任务中运行 kubectl 命令。例如:

- name: Apply Ingresses to the cluster.
  command: kubectl apply -f ~/ing/*.yaml

使用 Ansible 实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: kubernetes ansible


    【解决方案1】:

    您可以将 k8s Ansible 模块与“with_fileglob”递归模式一起使用。下面的代码应该可以满足您的要求。

    - name: Apply K8s resources
            k8s:
              definition: "{{ lookup('template', '{{ item }}') | from_yaml }}"
            with_fileglob:
              - "~/ing/*.yaml"
    

    【讨论】:

      猜你喜欢
      • 2020-01-23
      • 2021-11-04
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2016-10-16
      • 2020-06-13
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多