Yaml file in Python not loading with correct values

11/20/2019

I am loading a yaml file (present within a container in a kubernetes pod) in python using yaml library which has a section "commands".

Below is the commands section in yaml file:

command:
- sh
- "-c"
- |
  addgroup 2000
  adduser -D -G 2000 2000
  chown -R 2000:2000 /sh_pvc
  chmod 760 /sh_pvc 

Below is the code that I used to read the yaml file (I am bashing into the container and reading the file):

yaml_content = yaml.safe_load(stream(KubeCtl.v1.connect_get_namespaced_pod_exec, name = pod_name, namespace = pod_namespace, command=['/bin/sh', '-c', 'cat test_file.yaml'], stderr=True, stdin=True, stdout=True, tty=True))

NOTE: KubeCtl.v1 is a kubernetes api server object. 'test_file.yaml' is the yaml file being read

Response yaml_content is a dictionary in which the commands section appears as below:

  - command:
    - sh
    - -c
    - "addgroup 2000\nadduser -D -G 2000 2000 \nchown -R 2000:2000 /sh_pvc\nchmod\
      \ 760 /sh_pvc\n"

I would like the comments section to appear as it is in the original file. Any suggestions for this ?

I looked at many answers online and most of them are regarding reading the yaml file and not specifically aligned with my problem

-- user_11077035
kubernetes
python
yaml

1 Answer

11/20/2019

It may be a question of how yaml_content is output. You could try this:

print(yaml.dump(yaml_content, default_flow_style=False))

The block style indicator | wouldn't be printed out.

-- gears
Source: StackOverflow