community.hashi_vault.hashi_vault lookup – 从 HashiCorp Vault 检索密钥
注意
此 lookup 插件是 community.hashi_vault 集合 (版本 6.2.0) 的一部分。
如果您使用的是 ansible 包,则可能已安装此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用: ansible-galaxy collection install community.hashi_vault。您需要其他要求才能使用此 lookup 插件,有关详细信息,请参阅 要求。
要在剧本中使用它,请指定: community.hashi_vault.hashi_vault。
概要
- 从 HashiCorp Vault 检索密钥。 
- 考虑 迁移到集合中的其他插件。 
要求
以下要求是在执行此 lookup 的本地控制器节点上需要的。
关键字参数
这描述了 lookup 的关键字参数。这些是在以下示例中的值 key1=value1、key2=value2 等:lookup('community.hashi_vault.hashi_vault', key1=value1, key2=value2, ...) 和 query('community.hashi_vault.hashi_vault', key1=value1, key2=value2, ...)
| 参数 | 注释 | 
|---|---|
| 要使用的身份验证方法。 
 
 
 
 选项 
 配置 
 | |
| 如果指定,则将值设置为用于  配置 
 | |
| Azure AD服务主体或托管标识的客户端ID(也称为应用程序ID)。应为UUID。 如果未指定,将使用系统分配的托管标识。 配置 
 | |
| Azure AD服务主体的客户端密钥。 配置 
 | |
| 在Azure Active Directory中注册的应用程序的资源URL。通常不应更改默认值。 默认值:  配置 
 | |
| 服务主体的Azure Active Directory租户ID(也称为目录ID)。应为UUID。 使用服务主体进行Vault身份验证时需要,例如,当同时指定azure_client_id和azure_client_secret时需要。 使用托管标识进行Vault身份验证时可选。 配置 
 | |
| 用于身份验证的证书路径。 如果未通过任何其他方式指定,则将使用 配置 
 | |
| 对于 配置 
 | |
| 对于 配置 
 | |
| Vault挂载点。 如果未指定,则使用给定身份验证方法的默认挂载点。 不适用于令牌身份验证。 配置 
 | |
