Skip to content

Lesson 12 of 13 · 22m

Running Ansible Without Handing It the Keys

Why delegate_to and lookup('pipe') execute on your control node, ansible.cfg auto-loading, no_log, fact caches, and pinning the Galaxy supply chain.

The control node is the crown jewel, and Ansible will happily run code on it. That's the sentence to hold onto. A playbook is not a declarative manifest that only touches remote hosts — it's an executable that happens to default to SSH. Three mechanisms reach your machine directly: delegate_to: localhost, local_action, and the raw/command/shell family pointed at localhost. Plus one more people forget: lookup('pipe', '...') runs a local command on the control node at template-evaluation time, before a single task ships anywhere.

So "I'll just review this community playbook and run it" is the same threat model as "I'll just run this install.sh." Before you run a playbook you didn't write, grep -nE 'delegate_to: (localhost|127)|local_action|lookup\(.pipe|raw:|script:' — every hit is code executing where your SSH keys, your vault passwords, and your ~/.aws live.

ansible.cfg auto-loads, and that was the attack

Config discovery order: $ANSIBLE_CONFIG, then ./ansible.cfg, then ~/.ansible.cfg, then /etc/ansible/ansible.cfg. The ./ansible.cfg rung is the dangerous one — cd into a cloned repo and run ansible-playbook and that repo's config file is now steering your run: vault_password_file can point at an executable script (yes, the file may be a program Ansible runs to fetch the password), stdout_callback can load a plugin, library can shadow module paths. Ansible skips configs in world-writable directories — /tmp is safe — but a cloned repo in your home dir is not world-writable, so it loads.

The control that's actually verifiable: ANSIBLE_CONFIG=/etc/ansible/ansible.cfg pinned in CI, and awareness that checkout-then-run inherits whatever the author shipped.

Secrets: where they leak that isn't vault

Vault covers at-rest. The leaks are elsewhere:

  • no_log: true on every task that touches a decrypted value — otherwise -v prints the cleartext and CI archives it forever. This includes debug: tasks you added at 2 a.m. and left in.
  • Fact caches are plaintext. fact_caching = jsonfile writes every host's full ansible_facts — NICs, mounts, users, environment — to fact_caching_connection as world-readable JSON unless you set the directory's mode. Redis and memcached caches need their own auth; the convenience has an exposure profile.
  • Callback plugins record everything. ARA and community.general.log_plays persist every task result — registered results included — into a database. Excellent for audit; it is also a copy of everything no_log was trying not to say.
  • --ask-vault-pass vs a password file: the file must be 0600, outside the repo, and never committed — a vault password in git history is the oldest self-own in the ecosystem.
  • unsafe_writes and file mode: on copy/template/lineinfile — a rendered config landing 0644 that should be 0600 is a real, silent, findings-shaped bug.

SSH posture is Ansible posture

Agentless means OpenSSH is the perimeter — both directions. host_key_checking off (a common Stack Overflow "fix") makes every first-connect a MITM invitation; ProxyJump via ansible_ssh_common_args is the correct alternative to ForwardAgent — -A hands a compromised node your agent, and the node can then authenticate as you anywhere else. become with a dedicated ansible user and per-task escalation produces an auth.log/sudo trail that means something; an ansible user that is just root with extra steps produces noise.

The supply chain you pin or get pinned by

ansible-galaxy collection install pulls tarballs of arbitrary modules — code that executes on your nodes — keyed by a namespace on a public registry. That registry has already demonstrated the failure mode twice: a 2021 normalization bug granted access to other users' namespaces (evgeni's die-welt.net writeup), and the October 2023 Galaxy NG migration transferred role namespaces to unrelated GitHub users — installing elan.opencast_user briefly fetched a different person's code entirely. Red Hat's longer-term answer is GPG signatures: ansible-galaxy collection install ns.coll --keyring ~/.ansible/pubring.kbx --signature https://.../detached.asc --required-valid-signature-count 1. Almost nothing on public Galaxy is signed, so in practice the controls are the boring ones: requirements.yml with exact versions, vendored/checked-in collections for sensitive trees, and a review of any role that configures a security boundary before it ever runs.

Field notes

  • no_log on a task with loop logs nothing per item — apply it at the task level, not inside item output.
  • delegate_to/local_action/raw/pipe are also legitimate tools — the grep is a review gate, not a blocklist.
  • The dangerous playbook needn't be malicious: a well-meaning role that local_actions a cleanup rm -rf has your home directory's full filesystem view.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.