Chapter 2. Vault integration
2.1. About the Vault integration Copy linkLink copied to clipboard!
The integration of Red Hat Ansible Automation Platform and IBM HashiCorp Vault provides fully automated lifecycle management for Vault.
2.1.1. Introduction Copy linkLink copied to clipboard!
Vault lets you centrally store and manage secrets securely. The Ansible Automation Platform certified hashicorp.vault collection provides fully automated Key/Value V1 and V2 (KV1 and KV2) secret lifecycle management for Vault. You can create, update, and delete secrets through playbooks.
-
Existing
community.hashi_vaultusers: Thehashicorp.vaultsolution is intended to replace unsupportedcommunity.hashi_vaultcollection. Use the migration path to keep your existing playbooks. For more information about migrating, see Migrating fromcommunity.hashi_vault. -
New Vault users: The
hashicorp.vaultcollection is included in the supported execution environment from automation hub.
Although the hashicorp.vault and hashi.terraform collections work independently of each other and are designed for different tasks, you can use them together in advanced workflows.
2.2. Authenticating to hashicorp.vault Copy linkLink copied to clipboard!
After you install or migrate to the hashicorp.vault collection, authentication is configured in the Ansible Automation Platform user interface. An administrator creates a custom credential type to authenticate to Vault. Users create credentials (based on the credential type) to use with job templates in Ansible Automation Platform.
2.2.1. Authentication architecture Copy linkLink copied to clipboard!
The hashicorp.vault collection manages authentication through environment variables and client initialization. This approach enhances security by preventing sensitive credentials from being passed directly as module parameters within playbook tasks.
The hashicorp.vault collection injects credentials into job templates with environment variables, so you get simpler, cleaner task definitions while ensuring that authentication details remain secure.
The following authentication types are supported:
appRole authentication: Use either one of the following methods when using appRole authentication:
-
Set the
VAULT_APPROLE_ROLE_IDandVAULT_APPROLE_SECRET_IDenvironment variables. When you use environment variables, you must also create a custom credential type and credentials that will be passed to the job template. Directly pass the
role_idandsecret_idparameters to the tasks, for example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Set the
Token authentication: Set the
VAULT_TOKENenvironment variable.Optionally, you can configure parameters for the token. If parameters are not provided, then the module uses environment variables.
2.2.2. Creating a custom credential type Copy linkLink copied to clipboard!
As an admin, you create a secure credential type in Ansible Automation Platform, which is used to authenticate to Vault.
You can configure role-based (appRole) authentication or allow users to directly provide a token.
Prerequisites
Do one of the following:
-
New users: Install the Ansible Automation Platform certified
hashicorp.vaultcollection from Automation hub. -
community.hashi_vaultcollection users: Migrate fromcommunity.hashi_vault. For more information, see Migrating fromcommunity.hashi_vault.
Procedure
- Log in to Ansible Automation Platform.
-
From the navigation panel, select
. - Click . The Create Credential Type page opens.
- Enter a name and a description in the corresponding fields.
If you want to configure token authentication for individual users:
For Input configuration, enter:
fields: - id: vault_token type: string label: Hashicorp Vault Token secret: true
fields: - id: vault_token type: string label: Hashicorp Vault Token secret: trueCopy to Clipboard Copied! Toggle word wrap Toggle overflow For Injector configuration, enter:
env: VAULT_TOKEN: '{{ vault_token }}'env: VAULT_TOKEN: '{{ vault_token }}'Copy to Clipboard Copied! Toggle word wrap Toggle overflow
If you want to configure appRole authentication using
role_idandsecret_id:For Input configuration, enter:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For Injector configuration, enter:
env: VAULT_APPROLE_ROLE_ID: '{{ vault_approle_role_id }}' VAULT_APPROLE_SECRET_ID: '{{ vault_approle_secret_id }}'env: VAULT_APPROLE_ROLE_ID: '{{ vault_approle_role_id }}' VAULT_APPROLE_SECRET_ID: '{{ vault_approle_secret_id }}'Copy to Clipboard Copied! Toggle word wrap Toggle overflow
- Click .
Next step
Additional resources
2.2.3. Creating a custom credential Copy linkLink copied to clipboard!
Vault users must create a custom credential to use with job templates in Ansible Automation Platform.
Prerequisite
- Your administrator has created a Vault credential type.
Procedure
- Log in to Ansible Automation Platform.
-
From the navigation panel, select
, and then select . - Enter a name and a description in the corresponding fields.
- (Optional) From the Organization list, select an organization.
- From the Credential type list, select a Vault credential type. The fields that display depend on the credential type.
Do one of the following:
- For the token authentication, add your Vault token and edit any fields as needed.
- For the appRole authentication method, enter the IDs in the appRole Role ID and appRole Secret ID fields. Edit any other fields as needed.
- Click . You are ready to use the credential in a job template.
2.3. Migrating from community.hashi_vault Copy linkLink copied to clipboard!
If you are using the community.hashi_vault collection, you can migrate your existing playbooks to the hashicorp.vault collection.
2.3.1. Configuring KV1 modules Copy linkLink copied to clipboard!
If you are using KV1 with community.hashi_vault collection, configure the corresponding modules in the hashicorp.vault collection.
2.3.1.1. Configuring the hashicorp.vault.kv1_secret module Copy linkLink copied to clipboard!
Configuring this module is not required for migration because there are no corresponding modules in community.hashi_vault. However, you might want to configure something other than the defaults for auth_method and state after the migration. You can use the examples on Ansible automation hub for reference.
2.3.1.2. Configuring the hashicorp.vault.kv1_secret_info module Copy linkLink copied to clipboard!
The hashicorp.vault.kv1_secret_info module reads KV1 secrets.
The corresponding community.hashi_vault modules are:
-
community.hashi_vault.vault_kv1_get: Retrieves secrets from the HashiCorp Vault KV version 1 secret store. -
community.hashi_vault.vault_kv1_get lookup: Retrieves secrets from the HashiCorp Vault KV version 1 secret store.
Procedure
Replicate the
community.hashi_vault modulesto the followinghashicorp.vault.kv1_secret_secret_infoparameters.Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
(Required) Configure the
pathparameter. This is the path to the secret in thecommunity.hashi_vault.hashi_vaultmodules. Alias:secret_path - If needed, configure the optional parameters.
2.3.1.3. Configuring the hashicorp.vault.kv1_secret_get lookup plugin Copy linkLink copied to clipboard!
The hashicorp.vault.kv1_secret_get lookup plugin module reads KV1 secrets.
The corresponding community.hashi_vault modules are:
-
community.hashi_vault.hashi_vault: Retrieves secrets from HashiCorp Vault. -
community.hashi_vault.vault_kv1_get lookup: Gets secrets from the HashiCorp Vault KV version 1 secret store.
Procedure
Replicate
the community.hashi_vaultmodules to the followinghashicorp.vault.kv1_secret_getparameters.Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
(Required) Configure the secret parameter. This maps to secret in the
community.hashi_vault.hashi_vaultmodules. Alias:secret_path - If needed, configure the optional parameters.
Next step
2.3.1.4. Migration example for the hashicorp.vault.kv1_secret_info module Copy linkLink copied to clipboard!
The following example shows before and after configurations for the hashicorp.vault.kv1_secret_info module.
Example:
Before (community.hashi_vault)
After (hashicorp.vault)
- name: Read a kv1 secret from Vault (hashicorp.vault collection)
hashicorp.vault.kv1_secret_info:
url: https://vault.example.com:8201
token: "{{ vault_token }}"
path: sample
- name: Read a kv1 secret from Vault (hashicorp.vault collection)
hashicorp.vault.kv1_secret_info:
url: https://vault.example.com:8201
token: "{{ vault_token }}"
path: sample
2.3.1.5. Migration example for the hashicorp.vault.kv1_secret_get lookup Copy linkLink copied to clipboard!
The following example shows the KV1 secret get lookup.
Example:
Before (community.hashi_vault)
- name: Retrieve a secret from the Vault
ansible.builtin.debug:
msg: "{{ lookup('community.hashi_vault.vault_kv1_get', 'hello', url='https://vault:8201') }}"
- name: Retrieve a secret from the Vault
ansible.builtin.debug:
msg: "{{ lookup('community.hashi_vault.vault_kv1_get', 'hello', url='https://vault:8201') }}"
After (hashicorp.vault)
- name: Retrieve a secret from the Vault
ansible.builtin.debug:
msg: "{{ lookup('hashicorp.vault.kv1_secret_get',
secret='hello',
url='https://myvault_url:8201') }}"
- name: Retrieve a secret from the Vault
ansible.builtin.debug:
msg: "{{ lookup('hashicorp.vault.kv1_secret_get',
secret='hello',
url='https://myvault_url:8201') }}"
2.3.2. Configuring KV2 modules Copy linkLink copied to clipboard!
If you are using KV2 with community.hashi_vault collection, configure the corresponding modules in the hashicorp.vault collection.
2.3.2.1. Configuring the hashicorp.vault.kv2_secret module Copy linkLink copied to clipboard!
The hashicorp.vault.kv2_secret module performs Create, Update, and Delete (CRUD) operations on KV2 secrets through a unified interface.
The corresponding community.hashi_vault modules are:
-
community.hashi_vault.vault_kv2_write- Write KV2 secrets. -
community.hashi_vault.vault_kv2_delete- Delete KV2 secrets.
Prerequisites
-
Install the Ansible Automation Platform certified
hashicorp.vaultcollection.
Procedure
Replicate your automation tasks from both of the
community.hashi_vaultmodules to the followinghashicorp.vault.kv2_secretparameters. Thehashicorp.vault.kv2_secretparameters are similar tocommunity.hashi_vault.Copy to Clipboard Copied! Toggle word wrap Toggle overflow You must add the
stateparameter to thehashicorp.vault.kv2_secretmodule, as shown above. Valid options are:-
present: This is the equivalent ofcreateorupdatein thecommunity.hashi_vault.vault_kv2modules. -
absent: This is the equivalent ofdelete secretin thecommunity.hashi_vault.vault_kv2modules.
-
2.3.2.2. Configuring the hashicorp.vault.kv2_secret_info module Copy linkLink copied to clipboard!
The hashicorp.vault.kv2_secret_info module reads KV2 secrets.
The corresponding community.hashi_vault module is:
-
community.hashi_vault.vault_kv2_get: Gets secrets from the HashiCorp Vault KV version 2 secret store.
Procedure
Replicate the
community.hashi_vaultmodules to the followinghashicorp.vault.kv2_secret_infoparameters.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Configure the required parameters:
-
path: The path where the secret is located in the community.hashi_vault.hashi_vaultmodules. Alias:secret_path -
url: Maps tourlin thecommunity.hashi_vault.hashi_vaultmodules. Uses the same aliases asvault_address.
-
- If needed, configure the optional parameters.
2.3.2.3. Configuring the hashicorp.vault.kv2_secret_get lookup plugin Copy linkLink copied to clipboard!
The hashicorp.vault.kv2_secret_get lookup plugin module reads KV2 secrets.
The corresponding community.hashi_vault modules are:
-
community.hashi_vault.hashi_vault: Retrieves secrets from HashiCorp Vault. -
community.hashi_vault.vault_kv2_getlookup: Gets secrets from the HashiCorp Vault KV version 2 secret store.
Procedure
Replicate the
community.hashi_vaultmodules to the followinghashicorp.vault.kv2_secret_getparameters.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the following guidance to configure the
hashicorp.vault.kv2_secret_getparameters:-
auth_method: Maps identically toauth_methodin thecommunity.hashi_vault.hashi_vaultmodules. -
mount_point: Maps tomount_pointin thecommunity.hashi_vault.hashi_vaultmodules. Alias:secret_mount_path. -
namespace: Maps tonamespacein thecommunity.hashi_vault.hashi_vaultmodules. Alias:vault_namespace. -
secret: Maps tosecretin thecommunity.hashi_vault.hashi_vaultmodules. -
url: Maps tourlin thecommunity.hashi_vault.hashi_vaultmodules. Uses the same aliases asvault_address. -
version: Maps identically toversionin thecommunity.hashi_vault.hashi_vaultmodules.
-
Next step
2.3.2.4. Migration examples for the hashicorp.vault.kv2_secret module Copy linkLink copied to clipboard!
The following examples show basic before and after configurations for the hashicorp.vault.kv2_secret module.
KV2 delete operations are soft-delete.
Example 1: Basic Secret Write/Create
Before (community.hashi_vault):
After (hashicorp.vault):
Example 2: Basic Secret Delete
Before (community.hashi_vault):
- name: Delete the latest version of the secret/mysecret secret.
community.hashi_vault.vault_kv2_delete:
url: https://vault:8201
path: secret/mysecret
- name: Delete the latest version of the secret/mysecret secret.
community.hashi_vault.vault_kv2_delete:
url: https://vault:8201
path: secret/mysecret
After (hashicorp.vault):
- name: Delete the latest version of the secret/mysecret secret.
hashicorp.vault.kv2_secret:
url: https://vault:8201
path: secret/mysecret
state: absent
- name: Delete the latest version of the secret/mysecret secret.
hashicorp.vault.kv2_secret:
url: https://vault:8201
path: secret/mysecret
state: absent
Example 3: Secret Delete - specific version
Before (community.hashi_vault):
- name: Delete versions 1 and 3 of the secret/mysecret secret.
community.hashi_vault.vault_kv2_delete:
url: https://vault:8201
path: secret/mysecret
versions: [1, 3]
- name: Delete versions 1 and 3 of the secret/mysecret secret.
community.hashi_vault.vault_kv2_delete:
url: https://vault:8201
path: secret/mysecret
versions: [1, 3]
After (hashicorp.vault):
2.3.2.5. Migration examples for the hashicorp.vault.kv2_secret_info module Copy linkLink copied to clipboard!
The following examples show before and after configurations for the hashicorp.vault.kv2_secret_info module.
Example 1: Read a secret with token authentication
Before (community.hashi_vault)
- name: Read the latest version of a kv2 secret from Vault community.hashi_vault.vault_kv2_get:
url: https://vault.example.com:8200
token: "{{ vault_token }}"
path: myapp/config
register: response
- name: Read the latest version of a kv2 secret from Vault community.hashi_vault.vault_kv2_get:
url: https://vault.example.com:8200
token: "{{ vault_token }}"
path: myapp/config
register: response
After (hashicorp.vault)
- name: Read a secret with token authentication
hashicorp.vault.kv2_secret_info:
url: https://vault.example.com:8200
token: "{{ vault_token }}"
path: myapp/config
- name: Read a secret with token authentication
hashicorp.vault.kv2_secret_info:
url: https://vault.example.com:8200
token: "{{ vault_token }}"
path: myapp/config
Example 2: Read a secret with a specific version
Before (community.hashi.vault)
- name: Read version 5 of a secret from kv2
community.hashi_vault.vault_kv2_get:
url: https://vault.example.com:8200
path: myapp/config
version: 5
- name: Read version 5 of a secret from kv2
community.hashi_vault.vault_kv2_get:
url: https://vault.example.com:8200
path: myapp/config
version: 5
After (hashicorp.vault)
- name: Read a secret with a specific version
hashicorp.vault.kv2_secret_info:
url: https://vault.example.com:8200
path: myapp/config
version: 1
- name: Read a secret with a specific version
hashicorp.vault.kv2_secret_info:
url: https://vault.example.com:8200
path: myapp/config
version: 1
2.3.2.6. Migration examples for the hashicorp.vault.kv2_secret_get lookup Copy linkLink copied to clipboard!
The following example shows the KV2 secret get lookup for retrieving the latest version.
Example:
Before (community.hashi_vault)
- name: Return latest KV v2 secret from path
ansible.builtin.debug:
msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/data/hello
token=my_vault_token
url=http://myvault_url:8200') }}"
- name: Return latest KV v2 secret from path
ansible.builtin.debug:
msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/data/hello
token=my_vault_token
url=http://myvault_url:8200') }}"
After (hashicorp.vault)
name: Return latest KV v2 secret from path
ansible.builtin.debug:
msg: "{{ lookup('hashicorp.vault.kv2_secret_get', 'secret=secret/data/hello
url=http://myvault_url:8200') }}"
name: Return latest KV v2 secret from path
ansible.builtin.debug:
msg: "{{ lookup('hashicorp.vault.kv2_secret_get', 'secret=secret/data/hello
url=http://myvault_url:8200') }}"