Question on using apt_repository with Ansible

Viewed 1344

I am receiving an error in my test environment (Ubuntu 18.04) where I want to upload a Docker repository through Ansible. Here is my code:

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

The error output I receive is as follows:

{"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."}

I am positive the error is stemming from $(lsb_release -cs) because when I replace that coding with bionic it works. Would this be the only way in moving forward? I would like whatever repository I install to know the version since my script will be covering Ubuntu 16.04, 18.04, and 20.04, and Debian.

1 Answers

You are right about the fact that the output of lsb_release -cs command is not getting translated to the release name.

To make sure the release name is automatically used, you can use the ansible_distribution_release ansible fact when gather_facts is enabled.

Like this:

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

The other way, if gather_facts is not enabled is to capture the output of lsb_release -cs command into variable and use it.

Something like this:

- 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"
Related