Prerequisites
1. Enable the AppRole Auth Method
Enable AppRole so Vault Agent can authenticate:AppRole is a machine-friendly auth method designed for non-interactive workflows.
Learn more: AppRole Auth Method
Learn more: AppRole Auth Method
2. Create an AppRole for the Agent
Define a role with the appropriate policy:3. Retrieve Role ID and Secret ID
Fetch therole_id:
secret_id:
4. Store Role ID & Secret ID in Files
Create two files in your working directory: role.txtEnsure these files have restrictive permissions (e.g.,
chmod 600) to prevent unauthorized access.5. Configure Vault Agent (agent.hcl)
Define auto-auth and token sink settings:
mount_pathdefaults to"approle".- Adjust
addressif your Vault server listens on a different host or port.
6. Start Vault Agent
Run the agent with your configuration:6.1 Preserve the Secret ID File (Optional)
By default, Vault Agent deletessecret.txt. To retain it, add remove_secret_id_file = false:
secret.txt file will persist.
7. Templating with Vault Agent
Vault Agent can render templates populated with secrets. Follow these steps:7.1 Prepare the Template (web.tmpl)
7.2 Seed the KV Store
Populate Vault’s KV engine:7.3 Update agent.hcl with a Template Block
Add a template stanza to render web.tmpl to output.yaml:
agent.hcl snippet:
7.4 Restart Vault Agent & Verify
Conclusion
You’ve now automated the following with Vault Agent:- AppRole-based auto-authentication.
- Securely stored & managed
role_idandsecret_id. - Token persistency with customizable sinks.
- Dynamic templating to inject secrets into configuration files.