<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Ansible-Vault · Network Doodles</title><link>https://networkdoodles.com/tags/ansible-vault/</link><description>Network automation, from first playbook to agentic operations. Ansible, NetBox, MCP, Python and the occasional doodle.</description><language>en-us</language><managingEditor>David Henderson</managingEditor><lastBuildDate>Mon, 16 Nov 2026 09:00:00 -0500</lastBuildDate><atom:link href="https://networkdoodles.com/tags/ansible-vault/index.xml" rel="self" type="application/rss+xml"/><item><title>Ansible Vault for Network Device Passwords (Ansible 101 for Networking, Lesson 7)</title><link>https://networkdoodles.com/lessons/ansible-101/secrets/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/ansible-101/secrets/</guid><category>ansible</category><category>network-automation</category><category>automation</category><category>security</category><category>ansible-vault</category><category>howto</category><description>Get the switch password out of plain text with ansible-vault: what to encrypt, the vault_ variable pattern, keeping the vault key out of Git, and its limits.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>Since <a href="/lessons/ansible-101/hello-ansible/">lesson 1</a> the lab&rsquo;s switch password has been sitting in a YAML file in
plain text, with a comment promising that lesson 7 would fix it. This is lesson 7.</p>
<p>I&rsquo;ve written before about <a href="/posts/donthardcodepasswords/">why you shouldn&rsquo;t hardcode passwords</a>. This post is
the practical Ansible version: <strong>ansible-vault</strong>, the pattern that keeps vaulted files readable, and the one
file that must never reach Git.</p>
<hr>
<h2 id="the-problem-precisely">The problem, precisely</h2>
<p>Right now <code>inventory/group_vars/fabric.yml</code> contains:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">ansible_user</span><span class="p">:</span><span class="w"> </span><span class="l">admin</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">ansible_password</span><span class="p">:</span><span class="w"> </span><span class="l">capeos-lab</span><span class="w">
</span></span></span></code></pre></div><p>Commit that and the password is in Git forever. Not until you delete the line: forever. It&rsquo;s in the history,
in every clone, in every fork, in the CI cache. Rotating the password is the only real fix, and on a network
that&rsquo;s every device.</p>
<p>We want three things:</p>
<ol>
<li>The password is <strong>in Git</strong>, so the repo is still the complete description of the network.</li>
<li>It&rsquo;s <strong>unreadable</strong> to anyone without the key.</li>
<li>The <strong>key is not in Git</strong>.</li>
</ol>
<hr>
<h2 id="how-ansible-vault-works">How ansible-vault works</h2>
<p>ansible-vault encrypts files (or single values) with AES-256 using a password you choose, the <strong>vault
password</strong>. Ansible decrypts them in memory when it runs; they never touch disk in clear text. An encrypted
file looks like this, and it&rsquo;s safe to commit:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">$ANSIBLE_VAULT;1.1;AES256
</span></span><span class="line"><span class="cl">32653236363634656263313066396265613937616133336430353330393338626165333766383163
</span></span><span class="line"><span class="cl">3836373465373031333334333465393337376132646438390a616132313762633532636561363561
</span></span><span class="line"><span class="cl">...
</span></span></code></pre></div><p>The setup has a handful of moving parts, and each has one job:</p>
<table>
  <thead>
      <tr>
          <th>Piece</th>
          <th>What it&rsquo;s for</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>.vault-pass</code></td>
          <td>A file holding the vault password: the key. It stays on your machine, readable only by you (<code>chmod 600</code>), and never in Git.</td>
      </tr>
      <tr>
          <td><code>.gitignore</code> entry for <code>.vault-pass</code></td>
          <td>Makes sure the key can&rsquo;t be committed by accident. Add it <em>before</em> the file exists.</td>
      </tr>
      <tr>
          <td><code>vault_password_file = .vault-pass</code> in <code>ansible.cfg</code></td>
          <td>Tells Ansible where the key is, so every run decrypts without asking.</td>
      </tr>
      <tr>
          <td><code>ansible-vault create</code></td>
          <td>Makes a new encrypted file and opens it in your editor.</td>
      </tr>
      <tr>
          <td><code>ansible-vault view</code> / <code>edit</code></td>
          <td>Reads or changes an encrypted file without ever writing the clear text to disk.</td>
      </tr>
      <tr>
          <td><code>ansible-vault rekey</code></td>
          <td>Re-encrypts a file with a new vault password, for when the key itself has to change.</td>
      </tr>
      <tr>
          <td><code>ansible-vault encrypt_string</code></td>
          <td>Encrypts one value, for pasting into an otherwise plain YAML file.</td>
      </tr>
  </tbody>
