Ansible / Intermediate: reuse, secrets, and scoped validation / Lesson 11
Protect secrets across storage and execution
Distinguish Vault encryption at rest, secret retrieval, no_log, diff suppression, files, and downstream disclosure.
On this page
Lesson goals
- Explain what Vault does and does not protect.
- Trace secret-bearing data through controller and target boundaries.
- Design diagnostic and artifact policies without real credentials.
Vault protects stored content, not every use
Ansible Vault encrypts variables or files at rest. Authorized execution needs a decryption source, and the resulting plaintext can then pass through templates, module arguments, temporary files, logs, or target configuration. Encryption of the repository therefore does not make every downstream action safe. A password source can itself execute a retrieval script or contact a secret system, so do not treat secret loading as inert parsing. This course reads no Vault file, prompts for no password, and contacts no secret service. It uses explicit synthetic markers and policy models only.
Vault labels and access authority are separate concepts
Vault IDs help identify or select password sources in supported workflows, especially when several encrypted data sets are used. A label should not be mistaken for an authorization boundary by itself. The project must still control who can obtain decryption material and which automation can use it. Separate environments and least-privilege retrieval policies reduce accidental cross-environment exposure. Keep source identities and audit events without printing secret values. A renamed variable or encrypted filename does not revoke a credential already exposed; rotation and downstream recovery are separate operational procedures.
no_log and diff controls reduce specific disclosure paths
no_log can suppress task result output according to Ansible behavior, but it is not a universal data-loss-prevention system. A later debug task, application log, callback, external command, or persisted artifact can expose the same value. Diff output can reveal configuration content, so secret-bearing tasks may also need diff: false and careful artifact policies. Avoid putting secrets in task names, loop labels, or command-line arguments that other processes or logs can inspect. A safe diagnostic should show which input source and validation failed without dumping every variable.
Use a synthetic marker to review output policy
The following task describes a secret-shaped file contract with a clearly non-secret marker. It remains illustrative because writing any target file is an effect requiring separate scope and authorization. The restrictive file mode and suppressed diff express two different controls; neither proves all controller and application paths are protected. In a real project, validate destination ownership, lifecycle, backups, and application behavior too. Do not fabricate a Vault ciphertext block and imply it was encrypted or tested. Honest examples identify which parser and model were actually exercised.
- name: Illustrate protected synthetic content
hosts: lab_nodes
gather_facts: false
vars:
lab_token: LAB-NOT-A-CREDENTIAL
tasks:
- name: Describe a protected file contract
ansible.builtin.copy:
content: "{{ lab_token }}"
dest: /srv/synthetic-report/token.txt
mode: '0600'
no_log: true
diff: false
The marker is not a credential. YAML parsing does not decrypt, copy, suppress a real log, or change a target file; no Vault or module engine was executed.
Test redaction and access without using a real secret
An offline redaction fixture can show which fields a diagnostic is permitted to publish. It is not a test of no_log, callback plugins, or a secret manager. Later authorized tests should use non-secret canaries and inspect each approved output channel, temporary artifact, and destination permission. Do not deliberately send a real credential through logs to prove a filter works. Define retention and access for results, and keep failure diagnostics useful enough to identify the missing source or permission without revealing plaintext. Security requires a complete lifecycle, not one encryption checkbox.
record = {'source': 'lab-vault-label', 'token': 'LAB-NOT-A-CREDENTIAL', 'valid': True}
public = {key: value for key, value in record.items() if key != 'token'}
print(public['source'])
print('token_present=' + str('token' in public))
lab-vault-label
token_present=False
This dictionary filter excludes the synthetic token from a public diagnostic. It does not validate Ansible no_log or any real secret transport.
Common errors and fixes
Vault encryption is called complete runtime secrecy
- Why it happens
- Plaintext use and downstream outputs were omitted.
- How to fix it
- Trace the value through every execution and artifact boundary.
- Prevent it next time
- Separate at-rest and in-use protection reviews.
no_log is expected to hide a later debug task
- Why it happens
- One taskโs output policy was generalized globally.
- How to fix it
- Review each consumer and output channel.
- Prevent it next time
- Use synthetic canaries for disclosure tests.
A Vault label is treated as an access control
- Why it happens
- Selection metadata was confused with decryption authority.
- How to fix it
- Enforce approved password-source and environment access.
- Prevent it next time
- Audit source use without exposing values.
Check your understanding
1. Why can encrypted repository content still leak during a play?
Show worked solution
It is decrypted for authorized use and can then reach logs, module arguments, templates, files, or downstream applications. Review those paths and protect them independently rather than assuming repository encryption persists through every operation.
2. What should a redaction test use instead of a real credential?
Show worked solution
Use a clearly non-secret canary with a documented expected output policy. Inspect allowed channels and artifacts, assert the marker is absent where required, and record that this tests the fixture or configured redaction path rather than all possible disclosure mechanisms.
Key takeaways
- Protect storage, retrieval, use, output, and retention separately.
- Never use real credentials to test a teaching example.