使用ansible批量在服务器安装docker
注意:
1. ansible版本是2.8,其他版本也都是支持的,低版本请自行百度下,看下模块的使用方法
2. 我修改了docker的存储路径,默认是在/var/lib/docker,系统盘太小,所以单独挂载了一块数据盘,挂载到了/data,执行脚本请确定有/data此目录
3. 使用直接修改 yourhosts即可
4. 配置范例
cat /etc/ansible/hosts
[yourhosts]
web01 ansible_host=192.168.10.30 ansible_port=22 ansible_user=root ansible_password=1qaz
web02 ansible_host=192.168.10.23 ansible_port=22 ansible_user=root ansible_password=2wsx
5. 执行脚本
ansible-playbook docker_install.yml
6. 脚本内容:
cat /usr/local/srcipt/docker_install.yml
---
- hosts: yourhosts
remote_user: root
tasks:
- name: Add epel-release repository
yum_repository:
name: epel
description: EPEL YUM repo
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
gpgcheck: no
- name: Installing System Dependency Packages
yum:
name:
- yum-utils
- device-mapper-persistent-data
- lvm2
state: present
disable_gpg_check: yes
- name: Add Docker Yum Repo
shell: yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
- name: Install docker
yum:
name: docker-ce
state: present
disable_gpg_check: yes
notify: install docker
handlers:
- name: Created symlink from docker.services
listen: install docker
shell: systemctl enable docker
- name: Create docker configuration file paths
listen: install docker
file:
path: /etc/docker
state: directory
- name: Create docker data paths
listen: install docker
file:
path: /data/docker
state: directory
# 注意 marker中不能有值,因为docker配置文件的#不是注释,有可能启动失败
- name: Create configuration
listen: install docker
blockinfile:
path: /etc/docker/daemon.json
create: yes
content: |
{
"live-restore": true,
"registry-mirrors": ["https://pee6w651.mirror.aliyuncs.com"]
}
marker: " "
# marker: "#{mark} ansible aliyun config"
- name: Modify the data file path
listen: install docker
lineinfile:
path: /usr/lib/systemd/system/docker.service
regexp: "^ExecStart"
line: ExecStart=/usr/bin/dockerd --graph /data/docker
- name: daemon reload
listen: install docker
shell: systemctl disable docker && systemctl enable docker && systemctl daemon-reload
- name: start docker server
listen: install docker
service:
name: docker
state: started
联系客服