Skip to main content
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.

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

  1. Enable the AppRole auth method
  2. Create an AppRole named agent with the policy cloud-policy:
  3. Verify the role
    Expected output:

2. Retrieve Role ID & Secret ID

  1. Fetch the Role ID
  2. Generate a Secret ID
  3. 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 as agent.hcl. It tells the agent how to authenticate and where to write its token.
If you prefer to keep the Secret ID after login, set remove_secret_id_file_after_reading = false.

4. Run the Vault Agent

Start the agent with your configuration:
You should see logs like:
Verify the token is written:

5. Enable Templating

Stop the agent (Ctrl+C) and append a template block to agent.hcl:
Now your full agent.hcl looks like:

Template File: web.tmpl


6. Populate the KV Store

Store sample credentials under kv/apps/webapp:

7. Restart the Agent and Verify Rendering

Start the agent again:
You should see template rendering logs:
Inspect the generated file:

Configuration Blocks Overview


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
For more details, visit the Vault Agent Documentation.

Watch Video