关于将 apt_repository 与 Ansible 一起使用的问题

Rom*_*oma 3 ansible

我在测试环境 (Ubuntu 18.04) 中收到错误,我想通过 Ansible 上传 Docker 存储库。这是我的代码:

- name: Add Docker Repository
  apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable
      state: present
Run Code Online (Sandbox Code Playgroud)

我收到的错误输出如下:

{"changed": false, "msg": "Failed to update apt cache: E:The repository 'https://download.docker.com/linux/ubuntu $(lsb_release Release' does not have a Release file."}
Run Code Online (Sandbox Code Playgroud)

我确信错误是源于此$(lsb_release -cs),因为当我用它替换该编码时,bionic它就可以工作。这是前进的唯一途径吗?我希望我安装的任何存储库都知道版本,因为我的脚本将涵盖 Ubuntu 16.04、18.04、20.04 和 Debian。

ses*_*i_c 7

lsb_release -cs您对命令的输出没有转换为发行版名称这一事实是正确的。

为了确保自动使用版本名称,您可以在启用ansible_distribution_release时使用 ansible 事实。gather_facts

像这样:

- name: Add Docker Repository
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
    state: "present"
Run Code Online (Sandbox Code Playgroud)

另一种方法,如果gather_facts未启用,是将命令的输出捕获lsb_release -cs到变量中并使用它。

像这样的东西:

- name: Get OS release name
  command: "lsb_release -cs"
  changed_when: false
  register: lsb_rel
- name: Add Docker Repository
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ lsb_rel.stdout }} stable"
    state: "present"
Run Code Online (Sandbox Code Playgroud)