<p>Actually, i have kubernetes cluster set up. I want to generate yaml config file dynamically based on a template using python.</p>

<p>template.yaml</p>

<pre><code>apiVersion: v1
kind: pod
metadata:
  name: $name
spec:
  replicas: $replicas
  template:
    metadata:
      labels:
        run: $name
    spec:
      containers:
      - name: $name
        image: $image
        ports:
        - containerPort: 80
</code></pre>

<p>Placeholders name, replicas and image are the input of my python method.
Any help will be appreciated.</p>

<p>If you want a way to do it using pure python, with no libraries, here's one using multiline strings and format:</p>

<pre><code>def writeConfig(**kwargs):
    template = """
    apiVersion: v1
    kind: pod
    metadata:
      name: {name}
    spec:
      replicas: {replicas}
      template:
        metadata:
          labels:
            run: {name}
        spec:
          containers:
          - name: {name}
            image: {image}
            ports:
            - containerPort: 80"""

    with open('somefile.yaml', 'w') as yfile:
        yfile.write(template.format(**kwargs))

# usage:
writeConfig(name="someName", image="myImg", replicas="many")
</code></pre>
