Ansible 抛出“无法更新 apt 缓存:W:无法安全地从此类存储库更新”错误

MrS*_*ver 3 ansible kubernetes

我对 Ansible 非常陌生,我正在尝试在 EC2 实例(Ubuntu 18.04)上安装 kubectl 来上课。

我已经运行了剧本,一切进展顺利,直到遇到任务 4,然后抛出以下错误:

致命:[localhost]:失败!=> {"changed": false, "msg": "无法更新 apt 缓存: W:从这样的存储库更新无法安全地完成,因此默认情况下被禁用。, W:参见 apt-secure(8 )用于存储库创建和用户配置详细信息的联机帮助页。,W:GPG 错误:https://packages.cloud.google.com/apt kubernetes-xenial InRelease:无法验证以下签名,因为公钥不可用: NO_PUBKEY FEEA9169307EA071 NO_PUBKEY 8B57C5C2836F4BEB,E:存储库“https://apt.kubernetes.io kubernetes-xenial InRelease”未签名。”}

现在,每当我尝试再次运行任务 1 时,它都会引发相同的错误。有人可以建议我如何解决这个问题吗?

这是我写的剧本,它基于我完成的使用 Ansible 安装 Docker 的练习以及为我安装 kubectl 提供的命令:

- name: A playbook to install kubectl on a VM
  hosts: localhost
  user: ubuntu
  become: yes

  tasks:
  - name: 1. Update APT Package Manager
    apt:
      update_cache: yes

  - name: 2. Install dependency packages
    apt:
      name={{ item }}
    with_items:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg-agent
      - software-properties-common

  - name: 3. Get APT Key
    shell:
      cmd: curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      cmd: echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list

  - name: 4. Update Packages
    apt:
      update_cache: yes

  - name: 5. Install Kubectl
    apt:
      update_cache: yes
      name: kubectl

Run Code Online (Sandbox Code Playgroud)

U88*_*80D 5

关于部分

- name: 3. Get APT Key
    shell:
      cmd: curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
      cmd: echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
Run Code Online (Sandbox Code Playgroud)

_moduleshell将只执行第二个cmd。Ansible 只能将其中一个参数传递给模块,即最后一个。

要将文件从 HTTPS 下载到节点,您可以使用get_url_module,然后使用apt_key_module 任务来添加 apt key

- name: Download apt key
  get_url:
    url: https://packages.cloud.google.com/apt/doc/apt-key.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/apt-key.gpg
    state: present
Run Code Online (Sandbox Code Playgroud)

您也可以通过以下方式添加

- name: Add an Apt signing key, uses whichever key is at the URL
  ansible.builtin.apt_key:
    url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
    state: present
Run Code Online (Sandbox Code Playgroud)

或者如果您已经知道密钥 ID

- name: Add missing Apt signing key by ID from a keyserver
  ansible.builtin.apt_key:
   keyserver: keyserver.ubuntu.com
   id: "{{ KEY_ID }}"
Run Code Online (Sandbox Code Playgroud)