3

lets take this example of a config map

apiVersion: v1
kind: ConfigMap
data:
  abc.yml: |-
<yml here>

Getting an error like failed to parse yaml to Json.

1
  • remove '-' from abc.yml: |- and try Commented Mar 7, 2019 at 8:37

3 Answers 3

4

Yes you can do that, but you should care about the syntax. You can also follow techniques for yaml from here.

If you use kubectl create configmap myconfig --from-file=abc.yml, then it is ok.

But if you write the whole yaml file for your configmap in myconfig.yaml and then run kubectl create -f myconfig.yaml, then you should care about syntax.

Say your abc.yml file is as followings:

a:
  b: b1
  c: c1
d: d1

Then write your myconfig.yaml file:

apiVersion: v1
kind: ConfigMap
data:
  abc.yml: |
    a:
      b: b1
      c: c1
    d: d1

Now just run kubectl create -f myconfig.yaml. That's it.

Happy Kubernetes!!!.

Sign up to request clarification or add additional context in comments.

Comments

1

Create ConfigMap from file.

kubectl create configmap myconfig --from-file=youfile.yml.

You can check more examples on kubernetes docs

Comments

1

These could be the problems 1. most likely the issue could with the indentation. 2. remove '-' from abc.yml: |- and check

I followed the below steps and was able to load yaml file into configmap. it worked fine.

master $ cat c.yaml
apiVersion: v1
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
  name: example-redis-config
master $ kubectl create configmap  testcfg --from-file=./c.yaml
master $ kubectl get cm testcfg -oyaml
apiVersion: v1
data:
  c.yaml: |
    apiVersion: v1
    data:
      redis-config: |
        maxmemory 2mb
        maxmemory-policy allkeys-lru
    kind: ConfigMap
    metadata:
      name: example-redis-config
kind: ConfigMap
metadata:
  creationTimestamp: 2019-03-07T08:35:18Z
  name: testcfg
  namespace: default
  resourceVersion: "7520"
  selfLink: /api/v1/namespaces/default/configmaps/testcfg
  uid: f033536d-40b3-11e9-a67d-0242ac11005b

2 Comments

apiVersion: v1 kind: ConfigMap data: abc.yml: | namespace: "a_namespace" :development: <<: *defaults deployment >> default How to make the config map read the default variables ?
please state clearly what exactly you are trying to achieve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.