由于 apt-key 已弃用,如何使用 ansible playbook 管理 trust.gpg.d 中的密钥环文件?

ece*_*naz 31 apt gnupg ansible gpg-signature apt-key

在被弃用之前apt-key,我使用 Ansible playbook 在我的服务器中添加和更新密钥。目前,apt-key不再更新密钥。在几次搜索中,我发现我现在需要使用gpg。但是,我有很多服务器,我不想为每台服务器手动执行此操作。有没有办法用gpgAnsible 管理我的钥匙圈?

以下是我的 Ansible 任务,已弃用apt-key

- apt_key:
  url: "https://packages.treasuredata.com/GPG-KEY-td-agent"
  state: present

- apt_repository:
  repo: "deb http://packages.treasuredata.com/3/ubuntu/{{ ansible_distribution_release }}/ {{ ansible_distribution_release }} contrib"
  state: present
  filename: "treasure-data" # Name of the pre-compiled fluentd-agent
Run Code Online (Sandbox Code Playgroud)

我尝试过apt-key update,但它对我不起作用。如果密钥已存在但已过期,则不再更新它。

C0r*_*n3j 17

简而言之,您需要将具有正确扩展名的 GPG 密钥放入默认情况下不搜索的单独目录中,并将存储库配置指向该特定文件。

有关为什么需要单独目录的更多信息,请参阅“警告:apt-key 已弃用。改为在 trust.gpg.d 中管理密钥环文件”的答案

警告:apt不接受以 .gpg 扩展名保存的 ASCII GPG 密钥。

您可以通过以下方式验证您是否拥有旧的 ASCII GPG 格式(.asc)或较新的二进制 GPG 格式(.gpg)file

# file elastic-old.gpg
elastic-old.gpg: PGP public key block Public-Key (old)

# file elastic.gpg    
elastic.gpg: PGP/GPG key public ring (v4) created Mon Sep 16 17:07:54 2013 RSA (Encrypt or Sign) 2048 bits MPI=0xd70ed6cd267c5b3e...
Run Code Online (Sandbox Code Playgroud)

如果您的密钥是旧格式,则可以使用 .asc 扩展名,或者您可以选择将其解除保护gpg --dearmor elastic.gpg为新的二进制格式并使用 .gpg 扩展名。

去装甲步骤对于 ansible 自动化来说很烦人,所以我建议你只使用上游提供的任何格式。

在 Ubuntu 22.04 上,您需要使用一个未预加载的文件夹 -/etc/apt/keyrings或者您可以创建自己的目录并使用它。

至于 Ansible 部分,您可以使用get_urlfile将 GPG 密钥推送到系统上,然后apt_repository像以前一样使用添加存储库,并指定密钥环。

将二进制 GPG 格式与 .gpg 一起使用

- name: Add Example GPG key
  ansible.builtin.get_url:
    url: https://example.com/example.gpg
    dest: /etc/apt/keyrings/example.gpg
    mode: '0644'
    force: true
Run Code Online (Sandbox Code Playgroud)

或者如果上游仍未切换,则使用 .asc 扩展名

- name: Add Example GPG key
  ansible.builtin.get_url:
    url: https://example.com/example.gpg
    dest: /etc/apt/keyrings/example.asc
    mode: '0644'
    force: true
Run Code Online (Sandbox Code Playgroud)

然后你可以像以前一样通过 apt_repository 模块定义你的存储库。

- name: Add Example repo
  ansible.builtin.apt_repository:
    filename: example-repo
    repo: 'deb [signed-by=/etc/apt/keyrings/example.gpg] https://example.com/packages/8.x/apt stable main'
Run Code Online (Sandbox Code Playgroud)

请记住,apt_repository 使用旧.list格式而不是新的 DEB822 兼容.sources格式。

如果您想要/需要使用较新的 DEB822 格式并且您碰巧正在运行 Ansible 2.15 或更高版本,则应该使用deb822_repository 模块

如果您无法使用较旧的 Ansible 核心,您可以自行对其进行模板化,类似于:

tasks/main.yaml

- name: Add Elastic repo
  notify: apt update force
  ansible.builtin.template:
    src: repo.j2
    dest: /etc/apt/sources.list.d/elastic-8.x.sources
    mode: '0644'
  vars:
    repo_name: Example PPA
    repo_uris: https://example.com/packages/8.x/apt
    repo_suites: stable
    repo_components: main
    repo_signed_by: /etc/apt/keyrings/example.gpg
Run Code Online (Sandbox Code Playgroud)

templates/repo.j2

X-Repolib-Name: {{ repo_name }}
Types: deb
URIs: {{ repo_uris }}
Suites: {{ repo_suites }}
{% if repo_components is defined %}
Components: {{ repo_components }}
{% endif %}
Signed-By: {{ repo_signed_by }}
Run Code Online (Sandbox Code Playgroud)

  • 正如[此问题评论](https://github.com/ansible/ansible/issues/78063#issuecomment-1210837515)中所建议的,您可以使用“.asc”扩展名(而不是“. gpg`) 使其无需额外的亲爱的步骤即可工作。 (6认同)
  • 在使用 Ansible 将服务器文件 (https://example.com/example.gpg) 保存到本地文件夹 (/etc/apt/keyrings/example.gpg) 之前,有没有办法先将其删除? (5认同)
  • 提示:如果您自己以 root 身份创建 `/etc/apt/keyrings` 目录(可能是因为您的操作系统未附带该目录),请记住授予它“o+x”权限,否则它将无法工作。 (2认同)

Joe*_*Joe 10

为了扩展 @geerlingguy 关于使用.asc扩展的评论,这就是我最终添加Telegraf存储库的方式。请注意和任务influxdb.asc中的使用。get_urlapt_repository

- name: Install InfluxDB key
  get_url:
    url:  https://repos.influxdata.com/influxdb.key
    dest: /etc/apt/trusted.gpg.d/influxdb.asc

- name:  Add InfluxDB repository
  apt_repository:
    repo:  "deb [signed-by=/etc/apt/trusted.gpg.d/influxdb.asc] https://repos.influxdata.com/debian stable main"
    state: present
    update_cache: yes

- name:  Install telegraf
  package:
    name:  telegraf
    state: present
Run Code Online (Sandbox Code Playgroud)

用这个方法就可以完全绕过这个gpg --dearmor步骤。

  • “[证书不得放置在`/etc/apt/trusted.gpg.d`](https://wiki.debian.org/DebianRepository/UseThirdParty)” (6认同)