Ansible: no package available for docker-ce

你离开我真会死。 提交于 2021-02-10 14:46:01

问题


I want to install Docker on a remote machine with Ubuntu 16.04, using Ansible and following the official docs at https://docs.docker.com/engine/installation/linux/ubuntu/. All seems to work, until ansible reaches the task with name "install Docker", I get "No package matching 'docker-ce' is available".

The following part of the playbook, from the point where the repository is set:

- name: set the stable repository
    apt_repository: 
      repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable
      

  - name: Update all packages to the latest version
    apt:
      upgrade: dist

  - name: install Docker
    apt:
      name: docker-ce
      state: present

What could be the problem?


回答1:


Or you can use generic OS package manager module if Ansible version >= 2.0:

- name: install docker
  package:
    name: docker-ce
    state: present



回答2:


The problem is that the apt repository is being added incorrectly. The following line is literally adding the $(lsb_release -cs) and is not interpolated:

repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable

What you want is to use an Ansible fact instead like this:

repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable

This should get you the following in the /etc/apt/sources.list.d/download_docker_com_repo.list file:

deb [arch=amd64] https://download.docker.com/linux/ubuntu trusty stable

Note: You will likely need apt_key to import the GPG key as well (per the install instructions).




回答3:


you didn't run apt-get update, so the new repo isn't being read




回答4:


I know you are searching for ubuntu installation, but for other users (as I searched before), I will share my centos version (running):

---
- name: Install docker
  gather_facts: true
  check_mode: no # yes only for test
  hosts: swarm_cluster
  vars:
    sudoers:
      - ansible
  become: true
  tasks:

    #################### ABSENT ########################
# systemctl status docker
    - name: Stop and disable docker service
      service:
        name: docker
        enabled: no
        state: stopped
      register: docker_service_result
      failed_when: "docker_service_result is failed and 'Could not find the requested service' not in docker_service_result.msg"

# grep /etc/group -e "docker"
    - name: Remove docker group
      group:
        name: docker
        state: absent
        system: yes

    - name: Remove 'docker', 'yum-utils', 'lvm2' and 'device-mapper-persistent-data' packages
      package:
        name: '{{ item }}'
        state: absent
      with_items:
        - docker
        - docker-client
        - docker-client-latest
        - docker-common
        - docker-latest
        - docker-latest-logrotate
        - docker-logrotate
        - docker-engine
        - docker-ce
        - docker-ce-cli
        - containerd.io
        - lvm2
        - device-mapper-persistent-data
        - yum-utils

# sudo vi /etc/yum.repos.d/docker-ce.repo
    - name: Remove docker repository
      yum_repository:
        name: docker-ce
        state: absent

#################### PRESENT ########################
    - name: Install 'yum-utils', 'lvm2' and 'device-mapper-persistent-data'
      package:
        name: '{{ item }}'
        state: latest
      with_items:
        - yum-utils
        - device-mapper-persistent-data
        - lvm2

    - name: Install docker repository
      yum_repository:
        name: '{{ item.name }}'
        description: '{{ item.desc }}'
        file: docker-ce
        baseurl: '{{ item.baseurl }}'
        enabled: '1'
        gpgcheck: yes
        gpgkey: 'https://download.docker.com/linux/centos/gpg'
        state: present
      with_items:
        - { name: 'docker-ce-stable', desc: 'Docker CE Stable - $basearch', baseurl: 'https://download.docker.com/linux/centos/7/$basearch/stable'}
        - { name: 'docker-ce-edge', desc: 'Docker CE Edge - $basearch', baseurl: 'https://download.docker.com/linux/centos/7/$basearch/edge'}
        - { name: 'docker-ce-test', desc: 'Docker CE Test - $basearch', baseurl: 'https://download.docker.com/linux/centos/7/$basearch/test'}

    - name: Install docker
      package:
        name: '{{ item }}'
        state: latest
      with_items:
        - docker-ce
        - docker-ce-cli
        - containerd.io

    - name: Start docker service
      service:
        name: docker
        state: started
        enabled: yes

    - name: Test Docker with hello world example
      shell: 'docker run hello-world'
      register: hello_world_output

    - name: Show output of hello word example
      debug:
        msg: 'Container output: {{ hello_world_output.stdout }}'

    - name: Create docker group
      group:
        name: docker
        state: present
        system: yes

    - name: Add ansible to docker group
      user:
        name: ansible
        groups: docker
        append: yes

    - name: Reboot server
      shell: 'sleep 1 && reboot'
      async: 1
      poll: 0




回答5:


you need to run

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" sudo apt-get update sudo apt-get install docker-ce



来源:https://stackoverflow.com/questions/43050128/ansible-no-package-available-for-docker-ce

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!