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

时间:2019-07-18 08:08:27

标签: parameters ansible yaml persistent-volume-claims

我想将变量从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}

这是我要在其中发送param的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}

1 个答案:

答案 0 :(得分:0)

您可以在下面使用。

- 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 }