Ansible: Automate Everything from First Playbook to Production · Vault, Security and Secrets Management
Integrating External Secret Stores: AWS SSM and HashiCorp Vault
Pull secrets from AWS Parameter Store or HashiCorp Vault at deploy time instead of storing them in encrypted files.
What you'll build
A playbook that retrieves database credentials from AWS Systems Manager Parameter Store and application secrets from HashiCorp Vault, injecting them into configuration files at deploy time. No secrets are stored in the repository — not even encrypted.
The idea
Ansible Vault encrypts secrets in the repo, which works for small teams. As the organization grows, external secret stores offer stronger guarantees:
- Centralized rotation — change a password in one place, all deploys pick it up.
- Audit logging — every secret access is logged with who, when, and from where.
- Dynamic secrets — HashiCorp Vault can generate short-lived database credentials per deploy.
- Access control — IAM policies and Vault policies restrict which machines read which secrets.
Ansible accesses external stores through lookup plugins. A lookup runs on the control node at playbook parse time and returns the secret value as a string.
Common lookup plugins:
| Plugin | Source | Install |
|---|---|---|
amazon.aws.ssm_parameter |
AWS SSM Parameter Store | ansible-galaxy collection install amazon.aws |
community.hashi_vault.hashi_vault |
HashiCorp Vault KV store | ansible-galaxy collection install community.hashi_vault |
ansible.builtin.env |
Environment variables | Built-in |
ansible.builtin.file |
Local files | Built-in |
The code
Pull a database password from AWS SSM Parameter Store:
cat <<'PLAYBOOK'
# deploy_with_ssm.yml
---
- name: Deploy app with secrets from AWS SSM
hosts: webservers
become: true
vars:
db_host: "{{ lookup('amazon.aws.ssm_parameter', '/myapp/prod/db_host') }}"
db_password: "{{ lookup('amazon.aws.ssm_parameter', '/myapp/prod/db_password', decrypt=True) }}"
api_key: "{{ lookup('amazon.aws.ssm_parameter', '/myapp/prod/api_key', decrypt=True) }}"
tasks:
- name: Deploy database configuration
ansible.builtin.template:
src: templates/db.conf.j2
dest: /etc/myapp/db.conf
owner: appuser
group: appuser
mode: "0600"
notify: restart app
- name: Deploy API configuration
ansible.builtin.template:
src: templates/api.conf.j2
dest: /etc/myapp/api.conf
owner: appuser
group: appuser
mode: "0600"
notify: restart app
handlers:
- name: restart app
ansible.builtin.systemd:
name: myapp
state: restarted
PLAYBOOK
Output
# deploy_with_ssm.yml
---
- name: Deploy app with secrets from AWS SSM
hosts: webservers
become: true
vars:
db_host: "{{ lookup('amazon.aws.ssm_parameter', '/myapp/prod/db_host') }}"
db_password: "{{ lookup('amazon.aws.ssm_parameter', '/myapp/prod/db_password', decrypt=True) }}"
api_key: "{{ lookup('amazon.aws.ssm_parameter', '/myapp/prod/api_key', decrypt=True) }}"
tasks:
- name: Deploy database configuration
ansible.builtin.template:
src: templates/db.conf.j2
dest: /etc/myapp/db.conf
owner: appuser
group: appuser
mode: "0600"
notify: restart app
- name: Deploy API configuration
ansible.builtin.template:
src: templates/api.conf.j2
dest: /etc/myapp/api.conf
owner: appuser
group: appuser
mode: "0600"
notify: restart app
handlers:
- name: restart app
ansible.builtin.systemd:
name: myapp
state: restartedRun this on your computer — see “Get Started” for setup.
Pull secrets from HashiCorp Vault's KV v2 engine:
cat <<'PLAYBOOK'
# deploy_with_hcvault.yml
---
- name: Deploy app with secrets from HashiCorp Vault
hosts: webservers
become: true
vars:
vault_addr: "https://vault.internal:8200"
db_creds: "{{ lookup('community.hashi_vault.hashi_vault',
'secret/data/myapp/database',
url=vault_addr,
auth_method='approle',
role_id=lookup('env', 'VAULT_ROLE_ID'),
secret_id=lookup('env', 'VAULT_SECRET_ID')) }}"
tasks:
- name: Show retrieved secret keys (never log values)
ansible.builtin.debug:
msg: "Retrieved keys: {{ db_creds.data.data.keys() | list }}"
- name: Deploy database configuration
ansible.builtin.template:
src: templates/db.conf.j2
dest: /etc/myapp/db.conf
owner: appuser
group: appuser
mode: "0600"
vars:
db_host: "{{ db_creds.data.data.host }}"
db_password: "{{ db_creds.data.data.password }}"
no_log: true
notify: restart app
handlers:
- name: restart app
ansible.builtin.systemd:
name: myapp
state: restarted
PLAYBOOK
Output
# deploy_with_hcvault.yml
---
- name: Deploy app with secrets from HashiCorp Vault
hosts: webservers
become: true
vars:
vault_addr: "https://vault.internal:8200"
db_creds: "{{ lookup('community.hashi_vault.hashi_vault',
'secret/data/myapp/database',
url=vault_addr,
auth_method='approle',
role_id=lookup('env', 'VAULT_ROLE_ID'),
secret_id=lookup('env', 'VAULT_SECRET_ID')) }}"
tasks:
- name: Show retrieved secret keys (never log values)
ansible.builtin.debug:
msg: "Retrieved keys: {{ db_creds.data.data.keys() | list }}"
- name: Deploy database configuration
ansible.builtin.template:
src: templates/db.conf.j2
dest: /etc/myapp/db.conf
owner: appuser
group: appuser
mode: "0600"
vars:
db_host: "{{ db_creds.data.data.host }}"
db_password: "{{ db_creds.data.data.password }}"
no_log: true
notify: restart app
handlers:
- name: restart app
ansible.builtin.systemd:
name: myapp
state: restartedRun this on your computer — see “Get Started” for setup.
A requirements.yml to pin the needed collections:
cat <<'EOF'
# requirements.yml
---
collections:
- name: amazon.aws
version: ">=7.0.0,<8.0.0"
- name: community.hashi_vault
version: ">=6.0.0,<7.0.0"
- name: community.general
version: ">=8.0.0,<9.0.0"
# Install: ansible-galaxy collection install -r requirements.yml
EOF
Output
# requirements.yml
---
collections:
- name: amazon.aws
version: ">=7.0.0,<8.0.0"
- name: community.hashi_vault
version: ">=6.0.0,<7.0.0"
- name: community.general
version: ">=8.0.0,<9.0.0"
# Install: ansible-galaxy collection install -r requirements.ymlRun this on your computer — see “Get Started” for setup.
Use environment variables as a simple fallback for CI/CD:
# When no external store is available, fall back to CI/CD env vars
vars:
db_password: "{{ lookup('env', 'DB_PASSWORD') }}"
api_key: "{{ lookup('env', 'API_KEY') }}"
Common mistakes
| Mistake | Fix |
|---|---|
| Logging secret values in task output | Use no_log: true on every task that handles secrets; debug only key names, never values |
| Hardcoding Vault URLs and paths in playbooks | Put them in group_vars/ so each environment points to its own secret path |
| Using root Vault tokens in CI | Use AppRole or JWT auth with scoped policies; root tokens have unlimited access |
| Forgetting to install the lookup plugin collection | Add collections to requirements.yml and install them in your CI setup step |
Key points
- Lookup plugins run on the control node, not the managed host — secrets travel over SSH, never stored on disk at the target.
no_log: trueprevents Ansible from printing secret values in task output or logs.- AWS SSM with
decrypt=Truehandles SecureString parameters encrypted with KMS. - HashiCorp Vault's AppRole auth is designed for machines; never use human tokens in automation.
- For small teams, Ansible Vault (encrypted files) is simpler; for organizations with rotation and audit requirements, external stores are worth the setup cost.
Practice questions
Where do Ansible lookup plugins execute — on the control node or on managed hosts?
Answer
On the control node. The secret is retrieved locally and then passed to the managed host over SSH as a variable value. The secret is never written to disk on the target unless a task explicitly does so.
Why should you add `no_log: true` to tasks that handle secrets?
Answer
Without no_log, Ansible prints the full task output including variable values to stdout and log files. With no_log: true, the output is suppressed to prevent secrets from appearing in CI logs, terminal history, or Ansible log files.
What is the advantage of external secret stores over Ansible Vault encrypted files?
Answer
External stores provide centralized rotation (change once, all deploys pick it up), audit logging of every access, dynamic short-lived credentials, and IAM-based access control. Ansible Vault works well for small teams but requires re-encrypting files whenever a secret changes.
Open in the app to mark lessons complete, take notes and track your progress.