curl命令与管道不工作在Ansible



我正在尝试执行以下命令,这是Docker安装的一部分,但它卡住了。

命令的gpg部分卡住了,如果我在管道后删除gpg,它就可以工作了。

---
- hosts: all
become: yes
tasks:
- name: add docker GPG key
shell: "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg"

General Ansible advice:如果你只是在Ansible的shell任务中输入所有命令行,那么你做错了。
Ansible确实有现有的模块,这些模块旨在服务于Ansible目标的根源,这将大大简化您将尝试实现的所有任务。


话虽这么说,你现在必须明白Docker手册的这一行是想要实现什么。

curl -fsSL https://download.docker.com/linux/ubuntu/gpg  
| gpg --dearmor -o /etc/apt/keyrings/docker.gpg

此命令将Docker的GPG密钥添加到节点上的可信密匙环中,因此它可以验证稍后将在package任务中使用的包的真实性。

因此,在本例中,目的模块是apt_key模块。

你的任务最终是:

- name: add docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg

apt示例

要通过HTTPS下载文件到您的节点,您可以使用get_url_module,然后使用apt_key_module任务来添加密钥。

- name: Download apt key
get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /tmp # or /etc/pki/rpm-gpg depending on the infrastructure
- name: Add a key from a file
ansible.builtin.apt_key:
file: /tmp/gpg
state: present

也可以加上

- name: Add an Apt signing key, uses whichever key is at the URL
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

您可能需要为gpgkeyring使用其他模块或任务。

类似Q&

  • 下载并添加密钥

今天遇到了同样的问题,因为我不想使用apt_key模块,因为apt-key命令,该模块在引子下使用,已弃用。我和你走的是同一条路。

正如@Zeitounator提到的,这个问题是由于gpg在交互模式下运行并等待确认而引起的,我确信这是因为目标文件已经存在(可能是因为您之前运行了任务),所以它要求您覆盖该文件。因此,在这种情况下的解决方案是使用shell模块中的creates选项,该选项指向存储gpg密钥的路径。否则,如果文件存在,任务将不会再次运行。看到https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html参数创建

- name: add docker GPG key
shell: |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg |
gpg --dearmor -o /etc/apt/keyrings/docker.gpg
creates: /etc/apt/keyrings/docker.gpg

apt_key已弃用。一般情况下,可行的例子"- name:一种避免apt_key的方法,一旦它从你的发行版中删除…"建议使用ansible.builtin.get_urlansible.builtin.apt_repository的组合。

还需要注意的是,这个示例表明,"铠装键"应该使用"。asc"扩展名,"二进制"应该使用"。gpg"扩展名。虽然Docker Ubuntu的安装说明提到了docker.gpg,但我使用了docker.asc,因为Docker的安装说明暗示了密钥是装甲的(也就是说,他们需要运行gpg --dearmor)。

- name: install Docker | Add Docker’s official GPG key
become: yes
block:
- name: docker | add apt key
ansible.builtin.get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /etc/apt/keyrings/docker.asc
- name: docker | add apt source
ansible.builtin.apt_repository:
repo: deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
state: present

另请参见我如何管理可信.gpg.d中的密钥环文件,因为apt-key已弃用?

最新更新