In this tutorial, you’ll learn how to configure HashiCorp Vault Agent for automatic AppRole login and dynamic template rendering. By the end, you’ll have a Vault Agent setup that fetches a token via AppRole and injects secrets into a configuration file.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Prerequisites
- A local Vault server running, unsealed, and accessible at
http://127.0.0.1:7200. - Vault CLI (
vault) installed and authenticated as an operator. - Basic knowledge of Vault’s AppRole auth method and KV secrets engine.
Ensure your Vault server is unsealed and you have the
root or equivalent token in VAULT_TOKEN before proceeding.1. Enable and Configure AppRole
- Enable the AppRole auth method
- Create an AppRole named
agentwith the policycloud-policy: - Verify the role
Expected output:
2. Retrieve Role ID & Secret ID
- Fetch the Role ID
- Generate a Secret ID
- Store credentials in files for the agent to consume:
Keep
secret.txt secure! Anyone with access can authenticate as the AppRole.3. Create Vault Agent Configuration
Save the following asagent.hcl. It tells the agent how to authenticate and where to write its token.
remove_secret_id_file_after_reading = false.
4. Run the Vault Agent
Start the agent with your configuration:5. Enable Templating
Stop the agent (Ctrl+C) and append atemplate block to agent.hcl:
agent.hcl looks like:
Template File: web.tmpl
6. Populate the KV Store
Store sample credentials underkv/apps/webapp:
7. Restart the Agent and Verify Rendering
Start the agent again:Configuration Blocks Overview
| Block | Purpose |
|---|---|
| vault | Vault server address |
| auto_auth | AppRole login method and token sink |
| sink | File sink for writing the Vault token |
| template | Source and destination for rendering secrets |
Conclusion
You’ve successfully:- Enabled the AppRole auth method in Vault
- Retrieved Role ID and Secret ID for machine identity
- Configured Vault Agent for auto-authentication and token storage
- Rendered secrets into a dynamic configuration file using templating