ansible.windows.win_certificate_store 模块 – 管理证书存储

注意

此模块是 ansible.windows 集合 (版本 2.5.0) 的一部分。

如果您使用的是 ansible 软件包,则可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装它,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install ansible.windows

要在剧本中使用它,请指定:ansible.windows.win_certificate_store

概要

  • 用于导入/导出和删除本地证书存储中的证书和密钥。

  • 此模块不用于创建证书,仅管理作为文件或存储在存储区中的现有证书。

  • 它可以用于导入 PEM、DER、P7B、PKCS12 (PFX) 证书并导出 PEM、DER 和 PKCS12 证书。

参数

参数

注释

file_type

字符串

state=exported 时,导出证书的文件类型。

der 是一个二进制 ASN.1 编码文件。

pem 是 OpenSSL 格式的 der 文件的 base64 编码文件。

pkcs12 (也称为 pfx) 是一个二进制容器,与其他选项不同,它同时包含证书和私钥。

当设置 pkcs12 且私钥不可导出或当前用户无法访问时,它将引发异常。

选项

  • "der" ← (默认)

  • "pem"

  • "pkcs12"

key_exportable

布尔值

是否允许导出私钥。

如果为 false,则此模块和其他进程只能导出证书,而私钥不能导出。

仅当 state=present 时使用。

选项

  • false

  • true ← (默认)

key_storage

字符串

指定 Windows 导入私钥时存储私钥的位置。

设置为 default 时,使用 Windows 设置的默认选项,通常为 user

设置为 machine 时,密钥存储在各种用户都可以访问的路径中。

设置为 user 时,密钥存储在当前用户才能访问的路径中。

仅当 state=present 时使用,导入后无法更改。

有关详细信息,请参阅 https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509keystorageflags.aspx

选项

  • "default" ← (默认)

  • "machine"

  • "user"

password

字符串

pkcs12 证书密钥的密码。

这在读取 pkcs12 证书文件或设置密码时使用,其中 state=exportedfile_type=pkcs12

如果 pkcs12 文件未设置密码或导出文件不应设置密码,则不要设置此选项。

path

path

证书文件的路径。

当 *state* 为 presentexported 时,这是必需的。

当 *state* 为 absent 且未指定 *thumbprint* 时,将从此路径的证书中派生指纹。

state

字符串

如果为 present,将确保 *path* 中的证书导入到指定的证书存储中。

如果为 absent,将确保通过 *thumbprint* 或 *path* 中证书的指纹指定的证书从存储中删除。

如果为 exported,将确保 *path* 中的文件是 *thumbprint* 指定的证书。

导出证书时,如果 *path* 是目录,则模块将失败,否则将根据需要替换文件。

选项

  • "absent"

  • "exported"

  • "present" ← (默认)

store_location

字符串

导入证书或搜索证书时使用的存储位置。

store_type=system 时,可以设置为 CurrentUserLocalMachine

store_type=system 时,默认为 LocalMachine

store_type=service 时,必须设置为任何服务名称。

默认值: "LocalMachine"

store_name

字符串

导入证书或搜索证书时使用的存储名称。

AddressBook:其他用户的 X.509 证书存储

AuthRoot:第三方证书颁发机构 (CA) 的 X.509 证书存储区

CertificateAuthority:中间证书颁发机构 (CA) 的 X.509 证书存储区

Disallowed:已吊销证书的 X.509 证书存储区

My:个人证书的 X.509 证书存储区

Root:受信任的根证书颁发机构 (CA) 的 X.509 证书存储区

TrustedPeople:直接受信任的人员和资源的 X.509 证书存储区

TrustedPublisher:直接受信任的发布者的 X.509 证书存储区

默认值: "My"

store_type

字符串

在 ansible.windows 1.5.0 中添加

要管理的存储类型。

使用 system 管理系统存储区中的位置,LocalMachineCurrentUser

使用 service 管理由 *store_location* 指定的服务帐户的存储区。

选项

  • "system" ← (默认)

  • "service"

thumbprint

字符串

作为十六进制字符串的指纹,用于导出或删除。

请参阅示例,了解如何指定指纹。

注释

注意

  • 对 PKCS12 证书和密钥执行某些操作可能会失败,并出现错误 the specified network password is not correct,请使用 CredSSP 或 Kerberos 和凭据委派,或者使用 become 来绕过这些限制。

  • 证书必须位于 Windows 主机上才能使用 *path* 设置。

  • 在导入用于 IIS 的证书时,通常需要使用 machine key_storage 选项,因为 defaultuser 都将使 IIS APPPOOL 标识无法读取私钥,并阻止将证书绑定到 https 端点。

示例

- name: Import a certificate
  ansible.windows.win_certificate_store:
    path: C:\Temp\cert.pem
    state: present

- name: Import pfx certificate that is password protected
  ansible.windows.win_certificate_store:
    path: C:\Temp\cert.pfx
    state: present
    password: VeryStrongPasswordHere!
  become: true
  become_method: runas

- name: Import pfx certificate without password and set private key as un-exportable
  ansible.windows.win_certificate_store:
    path: C:\Temp\cert.pfx
    state: present
    key_exportable: false
  # usually you don't set this here but it is for illustrative purposes
  vars:
    ansible_winrm_transport: credssp

- name: Remove a certificate based on file thumbprint
  ansible.windows.win_certificate_store:
    path: C:\Temp\cert.pem
    state: absent

- name: Remove a certificate based on thumbprint
  ansible.windows.win_certificate_store:
    thumbprint: BD7AF104CF1872BDB518D95C9534EA941665FD27
    state: absent

- name: Remove certificate based on thumbprint is CurrentUser/TrustedPublishers store
  ansible.windows.win_certificate_store:
    thumbprint: BD7AF104CF1872BDB518D95C9534EA941665FD27
    state: absent
    store_location: CurrentUser
    store_name: TrustedPublisher

- name: Export certificate as der encoded file
  ansible.windows.win_certificate_store:
    path: C:\Temp\cert.cer
    state: exported
    file_type: der

- name: Export certificate and key as pfx encoded file
  ansible.windows.win_certificate_store:
    path: C:\Temp\cert.pfx
    state: exported
    file_type: pkcs12
    password: AnotherStrongPass!
  become: true
  become_method: runas
  become_user: SYSTEM

- name: Import certificate be used by IIS
  ansible.windows.win_certificate_store:
    path: C:\Temp\cert.pfx
    file_type: pkcs12
    password: StrongPassword!
    store_location: LocalMachine
    key_storage: machine
    state: present
  become: true
  become_method: runas
  become_user: SYSTEM

- name: Import certificate to be used for LDAPS
  ansible.windows.win_certificate_store:
    path: C:\Temp\cert.pfx
    password: StrongPassword!
    store_type: service
    store_location: NTDS
    key_exportable: false
    key_storage: machine
    state: present

返回值

常用的返回值已在 此处 记录,以下是此模块特有的字段

描述

thumbprints

列表 / 元素=字符串

模块触及的证书指纹列表。

返回:成功

示例: ["BC05633694E675449136679A658281F17A191087"]

作者

  • Jordan Borean (@jborean93)