在类似 Debian 的系统中安装 Docker 时,需要执行以下三个主要步骤:

  1. 添加 Docker 的 GPG 密钥 使用 ansible.builtin.apt_key 模块下载仓库的 GPG 签名密钥。这个加密密钥用于验证软件包和仓库的真实性,确保软件与 Docker 发布的版本一致。
  2. 添加 Docker 仓库 使用 ansible.builtin.apt_repository 模块将 Docker 仓库添加到系统中。这是一个额外的软件源,apt(你的系统包管理器)会从这里查找软件。
  3. 更新 apt 缓存并安装 Docker 使用 ansible.builtin.apt 模块更新可用软件包的缓存,并安装 Docker(docker-ce)。update_cache 参数会在安装前更新 apt 缓存,以确保下载最新版本的软件包。

参数说明

ansible.builtin.apt_key

  • url(字符串):GPG 密钥的 URL。
  • state(字符串):present/absent,确保密钥在系统中存在。

ansible.builtin.apt_repository

  • repo(字符串):仓库的参数。
  • state(字符串):present/absent,确保仓库在系统中存在。
  • filename(字符串):在 /etc/apt/sources.list.d/ 中创建的文件名。

ansible.builtin.apt

  • name(字符串):软件包名称(如 docker-ce)。
  • state(字符串):latest/present/absent,确保软件包在系统中存在并为最新版本。
  • update_cache(布尔值):是否在安装前更新 apt 缓存。

示例代码

以下是一个完整的 Ansible Playbook,用于在类似 Debian 的系统中安装 Docker:

---
- name: install Docker
  hosts: all
  become: true
  tasks:
    - name: Install apt-transport-https and other dependencies
      ansible.builtin.apt:
        name:
          - apt-transport-https
          - ca-certificates
          - lsb-release
          - gnupg
        state: latest
        update_cache: true

    - name: Add Docker signing key
      ansible.builtin.apt_key:
        url: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg"
        state: present

    - name: Add Docker repository
      ansible.builtin.apt_repository:
        repo: "deb [arch={{ ansible_architecture }}] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable"
        state: present
        filename: docker

    - name: Install Docker
      ansible.builtin.apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
        state: latest
        update_cache: true

执行过程

执行命令

ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu/inventory install\ Docker/debian.yml

输出结果

PLAY [install Docker] ******************************************************
TASK [Gathering Facts] ******************************************************
ok: [ubuntu.example.com]
TASK [Install apt-transport-https] ******************************************
changed: [ubuntu.example.com]
TASK [Add Docker signing key] ***********************************************
changed: [ubuntu.example.com]
TASK [Add Docker repository] ***********************************************
changed: [ubuntu.example.com]
TASK [Install Docker] ******************************************************
changed: [ubuntu.example.com]
PLAY RECAP ******************************************************************
ubuntu.example.com         : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

幂等性测试

再次运行 Playbook 时,不会重复执行已成功完成的任务:

ansible-pilot $ ansible-playbook -i virtualmachines/ubuntu/inventory install\ Docker/debian.yml

输出结果:

PLAY [install Docker] ******************************************************
TASK [Gathering Facts] ******************************************************
ok: [ubuntu.example.com]
TASK [Install apt-transport-https] ******************************************
ok: [ubuntu.example.com]
TASK [Add Docker signing key] ***********************************************
ok: [ubuntu.example.com]
TASK [Add Docker repository] ***********************************************
ok: [ubuntu.example.com]
TASK [Install Docker] ******************************************************
ok: [ubuntu.example.com]
PLAY RECAP ******************************************************************
ubuntu.example.com         : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前

在执行 Playbook 之前,目标主机上未安装 Docker:

$ ssh devops@ubuntu.example.com
$ lsb_release -a
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal
$ docker --version
-sh: 2: docker: not found
$ apt list installed docker-ce -a
Listing... Done

执行后

执行 Playbook 后,Docker 已成功安装并可以正常运行:

$ ssh devops@ubuntu.example.com
$ sudo su
root@ubuntu:/home/devops# cat /etc/apt/sources.list.d/docker.list 
deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
root@ubuntu:/home/devops# docker --version
Docker version 20.10.12, build e91ed57
root@ubuntu:/home/devops# apt list installed docker-ce -a
Listing... Done
docker-ce/focal,now 5:20.10.12~3-0~ubuntu-focal amd64 [installed]
...
root@ubuntu:/home/devops# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
...

通过以上步骤,你可以使用 Ansible 在类似 Debian 的系统中自动化安装 Docker,并确保其正常运行。