在
apt-key
被弃用之前,我使用 Ansible playbook 在我的服务器中添加和更新密钥。目前,apt-key
不再更新密钥。在几次搜索中,我发现我现在需要使用gpg
。但是,我有很多服务器,我不想为每台服务器手动执行此操作。有没有办法通过 Ansible 来管理我的钥匙圈?这是我的 Ansible 任务,已弃用 gpg
:
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
,但它对我不起作用。如果密钥已存在但已过期,则不再更新它。
有关为什么需要单独文件夹的更多信息,请参阅
“警告:apt-key 已弃用。改为在 trust.gpg.d 中管理密钥环文件”的答案警告:apt-key update
不接受以 .gpg 扩展名保存的 ASCII GPG 密钥。
apt
验证您是否拥有旧的 ASCII GPG 格式(.asc)或较新的二进制 GPG 格式(.gpg):
file
如果您的密钥是旧格式,您可以使用 .asc 扩展名,或者您可以选择通过
# 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...
将其解除武装为新的二进制格式并使用 .gpg 扩展名。
解除装甲步骤对于 ansible 自动化来说很烦人,所以我建议您按原样使用上游提供的任何格式。在 Ubuntu 22.04 上,您需要使用一个未预加载的文件夹 -
gpg --dearmor elastic.gpg
- 或者您可以创建自己的目录并使用它。
对于 Ansible 部分,您可以使用/etc/apt/keyrings
或
get_url
将 GPG 密钥推送到系统上,然后像之前一样使用 file
添加存储库,并添加指定密钥环。apt_repository
如果上游仍未切换,则使用 .asc 扩展名。
- 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
- 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 仍然使用旧的
- 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
格式,而不是新的符合 DEB822 的
.list
格式。如果您想要/需要使用较新的 DEB822 格式,您可以按照如下方式自行模板化。
.sources
tasks/repo.j2
- 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 }}
扩展的评论,这就是我最终为
Telegraf添加存储库的方式。请注意在
.asc
和 influxdb.asc
任务中使用 get_url
。apt_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
步骤。
提供 3 个变量,然后您就可以开始运行了。我插入了 sm- 前缀,这样我就可以清楚地看到我的脚本将它们放在那里,而不是任何其他进程。
gpg --dearmor