首页
文章
标签
关于
ansible自动化安装docker
发布于: 2025-3-18   更新于: 2025-3-18   未收录
文章字数: 422   阅读时间: 2 分钟   阅读量:

ansible自动化安装docker.md

本地软件准备

支持linux/macOS上运行。

1. 环境依赖:

依赖 python3-pip,


sudo apt update
# 更新python版本
sudo apt install python3-pip -y

# 安装ansible
sudo apt install ansible

# 验证 pip 是否安装成功:
python3 -m pip --version
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

2. 脚本下载

快速安装docker

1.在hosts.yml中添加要部署的ssh登录信息/服务器内外网IP地址,比如:

1
2
3
4
5
6
7
8
9
dev: #这个命名在1中被引用
  hosts:
    192.168.50.60:
  vars:
    ansible_user: root
    ansible_port: 22
    ansible_ssh_private_key_file: ~/.ssh/id_ed25519
    INTRANET_IP: "192.168.50.60"
    INTERNET_IP: "192.168.50.60"

2.运行ansible,会同时安装docker/docker-compose等:

ansible-playbook -i ./hosts.yml ./install_docker.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# ansible-playbook -i ./hosts.yml ./install_docker.yml

PLAY [dev] ***************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.50.130 is using the discovered Python interpreter at /usr/bin/python3.12, but future installation of another Python interpreter could change the meaning of that
path. See https://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.50.130]

TASK [init_system : include_tasks] ***************************************************************************************************************************************************************************
skipping: [192.168.50.130]

TASK [init_system : include_tasks] ***************************************************************************************************************************************************************************
skipping: [192.168.50.130]

TASK [init_system : include_tasks] ***************************************************************************************************************************************************************************
included: /root/dev-test/ansible-docker/roles/init_system/tasks/docker.yml for 192.168.50.130

TASK [init_system : include_tasks] ***************************************************************************************************************************************************************************
skipping: [192.168.50.130]

TASK [init_system : include_tasks] ***************************************************************************************************************************************************************************
included: /root/dev-test/ansible-docker/roles/init_system/tasks/docker_ubuntu.yml for 192.168.50.130

TASK [init_system : 检查 SELinux 配置文件是否存在] ***********************************************************************************************************************************************************
ok: [192.168.50.130]

TASK [init_system : 配置文件禁用SELinux] *********************************************************************************************************************************************************************
skipping: [192.168.50.130]

TASK [init_system : 立即禁用SELinux] *************************************************************************************************************************************************************************
skipping: [192.168.50.130]

TASK [init_system : 安装一些依赖包] **************************************************************************************************************************************************************************
ok: [192.168.50.130] => (item=apt-transport-https)
ok: [192.168.50.130] => (item=ca-certificates)
ok: [192.168.50.130] => (item=curl)
ok: [192.168.50.130] => (item=gnupg-agent)
ok: [192.168.50.130] => (item=software-properties-common)

TASK [init_system : 添加Docker GPG key] **********************************************************************************************************************************************************************
ok: [192.168.50.130]

TASK [init_system : 添加Docker APT仓库] **********************************************************************************************************************************************************************
changed: [192.168.50.130]

TASK [init_system : 安装docker及containerd] ******************************************************************************************************************************************************************

changed: [192.168.50.130] => (item=containerd.io)

changed: [192.168.50.130] => (item=docker-ce-cli)

changed: [192.168.50.130] => (item=docker-ce)

TASK [init_system : 二进制安装docker-compose] ****************************************************************************************************************************************************************
changed: [192.168.50.130]

TASK [init_system : 验证docker版本] **************************************************************************************************************************************************************************
ok: [192.168.50.130]

TASK [init_system : 验证docker-compose版本] ******************************************************************************************************************************************************************
ok: [192.168.50.130]

TASK [init_system : Docker version] **************************************************************************************************************************************************************************
ok: [192.168.50.130] => {
    "docker_version_output.stdout": "Docker version 28.0.1, build 068a01e"
}

TASK [init_system : Docker-Compose version] ******************************************************************************************************************************************************************
ok: [192.168.50.130] => {
    "docker_compose_version_output.stdout": "Docker Compose version v2.33.1"
}

TASK [init_system : 创建目录/etc/docker/] ********************************************************************************************************************************************************************
ok: [192.168.50.130]

TASK [init_system : 拷贝docker配置daemon.json] ***************************************************************************************************************************************************************
ok: [192.168.50.130]

TASK [init_system : 取消journald作为Docker日志引擎] **********************************************************************************************************************************************************
ok: [192.168.50.130]

TASK [init_system : 启动Docker守护进程并让它关机重启也生效] **************************************************************************************************************************************************
changed: [192.168.50.130]

