<p>Here's a simplified version of a kubernetes job YAML config I use commonly:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: myjob
spec:
  template:
    spec:
      containers:
      - name: mycontainer
        image: me/mycontainer:latest
        command: [&quot;bash&quot;, &quot;-c&quot;]
        args:
          - python -u myscript.py
              --param1 abc
              --param2 xyz
</code></pre>
<p>The above works great, and is easy to maintain and read. But now one of my parameters needs some minified YAML:</p>
<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: myjob
spec:
  template:
    spec:
      containers:
      - name: mycontainer
        image: me/mycontainer:latest
        command: [&quot;bash&quot;, &quot;-c&quot;]
        args:
          - python -u myscript.py
              --param_minified_yaml &quot;{key: value}&quot;
</code></pre>
<p>This bit of embedded minified yaml is being parsed by <code>kubectl</code> and causing: <code>error: error parsing STDIN: error converting YAML to JSON: yaml: line 26: mapping values are not allowed in this context</code></p>
<p>How can the embedded yaml in <code>args:</code> be escaped such that it's passed as a pure text argument?</p>

David Parks
<p>If the minified yaml (or the args string in general) does not include single quotes, you can wrap the whole command line in them:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: myjob
spec:
  template:
    spec:
      containers:
      - name: mycontainer
        image: me/mycontainer:latest
        command: [&quot;bash&quot;, &quot;-c&quot;]
        args:
          - 'python -u myscript.py
              --param_minified_yaml &quot;{key: value}&quot;'
</code></pre>
<p>If the arg string contains includes single quotes, the args string can be passed as a <a href="https://yaml-multiline.info/" rel="nofollow noreferrer">YAML multiline string</a>:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: myjob
spec:
  template:
    spec:
      containers:
      - name: mycontainer
        image: me/mycontainer:latest
        command: [&quot;bash&quot;, &quot;-c&quot;]
        args:
          - &gt;-
            python -u myscript.py
            --param_minified_yaml &quot;{key: 'value'}&quot;
</code></pre>
