title: With Ansible, how do I write a private key to a text file without losing the left padding?
body: <p>In other words, I want the content from an Ansible variable to be written to a particular &quot;column&quot; in a config file, and I don't want that content to be wrapped.</p>
  <p><strong>The task:</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>- name: build cloud-init file
    local_action:
      module: ansible.builtin.template
      src: cloud_config.j2
      dest: ./cloud_config.yml
      mode: 0640
    tags: ['containers', 'containers:configuration']
  </code></pre>
  <p><strong>The variable, which lives in a vault file:</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>machineuser_key: |
      -----BEGIN OPENSSH PRIVATE KEY-----
      b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
      ...
  </code></pre>
  <p><strong>The template:</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>write_files:
  {# ssh config to allow our deployment user to fetch the Ansible repo #}
  {# See https://docs.github.com/en/developers/overview/managing-deploy-keys#machine-users #}
  - path: /home/{{ gcp_deploy_user }}/.ssh/{{ gcp_deploy_user}}_github_rsa
    permissions: 600
    owner: {{ gcp_deploy_user }}
    content: |
      {{ machineuser_key }} 
  </code></pre>
  <p><strong>The output of that template task:</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>write_files:
  - path: /home/omegasphinx/.ssh/omegasphinx_github_rsa
    permissions: 600
    owner: omegasphinx
    content: |
      -----BEGIN OPENSSH PRIVATE KEY-----
  b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
  ...
  
  </code></pre>
  <p>I know that <code>blockinfile</code> is a recommended module when one wants to write a config file, and that I should use the <a href="https://stackoverflow.com/a/41615336/785400">&quot;Block Indication Indicator&quot; to do this</a>.  But when I do these things, it still wraps the content of the ssh key.  That <code>blockinfile</code> task looks like this:</p>
  <pre class="lang-yaml prettyprint-override"><code>- name: add ssh private key to cloud-init file
    local_action:
      module: ansible.builtin.blockinfile
      path: cloud_config.yml
      mode: 0440
      insertafter: &quot;# machineuser_ssh_block&quot;
      block: |2
        - path: /home/{{ gcp_deploy_user }}/.ssh/{{ gcp_deploy_user}}_github_rsa
          permissions: 600
          owner: {{ gcp_deploy_user }}
          content: |
            {{ machineuser_key }}
    tags: ['containers', 'containers:configuration']
  </code></pre>
  <p>And the <code>.j2</code> template looks like this:</p>
  <pre class="lang-yaml prettyprint-override"><code>write_files:
  # machineuser_ssh_block
  </code></pre>
  <p>The output then looks like this (exactly the same):</p>
  <pre class="lang-yaml prettyprint-override"><code>write_files:
   - path: /home/omegasphinx/.ssh/omegasphinx_github_rsa
    permissions: 600
    owner: omegasphinx
    content: |
      -----BEGIN OPENSSH PRIVATE KEY-----
  b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
  ...
  </code></pre>
  <p><strong>Other things I've tried:</strong></p>
  <ul>
  <li><p>I disable tabs-to-spaces in my editor and then added tabs before each line of the ssh key in the <code>machineuser_key</code> variable</p>
  </li>
  <li><p>The docs say &quot;You can manually disable the lstrip_blocks behavior by putting a plus sign (+) at the start of a block&quot;; so I added a block and then indented the variable inside the block:</p>
  </li>
  </ul>
  <pre><code>  {%+ if machineuser_key is defined %}
      {{ machineuser_key }} 
    {% endif %}
  </code></pre>
  <p><strong>My understanding of whitespace in Jinja2:</strong></p>
  <p>To give you some context on what I'm expecting to happen.  The &quot;<a href="https://jinja.palletsprojects.com/en/2.11.x/templates/#whitespace-control" rel="nofollow noreferrer">Whitespace Control</a>&quot; section of the Jinja docs states that &quot;a single trailing newline is stripped if present&quot; but &quot;other whitespace (spaces, tabs, newlines etc.) is returned unchanged.&quot;  In my case, I have &quot;other whitespace&quot;: spaces to the left of each line of the variable <code>machineuser_key</code>.  But obviously I'm missing something.</p>