Closed MonkadelicD closed 7 months ago
@MonkadelicD This appears to be intended Ansible behavior. You can see the same thing with a simple playbook:
- hosts: localhost
gather_facts: false
vars:
myvar: bar
tasks:
- debug:
msg: |-
{
"foo": "bar"
}
- debug:
msg: |-
{
"foo": "{{ myvar }}"
}
I believe https://github.com/ansible/ansible/issues/34595 explains why this is happening. One workaround would be to store your configmap data in a variable and convert it to json, for example:
- kubernetes.core.k8s:
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: mymap
namespace: default
data:
appsettings.json: "{{ data | to_nice_json(indent=2) }}"
vars:
myvar: foobar
data: |-
{
"somesetting": "{{ myvar }}"
}
@gravesm Thanks! Storing the data in a variable worked great.
SUMMARY
When applying a particular ConfigMap where the data is JSON and includes Jinja2 variables I get the following error: "json: cannot unmarshal object into Go struct field ConfigMap.data of type string, reason:BadRequest, code:400.
The JSON validates when fed to json_pp. Executing the playbook with ANSIBLE_KEEP_REMOTE_FILES=1 and feeding the args file to json_pp also shows valid. In the args file all Jinja2 variables are substituted with the expected values.
ISSUE TYPE
COMPONENT NAME
kubernetes.core.k8s
ANSIBLE VERSION
COLLECTION VERSION
CONFIGURATION
OS / ENVIRONMENT
RHEL 9.2 RKE2 1.25.12
STEPS TO REPRODUCE
I run the following playbook with: ansible-playbook -vvv -i inventory setup_app1.yaml The failed message: "msg": "Failed to create object: b'{\"kind\":\"Status\",\"apiVersion\":\"v1\",\"metadata\":{},\"status\":\"Failure\",\"message\":\"ConfigMap in version \\\"v1\\\" cannot be handled as a ConfigMap: json: cannot unmarshal object into Go struct field ConfigMap.data of type string\",\"reason\":\"BadRequest\",\"code\":400}\n'"
If I add two back slashes '\' before the first line in the json file "ConnectionStrings": I get no error but that line is missing in the mounted file in the container after deployment. I can use kubectl to create a ConfigMap using the same JSON. The only change I make is substituting Jinja2 variables with their corresponding values.
When running the playbook with ANSIBLE_KEEP_REMOTE_FILES=1, exploding Ansiballz_k8s.py, then inspecting the args file, I can verify the data is valid JSON. The true/false values in the yaml JSON are not quoted. I suspected they were being interpreted as boolean so I added single quotes in the yaml JSON. The output args file on the target host has double quotes around the true/false values but I still get the same error.
No issues with variable values. This is the playbook with relevant tasks. Some names and details have been changed to protect the innocent
EXPECTED RESULTS
I expect a status of changed when the task is run.
ACTUAL RESULTS
I get a failed result for the task