带有管道的curl命令在Ansible中不起作用

问题描述 投票:0回答:4

我尝试执行以下命令,这是 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"
docker curl ansible ansible-2.x gpg-signature
4个回答
4
投票

Ansible 一般建议: 如果您只是在 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

3
投票

示例

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
您可能需要使用其他模块或任务来实现 

gpg

keyring

类似问答

  • 下载并添加密钥

1
投票
今天遇到了同样的问题,因为我不想使用

apt_key

 模块,因为模块在后台使用的 
apt-key
 命令已被弃用。我和你遵循同样的方法。

正如@Zeitounator提到的,这个问题是因为gpg正在交互模式下运行并等待确认而引起的,我确信这是因为目标文件已经存在(可能是因为您之前运行了任务),所以它会问您覆盖该文件。 因此,这种情况的解决方案是使用

creates

 模块中的 
shell
 选项指向存储 gpg 密钥的路径。如果文件存在,任务将不会再次运行。请参阅
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#parameter-creates

- 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
    

1
投票

apt_key 已被弃用。。对于一般的解决方法, Ansible 示例“- name:从发行版中删除后避免 apt_key 的一种方法...”建议使用 ansible.builtin.get_url

ansible.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
另请参阅

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

© www.soinside.com 2019 - 2024. All rights reserved.