Security

Secret Masking in ansible-core 2.22: Registering Secrets and the Limits of $REDACTED$

ansible-core 2.22 added secret masking: values you register are replaced with $REDACTED$ at the output boundaries Ansible controls, while the real value stays usable in tasks, templates and conditionals. This guide shows how to register a secret from a playbook, proves the value is not altered, demonstrates why the no_log task keyword does not do the same job, and spells out the documented places masking does not reach.

Version scope and prerequisites

Secret masking is new in ansible-core 2.22, released 2026-11-02, and requires a controller Python of 3.13 to 3.15. What is new, and therefore what will not work on 2.21 or earlier, is the automatic registration of known secrets and the register_secret and mask_secrets filters. The older protections discussed alongside them — the no_log task keyword, diff: false, and Ansible Vault — are long-standing and work on earlier releases too. Run these examples in your own virtual environment, with versions you control before and after the change, rather than in the playground on this site: it always runs in check mode, which is not a security sandbox, and its input screening is meant to block risky playbook content, not to feed back real secrets you might register. Run these examples somewhere you choose what goes in and out, and never paste real credentials into a public runner.

Every value used below is a fake, obviously-labelled token. Do not adapt these examples by pasting a real credential into a playbook, a shell history, or a log you intend to share. Masking reduces the chance of a leak; it is not a reason to handle real secrets more casually.

Common setup

mkdir -p ~/tma-masking && cd ~/tma-masking
python3.13 -m venv .venv
.venv/bin/python -m pip install 'ansible-core==2.22.0'
.venv/bin/ansible --version
[defaults]
interpreter_python = auto_silent
[local]
localhost ansible_connection=local

Expected result from the version check: ansible [core 2.22.0]. If it reports 2.21 or lower, the register_secret filter will fail with No filter named 'register_secret' and nothing else in this article will reproduce.

What masking is, and what it is not

Masking happens at an egress boundary — a point where data leaves Ansible and could be seen or stored by something else. Upstream names three: display output (the screen and the log_path file, including task banners, debug messages, warnings, deprecations, errors, tracebacks and verbose module invocation lines); task results handed to callback plugins; and module logging to syslog or the Windows Event Log on the managed node.

Data that stays inside Ansible is never touched. Variables, module arguments and task results keep the real value. That is the whole design: because the redaction is applied to the output rather than to the data, a value registered late in a play is still masked in every later message, and the same value remains usable in a conditional or a template.

Masking is not encryption and not access control. It is a string search over outgoing text. Upstream states plainly that it is best effort and cannot guarantee a secret never leaks. Ansible Vault still protects data at rest, the no_log task keyword still has jobs only it can do, and file permissions still matter.

What gets registered without you asking

Several categories are registered automatically in 2.22: vault-encrypted content once decrypted, and every string or numeric value inside a vault-encrypted vars file; vault passwords supplied by prompt, file or script; values entered for --ask-pass, --ask-become-pass and --ask-vault-pass; prompted values from vars_prompt with private: true and from the pause module with echo: false; connection and become passwords and the SSH private key and passphrase; module options that the module author marked no_log: true in the argument spec; and the values produced by the password and unvault lookups and the vault and unvault filters.

How vault content is registered depends on how it is loaded, and the difference matters. A vaulted vars file is parsed and each nested string, integer and float is registered on its own, so a password inside it is masked wherever that password appears. The unvault lookup, by contrast, registers the entire decrypted text as one string, so a value inside it is only masked when the whole text appears. If you load structured secrets that way, parse the text and register the individual values yourself.

Registering a secret from a playbook

The register_secret filter takes a string, registers it, and returns it unchanged — so you can use it inline wherever the value is first produced. This play registers a fake token, asserts that the value really is untouched, prints it to show the redaction, and then shows a derived form that is not masked.

---
- name: Register a value as a secret and keep using it
  hosts: local
  gather_facts: false

  vars:
    fake_token_source: "tma-demo-token-0a1b2c3d4e5f"

  tasks:
    - name: Register the fake token as a secret
      ansible.builtin.set_fact:
        api_token: "{{ fake_token_source | register_secret }}"
      no_log: true

    - name: The registered value is unchanged inside Ansible
      ansible.builtin.assert:
        that:
          - api_token == fake_token_source
          - api_token | length == 27
        success_msg: Masking did not alter the value.

    - name: The same value is redacted on its way to the screen
      ansible.builtin.debug:
        msg: "Authorization: Bearer {{ api_token }}"

    - name: Derived forms are different strings and are not masked
      ansible.builtin.debug:
        msg: "sha256 of the token: {{ api_token | hash('sha256') }}"
.venv/bin/ansible-playbook -i hosts.ini masking-demo.yml

Expected result:

TASK [The registered value is unchanged inside Ansible] *****
ok: [localhost] => {
    "changed": false,
    "msg": "Masking did not alter the value."
}

TASK [The same value is redacted on its way to the screen] *****
ok: [localhost] => {
    "msg": "Authorization: Bearer $REDACTED$"
}

TASK [Derived forms are different strings and are not masked] *****
ok: [localhost] => {
    "msg": "sha256 of the token: 086a2cf8... (a full hex digest, unredacted)"
}

