community.general.java_cert 模块 – 使用 keytool 导入/移除 Java 密钥库 (cacerts) 中的证书

注意

此模块是 community.general 集合(版本 10.1.0)的一部分。

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

要安装它,请使用:ansible-galaxy collection install community.general。您需要进一步的要求才能使用此模块,请参阅 要求 以了解详细信息。

要在 playbook 中使用它,请指定:community.general.java_cert

概要

  • 这是一个围绕 keytool 的包装模块,可用于将证书和可选的私钥导入给定的 Java 密钥库,或将其从中删除。

要求

以下要求需要在执行此模块的主机上满足。

  • openssl

  • keytool

参数

参数

注释

attributes

别名: attr

字符串

在 community.general 8.5.0 中添加

结果文件系统对象应具有的属性。

要获取支持的标志,请查看目标系统上 chattr 的手册页。

此字符串应包含属性,顺序与 lsattr 显示的顺序相同。

= 运算符被假定为默认值,否则 +- 运算符需要包含在字符串中。

cert_alias

字符串

导入的证书别名。

在检查密钥库中是否存在证书时使用别名。

cert_content

字符串

在 community.general 8.6.0 中添加

用于创建密钥库的证书的内容。

加载证书需要 cert_urlcert_pathcert_contentpkcs12_path 中的一个。

cert_path

路径

要从中加载证书的本地路径。

加载证书需要 cert_urlcert_pathcert_contentpkcs12_path 中的一个。

cert_port

整数

连接到 URL 的端口。

这将用于创建服务器 URL:PORT。

默认值: 443

cert_url

字符串

从中获取 SSL 证书的基本 URL。

加载证书需要 cert_urlcert_pathcert_contentpkcs12_path 中的一个。

executable

字符串

如果未使用,则 keytool 二进制文件的路径,我们会在 PATH 中搜索它。

默认值: "keytool"

group

字符串

在 community.general 8.5.0 中添加

应该拥有文件系统对象的所有者的组的名称,就像提供给 chown 一样。

如果未指定,它将使用当前用户的当前组,除非您是 root 用户,在这种情况下,它可以保留以前的所有权。

keystore_create

布尔值

如果密钥库不存在,则创建密钥库。

选项

  • false ← (默认)

  • true

keystore_pass

字符串 / 必需

密钥库密码。

keystore_path

路径

密钥库的路径。

keystore_type

字符串

密钥库类型 (JCEKS, JKS)。

mode

任何

在 community.general 8.5.0 中添加

结果文件系统对象应具有的权限。

对于那些习惯于 /usr/bin/chmod 的人来说,请记住模式实际上是八进制数。您必须为 Ansible 提供足够的信息来正确解析它们。为了获得一致的结果,请引用八进制数(例如,'644''1777'),以便 Ansible 接收字符串并可以将其自身从字符串转换为数字。添加前导零(例如,0755)有时有效,但在循环和某些其他情况下可能会失败。

如果不遵循这些规则中的任何一个,则为 Ansible 提供数字将最终得到一个十进制数,这将产生意外的结果。

从 Ansible 1.8 开始,模式可以指定为符号模式(例如,u+rwxu=rw,g=r,o=r)。

如果未指定 mode 并且目标文件系统对象不存在,则在为新创建的文件系统对象设置模式时将使用系统上的默认 umask

如果未指定 mode 并且目标文件系统对象确实存在,则将使用现有文件系统对象的模式。

指定 mode 是确保使用正确权限创建文件系统对象的最佳方法。有关更多详细信息,请参阅 CVE-2020-1736。

owner

字符串

在 community.general 8.5.0 中添加

应拥有文件系统对象的用户的名称,就像提供给 chown 命令一样。

如果未指定,则使用当前用户,除非您是 root 用户,在这种情况下,它可以保留先前的所有权。

指定数字用户名将被视为用户 ID 而不是用户名。避免使用数字用户名以避免混淆。

pkcs12_alias

字符串

PKCS12 密钥库中的别名。

pkcs12_password

字符串

从 PKCS12 密钥库导入的密码。

pkcs12_path

路径

从本地加载 PKCS12 密钥库的路径。

cert_urlcert_pathcert_content 不同,PKCS12 密钥库嵌入了与证书匹配的私钥,用于将证书及其私钥都导入到 Java 密钥库中。

加载证书需要 cert_urlcert_pathcert_contentpkcs12_path 中的一个。

selevel

字符串

在 community.general 8.5.0 中添加

SELinux 文件系统对象上下文的级别部分。

这是 MLS/MCS 属性,有时称为 range

设置为 _default 时,如果可用,它将使用策略的 level 部分。

serole

字符串

在 community.general 8.5.0 中添加

