ansible - 在ansible中将ssl证书从服务器复制到主机

标签 ansible ssl-certificate ansible-galaxy

我正在尝试将本地容器注册表的证书复制到 ansible 版本 2.9.6 的 Docker 主机。注册表由我们本地网络内的其他人管理。

使用 shell,我会这样做:

openssl s_client -showcerts -connect registry.example:443 < /dev/null 2> /dev/null | openssl x509 -outform PEM > ca.crt

到目前为止,我围绕 ansible 的 community.crypto 模块进行了工作,并成功获得了证书:

- name: get certificate from registry
  community.crypto.get_certificate:
    host: "{{ registry_url }}"
    port: "{{ registry_port }}"
  delegate_to: localhost
  become: no
  register: cert

这与 shell 替代方案的前半部分类似。我还没有弄清楚如何完成后半部分的工作,即使用从服务器接收到的内容创建证书。

我尝试使用 community.crypto.x509_certificate,但无法使其表现得像以下 shell 示例中的 openssl_client 那样。

openssl x509 -outform PEM -in server_content_file -text -out ca.crt

有没有办法使用community.crypto模块或使用ansible以任何其他方式来做到这一点?

最佳答案

您还没有追求的一个选项是在 Ansible 中运行 openssl 命令。以下示例假设您已将 remotehost 变量设置为registry.example:

- hosts: localhost
  gather_facts: false
  tasks:
    - name: get remote certificate
      command: openssl s_client -showcerts -connect {{ remotehost }}
      register: remotecert

    - name: extract certificate
      command: openssl x509 -outform PEM
      args:
        stdin: "{{ remotecert.stdout }}"
      register: remotex509

    - name: write ca.crt
      copy:
        dest: ./ca.crt
        content: "{{ remotex509.stdout }}"

虽然粒度不太细,但您当然可以将所有内容组合起来 到单个 shell 脚本中:

- hosts: localhost
  gather_facts: false
  tasks:
    - name: get remote certificate
      shell: >-
        openssl s_client -showcerts -connect {{ remotehost }} |
        openssl x509 -outform PEM > ca.crt
      args:
        creates: ca.crt
      register: remotecert

creates 参数将导致任务被跳过,如果目标 文件 (ca.crt) 已存在。

关于ansible - 在ansible中将ssl证书从服务器复制到主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68440047/

相关文章:

centos - 如何使这个 ansible chkconfig 任务幂等?

ansible - 在完成一台主机而非所有 fork 主机后,为下一台主机执行 ansible 剧本

random - 如何创建随机字符串并在 Ansible 的 jinja 中使用它

python - 如何使用 python 连接到 Cosmos DB Emulator 表存储?

java - 使用 Java 套接字连接到未签名的 HTTPs 站点

ansible - 如何自动安装 Ansible Galaxy 角色?

Ansible 不允许剧本级别的环境

python-3.x - ubuntu、python、ssl.SSLError : [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl. c:852)

mysql - ansible galaxy - 从 repo 安装角色

git - 来自 Bitbucket.org 的 ansible-galaxy 和 git clone