如何将 k8s 模块中的变量作为参数传递给另一个 yml

How can I pass variable inside k8s module to another yml as parameter

我想将变量从 ansible k8s 模块传递给另一个 yml

- name: Create a Secret
  hosts: 127.0.0.1
  connection: local
  become: yes
  tasks:
    - name: Create minio-pvc
      k8s:
        state: present
        kind: PersistentVolumeClaim
        namespace: "{{ item.namespace }}"
        src: /tasks/minio-pvc.yml
      with_items:
        - { namespace: demo1}
        - { namespace: demo2}
        - { namespace: test}

这是我要在里面发送参数的yml文件。所以我可以创造 PersistentVolumeClaim 动态。

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    pv.kubernetes.io/bind-completed: "yes"
    pv.kubernetes.io/bound-by-controller: "yes"
  creationTimestamp: null
  labels:
    app: minio-storage-claim
  name: minio-pv-claim
  selfLink: /api/v1/namespaces/demo/persistentvolumeclaims/minio-pv-claim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  volumeName: $param(which must be in here)

像这样;

        namespace: "{{ item.namespace }}"
        src: /tasks/minio-pvc.yml
        param: "{{ item.param }}"
      with_items:
        - { namespace: demo1 , param: pv1}
        - { namespace: demo2 , param: pv2}
        - { namespace: test , param: pv3}

您可以在下面使用。

- name: Create a Secret
  hosts: 127.0.0.1
  connection: local
  become: yes
  tasks:
    - name: Create minio-pvc
      k8s:
        state: present
        kind: PersistentVolumeClaim
        namespace: "{{ item.namespace }}"
        definition: "{{ lookup('template', '/tasks/minio-pvc.yml') | regex_replace('\$param', item.param) }}"
      with_items:
        - { namespace: demo1, param: pv1 }
        - { namespace: demo2, param: pv2 }
        - { namespace: test, param: pv3 }