localhost : ok=4  changed=0  unreachable=0  failed=0  skipped=0

Three things are worth reading carefully. The assertion passes, which is the proof that masking is non-destructive — api_token still equals the source value and is still 27 characters long. The debug message is redacted even though the token is only part of a longer string. And the SHA-256 digest is printed in full, because a hashed copy of a secret is a different string and masking only matches what was registered. The same applies to base64, URL-encoded and upper-cased copies. The one exception upstream calls out is JSON string escaping, which is handled for you.

Two deliberate choices in that play. no_log: true sits on the set_fact task even though masking would cover it, because the registration happens during that task and a failure or edge case there could surface the value before it is registered — the same defence in depth upstream recommends for any task that produces a secret. And the two debug tasks exist only to demonstrate the boundary: printing a credential, masked or not, is a diagnostic step for an article, not a pattern to copy into a playbook.

For a list of values, apply the filter per element rather than to the list: {{ raw_passwords | map('register_secret') | list }}. The filter validates its input — a non-string, or a string that is empty after whitespace is stripped, raises an error by default. Pass validation_action='warn' or 'ignore' for a value that is legitimately optional; in both cases the value is returned unchanged and nothing is registered.

When you write text somewhere Ansible does not control — a file on the managed node, a message to a chat webhook — use mask_secrets to redact registered values yourself:

- name: Sanitize text you write somewhere Ansible does not control
  ansible.builtin.debug:
    msg: "{{ ('token=' ~ api_token) | mask_secrets(mask_placeholder='***') }}"

Expected result: "msg": "token=***".

The negative case: no_log on one task protects only that task

This example deliberately prints a fake token. It exists to show a failure mode. Do not run the same shape of play with a real credential, and do not keep a play like this in a repository as an example of acceptable practice.

The no_log task keyword censors the result of the task it is set on. It does not register anything as a secret, because Ansible has no way to know which value inside a result is the sensitive one — registering every value would redact ordinary strings like present for the rest of the run. So the registered result still holds the plain value, and the next task that prints it prints it in full.

---
- name: no_log on one task does not protect later tasks
  hosts: local
  gather_facts: false

  tasks:
    - name: Produce a fake token with the task result censored
      ansible.builtin.command: echo tma-demo-token-0a1b2c3d4e5f
      register: token_result
      no_log: true
      changed_when: false

    - name: The censored result still holds the value in plain form
      ansible.builtin.debug:
        var: token_result.stdout

Expected result — the first task is censored, the second is not:

TASK [Produce a fake token with the task result censored] *****
ok: [localhost]

TASK [The censored result still holds the value in plain form] *****
ok: [localhost] => {
    "token_result.stdout": "tma-demo-token-0a1b2c3d4e5f"
}

The repair is a register projection: the dictionary form of register lets you store the value and mark it as a secret in the same step, because register_secret returns its input unchanged. Keep no_log: true on the producing task as well — upstream recommends it so that a failure or edge case cannot expose the value before it is registered.

---
- name: Register the produced value as a secret so later output is masked
  hosts: local
  gather_facts: false

  vars:
    fake_token: "tma-demo-token-0a1b2c3d4e5f"

  tasks:
    - name: Produce a fake token and register it as a secret
      ansible.builtin.command: "echo {{ fake_token }}"
      register:
        api_token: _task.result.stdout | register_secret
        token_result: _task.result
      no_log: true
      changed_when: false

    - name: The full result can now be printed with the token masked
      ansible.builtin.debug:
        var: token_result.stdout

    - name: The value is still usable by later tasks
      ansible.builtin.assert:
        that:
          - api_token == fake_token

    - name: Sanitize text you write somewhere Ansible does not control
      ansible.builtin.debug:
        msg: "{{ ('token=' ~ api_token) | mask_secrets(mask_placeholder='***') }}"

Expected result — the same field now prints the placeholder, and the comparison still succeeds:

TASK [The full result can now be printed with the token masked] *****
ok: [localhost] => {
    "token_result.stdout": "$REDACTED$"
}

TASK [The value is still usable by later tasks] *****
ok: [localhost] => {
    "changed": false,
    "msg": "All assertions passed"
}

localhost : ok=4  changed=0  unreachable=0  failed=0  skipped=0

The module no_log option is a different thing

Ansible uses the name no_log for two unrelated mechanisms, and only one of them changed in 2.22.

  • The module option no_log: true is set by a module author in the argument spec to mark an option as sensitive — the password option of ansible.builtin.user, for example. Playbook authors never set it; they benefit automatically. This changed. Before 2.22 the value was replaced with the literal string VALUE_SPECIFIED_IN_NO_LOG_PARAMETER in the result, so a registered result could not be reused. From 2.22 the real value is kept in the result and registered as a secret instead. Playbooks or tests that check for the old placeholder need updating.
  • The task keyword no_log: true is set by you on a task or play and hides that task's whole result. This did not change, and it is still the right choice when a result could contain a secret in a form masking cannot recognise, when you are hitting one of the limits below, or when you simply cannot say which part of a result is sensitive.

