<p>With ansible: is it possible to patch resources with json or yaml snippets? I basically want to be able to accomplish the same thing as <code>kubectl patch &lt;Resource&gt; &lt;Name&gt; --type='merge' -p='{"spec":{ "test":"hello }}'</code>, to append/modify resource specs.</p>

<p><a href="https://docs.ansible.com/ansible/latest/modules/k8s_module.html" rel="noreferrer">https://docs.ansible.com/ansible/latest/modules/k8s_module.html</a>
Is it possible to do this with the k8s ansible module? It says that if a resource already exists and "status: present" is set that it will patch it, however it isn't patching as far as I can tell</p>

<p>Thanks</p>

<p>Yes, you can provide just a patch and if the resource already exists it should send a strategic-merge-patch (or just a merge-patch if it's a custom resource). Here's an example playbook that creates and modifies a configmap:</p>

<pre><code>---                                                                                                                                                                                                                                           
- 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
</code></pre>
