Kubernetes Ansible Operators - 修补现有的 Kubernetes 资源

Kubernetes Ansible Operators - Patch an Existing Kubernetes Resource

使用 ansible:是否可以使用 json 或 yaml 片段修补资源?我基本上希望能够完成与 kubectl patch <Resource> <Name> --type='merge' -p='{"spec":{ "test":"hello }}' 相同的事情,达到 append/modify 资源规格。

https://docs.ansible.com/ansible/latest/modules/k8s_module.html 是否可以使用 k8s ansible 模块执行此操作?它说如果资源已经存在并且设置了 "status: present" 它将修补它,但据我所知它没有修补

谢谢

是的,您可以只提供一个补丁,如果资源已经存在,它应该发送一个战略合并补丁(或者如果它是自定义资源,则只发送一个合并补丁)。这是一个创建和修改 configmap 的示例剧本:

---                                                                                                                                                                                                                                           
- hosts: localhost                                                                                                                                                                                                                            
  connection: local                                                                                                                                                                                                                           
  gather_facts: no                                                                                                                                                                                                                            

  vars:                                                                                                                                                                                                                                       
    cm: "{{ lookup('k8s',                                                                                                                                                                                                                     
      api_version='v1',                                                                                                                                                                                                                       
      kind='ConfigMap',                                                                                                                                                                                                                       
      namespace='default',                                                                                                                                                                                                                    
      resource_name='test') }}" 

  tasks:                                                                                                                                                                                                                                      
    - name: Create the ConfigMap                                                                                                                                                                                                              
      k8s:                                                                                                                                                                                                                                    
        definition:                                                                                                                                                                                                                           
          apiVersion: v1                                                                                                                                                                                                                      
          kind: ConfigMap                                                                                                                                                                                                                     
          metadata:
            name: test
            namespace: default
          data:
            hello: world

    - name: We will see the ConfigMap defined above
      debug:
        var: cm

    - name: Add a field to the ConfigMap (this will be a PATCH request)
      k8s:
        definition:
          apiVersion: v1
          kind: ConfigMap
          metadata:
            name: test
            namespace: default
          data:
            added: field

    - name: The same ConfigMap as before, but with an extra field in data
      debug:
        var: cm

    - name: Change a field in the ConfigMap (this will be a PATCH request)
      k8s:
        definition:
          apiVersion: v1
          kind: ConfigMap
          metadata:
            name: test
            namespace: default
          data:
            hello: everyone

    - name: The added field is unchanged, but the hello field has a new value
      debug:
        var: cm

    - name: Delete the added field in the ConfigMap (this will be a PATCH request)
      k8s:
        definition:
          apiVersion: v1
          kind: ConfigMap
          metadata:
            name: test
            namespace: default
          data:
            added: null

    - name: The hello field is unchanged, but the added field is now gone
      debug:
        var: cm