One consequence of the new behaviour deserves care: because a registered value is masked wherever it appears, a very short value redacts unrelated text. A two-character secret turns every ab in later output into $REDACTED$, including inside words like about and table, and the pattern of placeholders can give the value away. Be precise about what the available controls do here. There is no way to unregister a value: once a short secret has been registered, it stays registered for the rest of the run, and no_log: true on a later task does not undo the pollution in output that has already been written. The task keyword only censors the result of the task it is set on, so it keeps a particular task's output out of the log; it does not clean up the run. The durable fixes are upstream of the problem: prefer a longer value where the system accepting it allows, and where it does not, set no_log: true on the task that handles it so the short value never reaches a boundary in the first place. Upstream also notes that the matching rules describe the current implementation and may change, so do not write content or tests that depend on a particular value being masked or left visible.

Where masking does not reach

These are the documented limits. Each one is a place where a registered secret can still appear in plain text.

  • Transformed copies. Hashed, encoded, upper-cased or otherwise rewritten versions of a secret are different strings and are not masked unless registered separately. JSON escaping is the exception.
  • Output that bypasses Ansible. Anything a module writes to a file on the managed node, a plugin prints directly to stdout, or a task sends to an external service never crosses a masked boundary. Use mask_secrets for those.
  • Other Python libraries' logs. The log_path file is the root Python logger, so any library Ansible or a plugin imports writes into the same file. Only messages Ansible writes through Display are masked; an HTTP client logging request headers is not.
  • Persistent connection logging. The persistent_log_messages option used by network connection plugins runs in a separate process that never receives the registered secrets, so passwords are written in plain text. Enable it only while debugging and treat the log as sensitive.
  • Third-party callbacks. A callback receives the task result already masked, but anything it derives itself, and anything it writes to a file or an external service outside Display, is its own responsibility. Review callbacks before relying on masking with them.
  • Fact and inventory caches. Caching is not an output boundary. Cache plugins write real values to disk or an external service. If a secret can end up in a fact, treat the cache as sensitive or do not cache it.
  • Module-local values. The controller only sends a module the secrets that appear in that module's own options. A value the module reads from a file on the target or fetches from an API is unknown to it and unmasked in its own logs. It is masked once the result reaches the controller only if the controller knows about it — because it was already registered there, or because the module registered it itself through the ansible.module_utils.secrets API and that registration propagated back. A value the module merely returned, and that nothing registered anywhere, is not a secret as far as masking is concerned.
  • Worker boundaries. A secret registered inside a worker is sent back to the controller and inherited by workers forked afterwards. Workers already running for other hosts in the same task do not learn about it, so their output can still show the value.
  • Raw module output at debug level. A new secret arrives inside the module's raw JSON result and is only registered once that result is parsed. With ANSIBLE_DEBUG=1, the raw stdout is displayed before that happens and can show the value. In 2.22 the ssh, winrm and psrp plugins no longer print raw output at high verbosity for exactly this reason; third-party connection plugins may not have that protection.

Two other 2.22 changes are easy to trip over while reading output. The invocation key is no longer included by default in Python and PowerShell module results — set the INJECT_INVOCATION config item if you need it. And AnsibleModule.log() and run_command() no longer apply the old password-shaped heuristics, so a credential embedded in a URL is only redacted if it is a registered secret.

What still belongs in your playbooks

Masking is one layer. The protections that were correct before 2.22 are still correct.

The fragment below is illustrative and was not executed. Unlike the localhost demonstrations above, it writes to /etc on a managed node. Running it needs a template file at templates/app-credentials.j2, an existing /etc/app directory, and privilege escalation to write as root. Adapt it on a host you own; do not paste it into a production play as-is.
---
# ILLUSTRATION ONLY - not executed. Requires templates/app-credentials.j2,
# an existing /etc/app directory, and become privileges on the target.
- name: Render a credentials file
  ansible.builtin.template:
    src: app-credentials.j2
    dest: /etc/app/credentials.yml
    owner: root
    group: root
    mode: "0600"
  become: true
  no_log: true
  diff: false

mode: "0600" and explicit ownership keep the rendered file off other accounts. no_log: true hides the whole result rather than trusting a string match. diff: false matters because --diff prints file contents — a secret that was never registered appears in full in a diff, and that output often ends up in a CI log. There is more on that in what check mode actually proves, and Ansible Vault covers encryption at rest.

Troubleshooting

  • "No filter named 'register_secret'." You are not on 2.22. Check with .venv/bin/ansible --version, not ansible --version.
  • The value prints in full. Confirm the registration actually ran — if the task that registers it was skipped, nothing is registered. Then check whether the printed form is a transformation of the original.
  • Unrelated output is full of placeholders. A very short value got registered. Find it, make it longer, or switch that task to no_log: true.
  • A test broke after upgrading. Look for assertions on VALUE_SPECIFIED_IN_NO_LOG_PARAMETER or on invocation keys; both behaviours changed in 2.22.
  • Something still leaks. Work down the limits list above. Review the log and callback output before sharing it, regardless of what masking reported.

Related reading

Sources