</table>
<p>That <code>.gitignore</code> line is the most important line in this post. Everything else is recoverable; a committed
key isn&rsquo;t.</p>
<h3 id="a-whole-file-or-one-value">A whole file, or one value</h3>
<p>There are two ways to encrypt, and they suit different secrets.</p>
<p><strong>A whole file</strong> (<code>ansible-vault create</code>) is the simple one. <code>view</code>, <code>edit</code> and <code>rekey</code> all work on it, and
you can keep several secrets in it. The cost is that everything in the file is hidden, from reviewers as much
as from attackers.</p>
<p><strong>One value</strong> (<code>ansible-vault encrypt_string</code>) encrypts a single string and prints a block you paste into a
normal file:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">snmp_community</span><span class="p">:</span><span class="w"> </span>!<span class="l">vault |</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span><span class="l">$ANSIBLE_VAULT;1.1;AES256</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span><span class="m">66383430653433353434623339646165323662353563666433656536636265656363663036313434</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">          </span><span class="l">...</span><span class="w">
</span></span></span></code></pre></div><p>The rest of the file stays readable, and the variable name stays visible, so a diff shows <em>that</em> the
community changed even though nobody can see what to. The cost: each value is encrypted on its own, so you
can&rsquo;t <code>view</code> or <code>edit</code> it in place; changing it means encrypting a new value and pasting it over the old one.</p>
<p>Rule of thumb: inline for the odd value that lives naturally in a plain settings file, a vault file once you
have more than a couple of secrets, or any you&rsquo;ll change often.</p>
<hr>
<h2 id="the-vault_-pattern">The <code>vault_</code> pattern</h2>
<p>Here&rsquo;s the mistake almost everyone makes first: encrypting <code>group_vars/fabric.yml</code> whole. It works, and now
nobody can see <em>any</em> of the fabric&rsquo;s settings without the key, not the connection type, not the NTP servers,
nothing. Code review on that file becomes &ldquo;trust me&rdquo;.</p>
<p>The pattern that fixes it is small:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="c"># inventory/group_vars/all/vault.yml   (encrypted)</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">vault_ansible_password</span><span class="p">:</span><span class="w"> </span><span class="l">capeos-lab</span><span class="w">
</span></span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="c"># inventory/group_vars/fabric.yml     (plain text)</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">ansible_user</span><span class="p">:</span><span class="w"> </span><span class="l">admin</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w"></span><span class="nt">ansible_password</span><span class="p">:</span><span class="w"> </span><span class="s2">&#34;{{ vault_ansible_password }}&#34;</span><span class="w">   </span><span class="c"># from inventory/group_vars/all/vault.yml</span><span class="w">
</span></span></span></code></pre></div><ul>
<li>The <strong>encrypted file holds only secrets</strong>, each named with a <code>vault_</code> prefix.</li>
<li>The <strong>plain file references them</strong>, so anyone can read the fabric&rsquo;s settings, search for where the
password is used, and review changes, without the key.</li>
</ul>
<p><code>group_vars/all/</code> is a folder rather than a file: Ansible loads every file inside a group&rsquo;s folder, which lets
you keep <code>vault.yml</code> separate from the plain settings for the same group.</p>
<p>The playbooks don&rsquo;t change at all. Ansible finds the key through <code>ansible.cfg</code>, decrypts in memory, and the
switches never know anything changed.</p>

<figure class="netsim" data-netsim tabindex="0" aria-label="Where the vault password lives: the vault file in Git is ciphertext, the key file stays on your machine and is gitignored, at run time Ansible decrypts in memory and logs in to the switch, a clone of the repo gets only ciphertext, and CI writes the key from a masked variable. Use the buttons, or the left and right arrow keys, to step through it." data-autoplay="false">
  <svg class="ns-svg" role="img" aria-label="Where the vault password lives: the vault file in Git is ciphertext, the key file stays on your machine and is gitignored, at run time Ansible decrypts in memory and logs in to the switch, a clone of the repo gets only ciphertext, and CI writes the key from a masked variable"></svg>
  <div class="ns-ctl">
    <button class="icon-btn" type="button" data-ns="restart" aria-label="Back to the first step" title="Start again"><svg class="ico" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M6 5v14M19 5l-9 7 9 7z"/></svg>
</button>
    <button class="icon-btn" type="button" data-ns="prev" aria-label="Previous step" title="Previous step"><svg class="ico" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 12H5M11 6l-6 6 6 6"/></svg>
