Closed jgagnon44 closed 1 year ago
@jgagnon44 A ConfigMap is just key/value data. It's not structured. The path /data/config.conf/metricsBindAddress
doesn't exist. If you want to modify that yaml string you will have to read that string in as yaml, change it how you want, and then pass the entire modified yaml string to your json patch using the path /data/config.conf
.
OK. I was suspecting something along these lines, but wasn't sure. I'm very new to Ansible. How could I pull the config.conf
"data", change it and then replace the existing with the modified?
I would probably do something like:
- hosts: localhost
gather_facts: false
tasks:
- kubernetes.core.k8s:
kind: Namespace
name: testing
- kubernetes.core.k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: test-map
namespace: testing
data:
config.conf: |
apiVersion: v1
kind: Pod
metadata:
name: my-pod
- kubernetes.core.k8s_info:
kind: ConfigMap
name: test-map
namespace: testing
register: result
- set_fact:
new_data: "{{ result.resources[0].data['config.conf'] | from_yaml | combine(patch, recursive=true) }}"
vars:
patch:
kind: Secret
metadata:
namespace: some-ns
- kubernetes.core.k8s_json_patch:
kind: ConfigMap
name: test-map
namespace: testing
patch:
- op: replace
path: /data/config.conf
value: "{{ new_data | to_nice_yaml(indent=2) }}"
Do note the limitation with string width: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#formatting-data-yaml-and-json.
Took me a bit, but got it work. Thanks.
SUMMARY
Getting "422 - Unprocessable Entity" when attempting to modify ConfigMap.
I am wondering if the issue is related to the unique representation of the ConfigMap data. I am not sure if I using the correct path to the element of interest.
For example, the ConfigMap I'm attempting to modify, appears as follows when rendered as JSON:
ISSUE TYPE
COMPONENT NAME
kubernetes.core.k8s_json_patch
ANSIBLE VERSION
COLLECTION VERSION
CONFIGURATION
OS / ENVIRONMENT
"Launch" computer: Windows 11 21H2, OS build: 22000.1455
Ansible "node": Docker image: cytopia/ansible:latest-tools Linux ec1303bf69ab 5.10.104-linuxkit #1 SMP Thu Mar 17 17:08:06 UTC 2022 x86_64 Linux
Kubernetes node (control plane): Linux gagnon-m1 5.4.0-126-generic #142-Ubuntu SMP Fri Aug 26 12:12:57 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
STEPS TO REPRODUCE
This is being run via a Docker image that contains an Ansible installation. I am running the container and then inside the container, running the playbook. The commands below summarize the process.
In the container:
EXPECTED RESULTS
The
metricsBindAddress
in theconfig.conf
entry in theConfigMap
data to be changed to0.0.0.0
.ACTUAL RESULTS