<p>I have a Ansible <code>group_vars</code> directory with the following file within it:</p>

<pre><code>$ cat inventory/group_vars/env1
...
...
ldap_config: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          31636161623166323039356163363432336566356165633232643932623133643764343134613064
          6563346430393264643432636434356334313065653537300a353431376264333463333238383833
          31633664303532356635303336383361386165613431346565373239643431303235323132633331
          3561343765383538340a373436653232326632316133623935333739323165303532353830386532
          39616232633436333238396139323631633966333635393431373565643339313031393031313836
          61306163333539616264353163353535366537356662333833653634393963663838303230386362
          31396431636630393439306663313762313531633130326633383164393938363165333866626438
...
...
</code></pre>

<p>This Ansible encrypted string has a Kubernetes secret encapsulated within it. A base64 blob that looks something like this:</p>

<pre><code>IyMKIyBIb3N0IERhdGFiYXNlCiMKIyBsb2NhbGhvc3QgaXMgdXNlZCB0byBjb25maWd1cmUgdGhlIGxvb3BiYWNrIGludGVyZmFjZQojIHdoZW4gdGhlIHN5c3RlbSBpcyBib290aW5nLiAgRG8gbm90IGNoYW5nZSB0aGlzIGVudHJ5LgojIwoxMjcuMC4wLjEJbG9jYWxob3N0CjI1NS4yNTUuMjU1LjI1NQlicm9hZGNhc3Rob3N0Cjo6MSAgICAgICAgICAgICBsb2NhbGhvc3QKIyBBZGRlZCBieSBEb2NrZXIgRGVza3RvcAojIFRvIGFsbG93IHRoZSBzYW1lIGt1YmUgY29udGV4dCB0byB3b3JrIG9uIHRoZSBob3N0IGFuZCB0aGUgY29udGFpbmVyOgoxMjcuMC4wLjEga3ViZXJuZXRlcy5kb2NrZXIuaW50ZXJuYWwKIyBFbmQgb2Ygc2VjdGlvbgo=
</code></pre>

<p>How can I decrypt this in a single CLI?</p>

slm
<p>We can use an Ansible adhoc command to retrieve the variable of interest, <code>ldap_config</code>. To start we're going to use this adhoc to retrieve the Ansible encrypted vault string:</p>

<pre><code>$ ansible -i "localhost," all               \
    -m debug                                \
    -a 'msg="{{ ldap_config }}"'            \
    --vault-password-file=~/.vault_pass.txt \
    -e@inventory/group_vars/env1
localhost | SUCCESS =&gt; {
    "msg": "ABCD......."
</code></pre>

<p>Make note that we're: </p>

<ul>
<li>using the <code>debug</code> module and having it print the variable, <code>msg={{ ldap_config }}</code></li>
<li>giving <code>ansible</code> the path to the secret to decrypt encrypted strings</li>
<li>using the notation <code>-e@&lt; ...path to file...&gt;</code> to pass the file with the encrypted vault variables</li>
</ul>

<p>Now we can use Jinja2 filters to do the rest of the parsing:</p>

<pre><code>$ ansible -i "localhost," all                             \
     -m debug                                             \
     -a 'msg="{{ ldap_config | b64decode | from_yaml }}"' \
     --vault-password-file=~/.vault_pass.txt              \
     -e@inventory/group_vars/env1
localhost | SUCCESS =&gt; {
    "msg": {
        "apiVersion": "v1",
        "bindDN": "uid=readonly,cn=users,cn=accounts,dc=mydom,dc=com",
        "bindPassword": "my secret password to ldap",
        "ca": "",
        "insecure": true,
        "kind": "LDAPSyncConfig",
        "rfc2307": {
            "groupMembershipAttributes": [
                "member"
            ],
            "groupNameAttributes": [
                "cn"
            ],
            "groupUIDAttribute": "dn",
            "groupsQuery": {
                "baseDN": "cn=groups,cn=accounts,dc=mydom,dc=com",
                "derefAliases": "never",
                "filter": "(objectclass=groupOfNames)",
                "scope": "sub"
            },
            "tolerateMemberNotFoundErrors": false,
            "tolerateMemberOutOfScopeErrors": false,
            "userNameAttributes": [
                "uid"
            ],
            "userUIDAttribute": "dn",
            "usersQuery": {
                "baseDN": "cn=users,cn=accounts,dc=mydom,dc=com",
                "derefAliases": "never",
                "scope": "sub"
            }
        },
        "url": "ldap://192.168.1.10:389"
    }
}
</code></pre>

<p><strong>NOTE:</strong> The above section <code>-a 'msg="{{ ldap_config | b64decode | from_yaml }}"</code> is what's doing the heavy lifting in terms of converting from Base64 to YAML.</p>

<h3>References</h3>

<ul>
<li><a href="https://stackoverflow.com/questions/37652464/how-to-run-ansible-without-hosts-file">How to run Ansible without hosts file</a></li>
<li><a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data" rel="nofollow noreferrer">https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#filters-for-formatting-data</a></li>
<li><a href="https://stackoverflow.com/questions/39047657/base64-decode-string-in-jinja">Base64 Decode String in jinja</a></li>
<li><a href="https://stackoverflow.com/questions/43467180/how-to-decrypt-string-with-ansible-vault-2-3-0">How to decrypt string with ansible-vault 2.3.0</a></li>
</ul>
