Skip to main content
Learn how to leverage HashiCorp Vault Agent to automatically authenticate via AppRole and render configuration files with secrets fetched from Vault.

Prerequisites


1. Enable the AppRole Auth Method

Enable AppRole so Vault Agent can authenticate:
Expected output:
AppRole is a machine-friendly auth method designed for non-interactive workflows.
Learn more: AppRole Auth Method

2. Create an AppRole for the Agent

Define a role with the appropriate policy:
Verify the role settings:
Sample output:

3. Retrieve Role ID and Secret ID

Fetch the role_id:
Generate a one-time secret_id:
Example JSON response:

4. Store Role ID & Secret ID in Files

Create two files in your working directory: role.txt
secret.txt
Ensure 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_path defaults to "approle".
  • Adjust address if your Vault server listens on a different host or port.

6. Start Vault Agent

Run the agent with your configuration:
You should see logs indicating successful authentication and token writing:
Verify the token:

6.1 Preserve the Secret ID File (Optional)

By default, Vault Agent deletes secret.txt. To retain it, add remove_secret_id_file = false:
Restart Vault Agent. The 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:
Full agent.hcl snippet:

7.4 Restart Vault Agent & Verify

Check the rendered file:
Expected content:

Conclusion

You’ve now automated the following with Vault Agent:
  1. AppRole-based auto-authentication.
  2. Securely stored & managed role_id and secret_id.
  3. Token persistency with customizable sinks.
  4. Dynamic templating to inject secrets into configuration files.

Watch Video