| 秘密所在的Vault命名空间。此选项需要HVAC 0.7.0+和Vault 0.11+。 或者,这可以通过使用命名空间作为前缀来实现身份验证挂载点和/或秘密路径(例如 如果设置了环境变量 配置 
 | |
| 身份验证密码。 配置 
 | |
| 用于访问Vault服务的代理的URL。 它可以是字符串或字典。 如果是字典,请提供方案(例如 如果是字符串,请提供单个URL,该URL将用作 可以解释为字典的字符串将转换为字典(参见示例)。 您可以为HTTP和HTTPS资源指定不同的代理。 如果未指定,则使用Requests库中的环境变量。 配置 
 | |
| 允许根据urllib3库中的Retry类重试错误。 此集合定义了重试与Vault连接的推荐默认值。 此选项可以指定为正数(整数)或字典。 如果未指定此选项或数字为 一个数字设置总重试次数,并使用集合默认值进行其他设置。 字典值直接用于初始化 有关重试的详细信息,请参见集合用户指南。 配置 
 | |
| 控制是否以及如何显示retries的消息。 如果未重试请求,则此项无效。 选项 
 配置 
 | |
| 控制返回时如何处理路径中的多个键/值对。 
 
 
 选项 
 | |
| Vault 角色 ID 或名称。用于  对于  对于  配置 
 | |
| Vault 中请求的密钥路径,格式为  | |
| 用于 Vault AppRole 身份验证的密钥 ID。 配置 
 | |
| 设置连接超时时间(秒)。 如果未设置,则使用  配置 
 | |
| Vault 令牌。令牌可以显式指定,也可以通过列出的 [env] 变量以及  如果没有显式或通过环境变量提供令牌,插件将检查令牌文件,该文件由 *token_path* 和 *token_file* 确定。 令牌加载顺序(先找到先用)为: 配置 
 | |
| 如果没有指定令牌,将尝试从 *token_path* 中的此文件读取令牌。 默认:  配置 
 | |
| 如果没有指定令牌,将尝试从此路径读取 *token_file*。 配置 
 | |
| 对于令牌身份验证,将在使用令牌之前执行  如果您的令牌没有  选项 
 配置 
 | |
| Vault 服务的 URL。 如果没有通过其他任何方式指定,则将使用  如果  配置 
 | |
| 身份验证用户名。 配置 
 | |
| 控制 SSL 证书的验证,大多数情况下你只想对自签名证书关闭验证。 如果设置了  如果既没有设置 *validate_certs* 也未设置  选项 
 配置 
 | 
注释
注意
- 由于 HVAC 库的当前限制,如果指定了错误的端点,则不一定会出现错误。 
- 从 community.hashi_vault 0.1.0 版本开始,当指定 KV v2 路径时,只返回密钥的最新版本。 
- 从 community.hashi_vault 0.1.0 版本开始,所有选项都可以通过终端字符串(空格分隔的键值对)或参数提供(参见示例)。 
- 从 community.hashi_vault 0.1.0 版本开始,当 *secret* 是终端字符串中的第一个选项时,不需要 - secret=(参见示例)。
另请参阅
另请参阅
- community.hashi_vault.hashi_vault 迁移指南
- 从 - hashi_vault查找迁移。
- 关于 community.hashi_vault.hashi_vault 查找
- hashi_vault查找的过去、现在和未来。
- community.hashi_vault.vault_read 查找
- community.hashi_vault.vault_read查找插件的官方文档。
- community.hashi_vault.vault_read
- 对 HashiCorp Vault 执行读取操作。 
- community.hashi_vault.vault_kv2_get 查找
- community.hashi_vault.vault_kv2_get查找插件的官方文档。
- community.hashi_vault.vault_kv2_get
- 从 HashiCorp Vault 的 KV 版本 2 密钥存储中获取密钥。 
- community.hashi_vault.vault_kv1_get 查找
- community.hashi_vault.vault_kv1_get查找插件的官方文档。
- community.hashi_vault.vault_kv1_get
- 从 HashiCorp Vault 的 KV 版本 1 密钥存储中获取密钥。 
- community.hashi_vault 查找指南
- 关于在 - community.hashi_vault中使用查找的指导。
示例
- ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/hello:value token=c975b780-d1be-8016-866b-01d0f9b688a5 url=http://myvault:8200') }}"
- name: Return all secrets from a path
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/hello token=c975b780-d1be-8016-866b-01d0f9b688a5 url=http://myvault:8200') }}"
- name: Vault that requires authentication via LDAP
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/hello:value auth_method=ldap mount_point=ldap username=myuser password=mypas') }}"
- name: Vault that requires authentication via username and password
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/hola:val auth_method=userpass username=myuser password=psw url=http://vault:8200') }}"
- name: Connect to Vault using TLS
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/hola:value token=c975b780-d1be-8016-866b-01d0f9b688a5 validate_certs=False') }}"
- name: using certificate auth
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/hi:val token=xxxx url=https://vault:8200 validate_certs=True cacert=/cacert/path/ca.pem') }}"
- name: Authenticate with a Vault app role
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/hello:value auth_method=approle role_id=myroleid secret_id=mysecretid') }}"
- name: Return all secrets from a path in a namespace
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=secret/hello token=c975b780-d1be-8016-866b-01d0f9b688a5 namespace=teama/admins') }}"
# When using KV v2 the PATH should include "data" between the secret engine mount and path (e.g. "secret/data/:path")
# see: https://www.vaultproject.io/api/secret/kv/kv-v2.html#read-secret-version
- 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') }}"
# The following examples show more modern syntax, with parameters specified separately from the term string.
- name: secret= is not required if secret is first
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/hello token=<token> url=http://myvault_url:8200') }}"
- name: options can be specified as parameters rather than put in term string
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/hello', token=my_token_var, url='http://myvault_url:8200') }}"
# return_format (or its alias 'as') can control how secrets are returned to you
- name: return secrets as a dict (default)
  ansible.builtin.set_fact:
    my_secrets: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/manysecrets', token=my_token_var, url='http://myvault_url:8200') }}"
- ansible.builtin.debug:
    msg: "{{ my_secrets['secret_key'] }}"
- ansible.builtin.debug:
    msg: "Secret '{{ item.key }}' has value '{{ item.value }}'"
  loop: "{{ my_secrets | dict2items }}"
- name: return secrets as values only
  ansible.builtin.debug:
    msg: "A secret value: {{ item }}"
  loop: "{{ query('community.hashi_vault.hashi_vault', 'secret/data/manysecrets', token=my_token_var, url='http://vault_url:8200', return_format='values') }}"
- name: return raw secret from API, including metadata
  ansible.builtin.set_fact:
    my_secret: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/hello:value', token=my_token_var, url='http://myvault_url:8200', as='raw') }}"
- ansible.builtin.debug:
    msg: "This is version {{ my_secret['metadata']['version'] }} of hello:value. The secret data is {{ my_secret['data']['data']['value'] }}"
# AWS IAM authentication method
# uses Ansible standard AWS options
- name: authenticate with aws_iam
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/hello:value', auth_method='aws_iam', role_id='myroleid', profile=my_boto_profile) }}"
# JWT auth
- name: Authenticate with a JWT
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/hola:val', auth_method='jwt', role_id='myroleid', jwt='myjwt', url='https://vault:8200') }}"
# Disabling Token Validation
# Use this when your token does not have the lookup-self capability. Usually this is applied to all tokens via the default policy.
# However you can choose to create tokens without applying the default policy, or you can modify your default policy not to include it.
# When disabled, your invalid or expired token will be indistinguishable from insufficent permissions.
- name: authenticate without token validation
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/hello:value', token=my_token, token_validate=False) }}"
# "none" auth method does no authentication and does not send a token to the Vault address.
# One example of where this could be used is with a Vault agent where the agent will handle authentication to Vault.
# https://www.vaultproject.io/docs/agent
- name: authenticate with vault agent
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/hello:value', auth_method='none', url='http://127.0.0.1:8100') }}"
# Use a proxy
- name: use a proxy with login/password
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=... token=... url=https://... proxies=https://user:pass@myproxy:8080') }}"
- name: 'use a socks proxy (need some additional dependencies, see: https://requests.pythonlang.cn/en/master/user/advanced/#socks )'
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret=... token=... url=https://... proxies=socks5://myproxy:1080') }}"
- name: use proxies with a dict (as param)
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', '...', proxies={'http': 'http://myproxy1', 'https': 'http://myproxy2'}) }}"
- name: use proxies with a dict (as param, pre-defined var)
  vars:
    prox:
      http: http://myproxy1
      https: https://myproxy2
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', '...', proxies=prox }}"
- name: use proxies with a dict (as direct ansible var)
  vars:
    ansible_hashi_vault_proxies:
      http: http://myproxy1
      https: https://myproxy2
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', '...' }}"
- name: use proxies with a dict (in the term string, JSON syntax)
  ansible.builtin.debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', '... proxies={\"http\":\"http://myproxy1\",\"https\":\"http://myproxy2\"}') }}"
- name: use ansible vars to supply some options
  vars:
    ansible_hashi_vault_url: 'https://myvault:8282'
    ansible_hashi_vault_auth_method: token
  set_fact:
    secret1: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/secret1') }}"
    secret2: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/secret2') }}"
- name: use a custom timeout
  debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/secret1', timeout=120) }}"
- name: use a custom timeout and retry on failure 3 times (with collection retry defaults)
  vars:
    ansible_hashi_vault_timeout: 5
    ansible_hashi_vault_retries: 3
  debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/secret1') }}"
- name: retry on failure (with custom retry settings and no warnings)
  vars:
    ansible_hashi_vault_retries:
      total: 6
      backoff_factor: 0.9
      status_forcelist: [500, 502]
      allowed_methods:
        - GET
        - PUT
  debug:
    msg: "{{ lookup('community.hashi_vault.hashi_vault', 'secret/data/secret1', retry_action='warn') }}"
返回值
| 键 | 描述 | 
|---|---|
| 请求的密钥(s) 返回:成功 | 