PLAY RECAP ***************************************************************************************************************************************************************************************************
192.168.50.130             : ok=17   changed=1    unreachable=0    failed=0    skipped=5    rescued=0    ignored=0
kelleygo
随笔记录,为技术沉淀.
目录
相关文章
ubuntu2404手动安装docker
ubuntu手动安装docker.md Ubuntu 24.04.1 LTS 安装docekr-compose docker; 非snap 安装. 查看系统版本: # lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 24.04.1 LTS Release: 24.04 Codename: noble 一 安装前准备 强制删除snap安装的docekr,如果没有使用snap安装过docker请跳过。 sudo snap remove –purge docker 二 安装步骤: sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common curl -fsSL https://download.
2025-3-18
Centos7.x配置Go开发环境
Centos7.x配置Go开发环境 安装golang 准备环境 $ sudo yum update $ sudo yum install wget vim -y 源码包编译安装(可下最新版本): $ wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz $ sudo tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gz https://studygolang.com/dl下载最新的源码包编译 设置环境变量 Go代码必须放在工作空间中,实际上就是一个目录,且必须包含src、pkg、bin三个子目录。它们的用途如下: bin:包含编译后的可执行命令 pkg:包含包对象 src:包含Go的源文件,它们被组织成包 因此首先创建go语言的工作空间: $ mkdir $HOME/gowork 然后在配置文件中添加环境变量 $ vim /etc/profile 加入以下内容: # Golang export GO111MODULE=on export GOROOT=/usr/local/go export GOPATH=$HOME/gowork export GOCACHE=/tmp/gocache export GONOSUMDB="github.
2024-4-21
ubuntu 22.04 安装go
ubuntu 22.04 安装go(适用ubuntu20以上版本) 环境准备 apt update && apt-get upgrade -y 环境安装 mkdir ~/go cd ~/go wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz tar -C /usr/local go1.22.2.linux-amd64.tar.gz 设置环境变量 Go代码必须放在工作空间中,实际上就是一个目录,且必须包含src、pkg、bin三个子目录。它们的用途如下: bin:包含编译后的可执行命令 pkg:包含包对象 src:包含Go的源文件,它们被组织成包 因此首先创建go语言的工作空间: $ mkdir $HOME/gowork 在配置文件中添加环境变量 $ vim /etc/profile 加入以下内容: export GOROOT=/usr/local/go export GOPATH=$HOME/gowork export GOBIN=$GOPATH/bin export PATH=$GOPATH:$GOBIN:$GOROOT/bin:$PATH export GONOSUMDB="github.com" export GONOPROXY="github.com" export GOPROXY=https://goproxy.cn,direct export GOPRIVATE="github.
2024-4-21
ubuntu22.04安装gitlab
1. gitlab安装前准备 系统版本:Ubuntu 22.04.1 LTS 1.1 更新系统环境 sudo apt update sudo apt upgrade 1.2 安装和配置必须的依赖项 1 2 sudo apt-get install -y curl openssh-server ca-certificates tzdata perl sudo apt-get install curl 2.下载和安装gitlab 2.1 下载指定的版本 1 curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash 2.2 开始安装 注意:localhost为访问gitlab的地址,85为访问gitlab的端口号 1 sudo EXTERNAL_URL="http://localhost:85" apt-get install gitlab-ce=15.9.3-ce.0 除非您在安装过程中指定了自定义密码,否则将随机生成一个密码并存储在 /etc/gitlab/initial_root_password文件中(出于安全原因,24 小时后,此文件会被第一次 gitlab-ctl reconfigure 自动删除, 因此若使用随机密码登录,建议安装成功初始登录成功之后,立即修改初始密码)。使用此密码和用户名 root 登录。
2023-4-28
centos 7.9离线安装docker
centos 7.9安装docker 1.离线下载docker https://download.docker.com/linux/static/stable/x86_64/docker-20.10.9.tgz 2.离线安装docker 准备前: 2.0.1 关闭Selinux 首先, 执行getenforce或sestatus, 查询selinux状态, CentOS系统安装后默认为enforcing. getenforce 或 sestatus 2.0.2 修改selinux配置 1 2 3 4 5 vim /etc/selinux/config 修改selinux配置文件 SELINUX=enforcing 修改为 SELINUX=disabled 2.0.3 重复服务器以上修改生效 1 reboot 2.1 移动文件到目标服务器并执行tar命令解压,如: tar -zxvf docker-20.10.9.tgz 2.2 将/home/docker目录下解压出来的所有docker文件复制到 /usr/bin/ 目录下 cp docker/* /usr/bin/ 2.3 将docker注册为service,进入/etc/systemd/system/目录,并创建docker.service文件 1 2 cd /etc/systemd/system/ touch docker.
2023-3-2