</button>
    <button class="btn sm primary ns-play" type="button" data-ns="play" aria-pressed="false"><span data-ns-play>Play</span></button>
    <button class="icon-btn" type="button" data-ns="next" aria-label="Next step" title="Next step"><svg class="ico" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
</button>
    <button class="icon-btn" type="button" data-ns="again" aria-label="Play this step again" title="Play this step again"><svg class="ico" viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 12a8 8 0 1 0 2.5-5.8M4 4v4h4"/></svg>
</button>
    <span class="ns-count mono" data-ns-count></span>
    <button class="btn sm ns-speed" type="button" data-ns="speed" aria-pressed="false">Slow: off</button>
  </div>
  <div class="ns-dots" data-ns-dots role="group" aria-label="Jump to a step"></div>
  <figcaption class="ns-caption" data-ns-caption aria-live="polite"></figcaption>
  <div class="ns-panels">
    <div class="ns-codebox" data-ns-code hidden></div>
    <div class="ns-frame" data-ns-frame hidden></div>
    <div class="ns-tablebox" data-ns-table hidden></div>
  </div>
  <script type="application/json">{"h":340,"links":[],"narrow":{"at":{"ans":[270,230],"ci":[80,380],"clone":[80,230],"key":[270,70],"leaf1":[270,380],"repo":[80,70]},"h":440,"w":360},"nodes":{"ans":{"kind":"gear","label":"ansible-playbook","sub":"on your machine","x":360,"y":250},"ci":{"hidden":true,"kind":"ci","label":"CI job","sub":"VAULT_PASSWORD, masked","x":640,"y":80},"clone":{"hidden":true,"kind":"laptop","label":"A clone","sub":"no .vault-pass","x":110,"y":250},"key":{"kind":"file","label":".vault-pass","sub":"your machine only","x":360,"y":80},"leaf1":{"kind":"switch","label":"leaf1","sub":"login: admin","x":640,"y":250},"repo":{"kind":"repo","label":"Git repo","sub":"vault.yml: ciphertext","x":110,"y":80}},"steps":[{"code":{"lines":["$ANSIBLE_VAULT;1.1;AES256","32653236363634656263313066396265613937616133336430353330393338626165333766383163","3836373465373031333334333465393337376132646438390a616132313762633532636561363561","..."],"title":"inventory/group_vars/all/vault.yml"},"hot":["repo"],"text":"`group_vars/all/vault.yml` is AES-256 ciphertext. Safe to commit: without the key, it's noise.","title":"In Git, the vault file is ciphertext."},{"code":{"hl":[1],"lines":["ansible_user: admin","ansible_password: \"{{ vault_ansible_password }}\""],"title":"inventory/group_vars/fabric.yml"},"hot":["repo"],"text":"`fabric.yml` stays readable. Reviewers can see where the password is used without seeing it.","title":"The plain file only names the secret."},{"code":{"hl":[1],"lines":["# .gitignore",".vault-pass","","# ansible.cfg","vault_password_file = .vault-pass"],"title":"two lines that keep it out"},"hot":["key"],"packets":[{"c":"red","end":"drop","label":"git add","path":["key","repo"]}],"say":{"key":"gitignored,\nchmod 600"},"sayAt":{"key":{"narrow":"below","wide":"right"}},"text":"`.vault-pass` holds the vault password. `.gitignore` lists it, so it can't be committed by accident.","title":"The key stays on your machine."},{"hot":["ans"],"packets":[{"label":"ciphertext","path":["repo","ans"]},{"label":"key","path":["key","ans"]}],"say":{"ans":"decrypted in memory,\nnever on disk"},"sayAt":{"ans":{"narrow":"below","wide":"right"}},"text":"Ansible reads the ciphertext and the key, and decrypts the value in memory. The clear text never touches disk.","title":"At run time, decrypted in memory."},{"code":null,"good":["leaf1"],"packets":[{"c":"green","label":"SSH login","path":["ans","leaf1"]}],"text":"Ansible logs in to leaf1 as `admin` with the decrypted password. The switch never knows a vault was involved.","title":"The switch just sees a login."},{"bad":["clone"],"packets":[{"label":"git clone","path":["repo","clone"]}],"say":{"clone":"ciphertext,\nno key"},"sayAt":{"clone":{"narrow":"right","wide":"right"}},"show":["clone"],"text":"Anyone who clones the repo has `vault.yml` but no key. Without `.vault-pass`, the password stays unreadable.","title":"A clone gets only ciphertext."},{"code":{"hl":[1],"lines":["before_script:","  - echo \"${VAULT_PASSWORD:-not-needed-for-lint}\" \u003e .vault-pass"],"title":".gitlab-ci.yml"},"hot":["ci"],"packets":[{"label":"fresh clone","path":["repo","ci"]}],"show":["ci"],"text":"A job starts from a fresh clone, writes `.vault-pass` from `VAULT_PASSWORD`, and the container is thrown away afterwards.","title":"CI writes the key from a masked variable."}],"w":760}</script>
