简而言之,您需要将具有正确扩展名的 GPG 密钥放入默认情况下不搜索的单独目录中,并将存储库配置指向该特定文件。
有关为什么需要单独目录的更多信息,请参阅这是对“警告:apt-key 已弃用。改为管理 trust.gpg.d 中的密钥环文件”的答案 https://stackoverflow.com/a/71384057.
警告: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...
如果您的密钥是旧格式,您可以使用 .asc 扩展名,或者您可以选择通过以下方式解除它的保护gpg --dearmor elastic.gpg
转换为新的二进制格式并使用 .gpg 扩展名。
去装甲步骤对于 ansible 自动化来说很烦人,所以我建议你只使用上游提供的任何格式。
在 Ubuntu 22.04 上,您需要使用一个未预加载的文件夹 -/etc/apt/keyrings
- 或者您可以创建自己的目录并使用它。
至于Ansible部分,你可以使用get_url
or file
将 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
或者如果上游仍未切换,则使用 .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
然后你可以像以前一样通过 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'
请记住 apt_repository 使用旧的.list
格式而不是新的 DEB822 兼容格式.sources
format.
如果您想要/需要使用较新的 DEB822 格式并且您碰巧正在运行 Ansible 2.15 或更高版本,则应该使用deb822_repository 模块 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/deb822_repository_module.html.
如果您无法使用较旧的 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
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 }}