SELinux 文件系统对象上下文的角色部分。

设置为 _default 时,如果可用,它将使用策略的 role 部分。

setype

字符串

在 community.general 8.5.0 中添加

SELinux 文件系统对象上下文的类型部分。

设置为 _default 时,如果可用,它将使用策略的 type 部分。

seuser

字符串

在 community.general 8.5.0 中添加

SELinux 文件系统对象上下文的用户部分。

默认情况下,它使用 system 策略(如果适用)。

设置为 _default 时,如果可用,它将使用策略的 user 部分。

state

字符串

定义可以是证书导入或删除的操作。

当存在 state 时,证书将始终幂等地插入到密钥库中,即使已经存在不同的证书别名。

选项

  • "absent"

  • "present" ← (默认)

trust_cacert

布尔值

在 community.general 0.2.0 中添加

信任导入的证书作为 CA 证书。

选项

  • false ← (默认)

  • true

unsafe_writes

布尔值

在 community.general 8.5.0 中添加

影响何时使用原子操作以防止数据损坏或从目标文件系统对象读取不一致。

默认情况下,此模块使用原子操作来防止数据损坏或从目标文件系统对象读取不一致,但有时系统配置不当或只是以阻止这种情况的方式损坏。一个例子是 Docker 挂载的文件系统对象,它不能从容器内部原子地更新,并且只能以不安全的方式写入。

此选项允许 Ansible 在原子操作失败时回退到不安全的文件系统对象更新方法(但是,它不会强制 Ansible 执行不安全的写入)。

重要!不安全的写入可能会受到竞争条件的影响,并可能导致数据损坏。

选项

  • false ← (默认)

  • true

属性

属性

支持

描述

check_mode

支持:完全

可以在 check_mode 中运行并返回更改状态预测,而无需修改目标。

diff_mode

支持:完全

当处于 diff 模式时,将返回有关已更改内容(或可能需要在 check_mode 中更改的内容)的详细信息。

示例

- name: Import SSL certificate from google.com to a given cacerts keystore
  community.general.java_cert:
    cert_url: google.com
    cert_port: 443
    keystore_path: /usr/lib/jvm/jre7/lib/security/cacerts
    keystore_pass: changeit
    state: present

- name: Remove certificate with given alias from a keystore
  community.general.java_cert:
    cert_url: google.com
    keystore_path: /usr/lib/jvm/jre7/lib/security/cacerts
    keystore_pass: changeit
    executable: /usr/lib/jvm/jre7/bin/keytool
    state: absent

- name: Import trusted CA from SSL certificate
  community.general.java_cert:
    cert_path: /opt/certs/rootca.crt
    keystore_path: /tmp/cacerts
    keystore_pass: changeit
    keystore_create: true
    state: present
    cert_alias: LE_RootCA
    trust_cacert: true

- name: Import trusted CA from the SSL certificate stored in the cert_content variable
  community.general.java_cert:
    cert_content: |
      -----BEGIN CERTIFICATE-----
      ...
      -----END CERTIFICATE-----
    keystore_path: /tmp/cacerts
    keystore_pass: changeit
    keystore_create: true
    state: present
    cert_alias: LE_RootCA
    trust_cacert: true

- name: Import SSL certificate from google.com to a keystore, create it if it doesn't exist
  community.general.java_cert:
    cert_url: google.com
    keystore_path: /tmp/cacerts
    keystore_pass: changeit
    keystore_create: true
    state: present

- name: Import a pkcs12 keystore with a specified alias, create it if it doesn't exist
  community.general.java_cert:
    pkcs12_path: "/tmp/importkeystore.p12"
    cert_alias: default
    keystore_path: /opt/wildfly/standalone/configuration/defaultkeystore.jks
    keystore_pass: changeit
    keystore_create: true
    state: present

- name: Import SSL certificate to JCEKS keystore
  community.general.java_cert:
    pkcs12_path: "/tmp/importkeystore.p12"
    pkcs12_alias: default
    pkcs12_password: somepass
    cert_alias: default
    keystore_path: /opt/someapp/security/keystore.jceks
    keystore_type: "JCEKS"
    keystore_pass: changeit
    keystore_create: true
    state: present

返回值

常见的返回值记录在此处,以下是此模块独有的字段

描述

cmd

字符串

执行完成操作的命令。

返回:成功

示例:"keytool -importcert -noprompt -keystore"

msg

字符串

执行给定命令后来自 keytool 命令的 stdout 输出。

返回:成功

示例:"模块 需要在 keystore_path '/tmp/test/cacerts' 处存在 密钥库"

rc

整数

Keytool 命令执行返回值。

返回:成功

示例:0

作者

  • Adam Hamsik (@haad)