</figure>
<script src="/js/netsim.c40a98d103487ae5866edfa12abb4afc1d3aa7ad2d12fed4fe925fe00b588106.js" integrity="sha256-xAqY0QNIeuWGbt&#43;hKrtK/B06p60tEv7U/pJf4AtYgQY=" defer></script>

<hr>
<h2 id="checking-you-actually-fixed-it">Checking you actually fixed it</h2>
<p>Encrypting the file you were looking at isn&rsquo;t the same as getting the password out of the repo. Search every
<strong>tracked</strong> file for the old password, not just the one you edited, because it tends to turn up in a README,
a test script or an example inventory somebody copied. <code>git grep</code> does exactly that: it searches what Git
would commit, and nothing else.</p>
<p>Expect some hits you meant. A lab&rsquo;s README that documents the login on purpose is fine. Anything you&rsquo;d copy
into production isn&rsquo;t.</p>
<p>And if the password was <em>ever</em> committed in clear text, assume it&rsquo;s compromised and rotate it. Removing it
from the latest commit doesn&rsquo;t remove it from history.</p>
<hr>
<h2 id="where-vault-stops-being-enough">Where vault stops being enough</h2>
<p>ansible-vault is the right first step, and for a lab or a small team it might be all you need. It has limits
worth knowing before you outgrow it:</p>
<ul>
<li><strong>One key unlocks everything.</strong> Everyone who can run the playbooks can read every secret. Vault IDs (separate
keys per environment) help a little.</li>
<li><strong>Rotation is manual.</strong> Change a device password and someone has to edit, re-encrypt and commit.</li>
<li><strong>The key still has to live somewhere.</strong> On a laptop it&rsquo;s a file; in CI it&rsquo;s a masked variable. Either way,
it&rsquo;s a long-lived secret protecting other secrets.</li>
</ul>
<p>The next step is a <strong>secrets manager</strong>: HashiCorp Vault, CyberArk, AWS Secrets Manager, Azure Key Vault. Ansible
fetches secrets at runtime through a lookup plugin, nothing sensitive is stored in Git at all, and access is
per person or per pipeline, audited and revocable. If you run Ansible Automation Platform, its credential store
does the same job and keeps secrets out of the playbook&rsquo;s reach entirely.</p>
<p>The playbooks don&rsquo;t change much when you move: <code>ansible_password</code> just gets its value from a lookup instead
of a <code>vault_</code> variable. That&rsquo;s another reason the <code>vault_</code> pattern pays off. The plain files never cared where
the value came from.</p>
<hr>
<h2 id="where-people-get-it-wrong">Where people get it wrong</h2>
<h3 id="1-committing-the-key-next-to-the-lock">1. Committing the key next to the lock</h3>
<p><code>.vault-pass</code> in the repo, or the vault password in the README, or <code>echo</code> in a committed script. All three
undo the encryption. The key lives outside Git, always.</p>
<h3 id="2-encrypting-everything">2. Encrypting everything</h3>
<p>A fully encrypted <code>group_vars</code> file can&rsquo;t be reviewed, diffed or searched. Encrypt secrets only, with the
<code>vault_</code> pattern.</p>
<h3 id="3-printing-secrets">3. Printing secrets</h3>
<p><code>debug: var=ansible_password</code> prints it decrypted, straight into your CI log. Any task that handles a secret
should have <code>no_log: true</code>, and nothing should ever <code>debug</code> one.</p>
<h3 id="4-its-only-the-lab-password">4. &ldquo;It&rsquo;s only the lab password&rdquo;</h3>
<p>Lab passwords get reused, lab repos get made public, and lab inventories get copied into production. Build the
habit where it&rsquo;s cheap.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/ansible-101/#chapter-7">Chapter 7 of the lab</a> moves the switch password into a vault file with the
<code>vault_</code> pattern, keeps the key out of Git, and encrypts the SNMP community inline with <code>encrypt_string</code>.</p>
<p>Next, in <a href="/lessons/ansible-101/production/">lesson 8</a>, the finale: running all of this the way production does, in
an execution environment, lint-clean, from a pipeline.</p>
]]></content:encoded></item></channel></rss>