简而言之,您需要将具有正确扩展名的GPG密钥放入一个默认不被搜索的单独目录,并将您的存储库配置指向该特定文件。
有关为什么需要一个单独目录的更多信息,请参阅
此答案中对“警告:apt-key已弃用。请在trusted.gpg.d中管理密钥环文件”。
警告:apt
不会接受以.gpg扩展名保存的ASCII GPG密钥。
您可以通过
file
命令验证您是否具有旧的ASCII GPG格式(.asc)还是较新的二进制GPG格式(.gpg)。
elastic-old.gpg: PGP public key block Public-Key (old)
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
或
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格式。
如果您想/需要使用较新的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
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 }}
apt_key
现在在内部使用gpg
。你的 Ansible 版本是多少?如果是旧版本,你考虑过升级 Ansible 吗? - β.εηοιτ.βεapt_key
并不意味着它使用二进制文件apt-key
。实际上,大多数 Ansible 模块并不使用任何系统 OS 命令,而是使用 Python 工具,因为 Ansible 全部基于 Python。参见:Ansible 通过连接到节点并向其推送名为“Ansible 模块”的小程序来工作。这些程序编写成所需系统状态的资源模型。然后,Ansible 执行这些模块(默认情况下通过 SSH),并在完成时将它们删除。。 - β.εηοιτ.βεapt_key
不再适用... - lorenzo-bettini