How do you integrate HashiCorp Vault with Ansible?
AdvancedProductionSecurity and Secrets
Answer
Use the community.hashi_vault collection (it needs the hvac Python library on the controller). Authenticate with AppRole, JWT/OIDC or a cloud auth method rather than a long-lived token.
db_password: >-
{{ lookup('community.hashi_vault.hashi_vault',
'secret/data/prod/app:db_password',
url='https://vault.example.com:8200',
auth_method='approle',
role_id=vault_role_id,
secret_id=vault_secret_id) }}
Issues to design for: the secret_id is itself a secret (deliver it through CI as a short-lived, response-wrapped value), every lookup is a network call so fetch once per play, and Vault policies should give each pipeline access only to its environment's paths. AAP has a native HashiCorp Vault credential type that does this without code.
More on Security and Secrets
How do you set up a secure automation user and SSH access?BeginnerHow do you give the automation user sudo with least privilege?IntermediateWhat are the alternatives to Ansible Vault for secrets, and when do you use them?IntermediateHow do you read AWS Secrets Manager and SSM parameters in a playbook?IntermediateHow do you stop secrets from leaking into logs and output?BeginnerHow do you handle the vault password in CI/CD?Intermediate