# Not able to use the patch parameter of the k8s module

**URL:** <https://forum.ansible.com/t/not-able-to-use-the-patch-parameter-of-the-k8s-module/5852>\
**Category:** Get Help\
**Tags:** kubernetes\
**Created:** [May 17, 2024, 7:04am UTC](https://forum.ansible.com/t/not-able-to-use-the-patch-parameter-of-the-k8s-module/5852 "2024-05-17T07:04:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kushagra](https://avatars.discourse-cdn.com/v4/letter/k/85f322/32.png) [@kushagra](https://forum.ansible.com/u/kushagra)\
**Post date:** [May 17, 2024, 7:04am UTC](https://forum.ansible.com/t/not-able-to-use-the-patch-parameter-of-the-k8s-module/5852/1 "2024-05-17T07:04:06Z")

</div>

Hi team,  
We are using the K8s module to patch our config map file.  
The config map file which we are patching has 2 data.

The data which we pass its replacing the actual data.  
We want to append the data in the current data field  
Our code:

```auto
- name: Configure prometheus-server-conf
  kubernetes.core.k8s:
    state: patched
    kind: ConfigMap
    name: prometheus-server-conf
    namespace: monitoring
    definition:
      data:
        prometheus.yml: |-
          scrape_configs:
            - job_name: "ceph"
              static_configs:
                - targets: [' 10.0.1.34:9283', ' 10.0.1.35:9283', ' 10.0.1.36:9283']

```

Example:  
we have a following file:

```auto
apiVersion: v1
data:
prometheus.yml: |-
    scrape_configs:
      - job_name: 'kubernetes-apiservers'
        kubernetes_sd_configs:
        - role: endpoints
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        relabel_configs:
        - source_labels: [__meta_kubernetes_namespace,__ meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
          action: keep
          regex: default;kubernetes;https

```

We want the following:

```auto
apiVersion: v1
data:
prometheus.yml: |-
    scrape_configs:
      - job_name: 'kubernetes-apiservers'
        kubernetes_sd_configs:
        - role: endpoints
        scheme: https
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        relabel_configs:
        - source_labels: [__meta_kubernetes_namespace,__ meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
          action: keep
          regex: default;kubernetes;https
      - job_name: "ceph"
        static_configs:
          - targets: [' 10.0.1.34:9283', ' 10.0.1.35:9283', ' 10.0.1.36:9283']

```

The following is happening:

```auto
apiVersion: v1
data:
prometheus.yml: |-
    scrape_configs:
      - job_name: "ceph"
        static_configs:
          - targets: [' 10.0.1.34:9283', ' 10.0.1.35:9283', ' 10.0.1.36:9283']

```

Kindly help.

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [May 19, 2024, 9:34am UTC](https://forum.ansible.com/t/not-able-to-use-the-patch-parameter-of-the-k8s-module/5852/2 "2024-05-19T09:34:13Z")

</div>

Hi,

Perhaps try to explicitly specify you’d like to perform a [“strategic-merge” patch](https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_module.html#parameter-merge_type), although description states it should be the default.

Edit: I highly doubt it, but I’m also wondering if stripping last newline with a literal block scalar (`|-`) as you do in your patch definition could invalidate the key you’re trying to patch in some way. Just a thought.

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [May 19, 2024, 1:23pm UTC](https://forum.ansible.com/t/not-able-to-use-the-patch-parameter-of-the-k8s-module/5852/3 "2024-05-19T13:23:14Z")

</div>

I didn’t make any investigation or test on my side, but since `data` in ConfigMap is `string` type, in the first place, your `prometheus.yml` is just one big string data instead of well-structured data.

I mean,

```yaml
prometheus.yml: |-
    scrape_configs:
      - job_name: 'kubernetes-apiservers'
        kubernetes_sd_configs:
        ...

```

almost equals to:

```yaml
prometheus.yml: "scrape_configs:\n - job_name: 'kubernetes-apiservers'\n kubernetes_sd_configs:\n ..."

```

So AFAIK I don’t think a merge strategy will work 🤔

If we want to patch an existing ConfigMap that has any keys with the data in YAML format, it would be difficult unless we get the existing ConfigMap, convert it to dict with `from_yaml`, patch it, and then update the ConfigMap with `to_nice_yaml`. If there is a better way, I’d like to know too.

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [May 19, 2024, 1:23pm UTC](https://forum.ansible.com/t/not-able-to-use-the-patch-parameter-of-the-k8s-module/5852/4 "2024-05-19T13:23:52Z")

</div>

There is a similar KEP for kustomize: [kustomize/proposals/22-03-value-in-the-structured-data.md at master · kubernetes-sigs/kustomize · GitHub](https://github.com/kubernetes-sigs/kustomize/blob/master/proposals/22-03-value-in-the-structured-data.md)
