简介
从接触到使用Ansible大概有一天的时间,对它的理解也是比较肤浅。我单纯的认为,他可以帮助我们在服务器之间传输文件,同时还可以执行一些shell命令。
安装Ansible
sudo apt-get update
sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get update
sudo apt-get install ansible
编写docker的playbook yaml文件
---
- hosts: localhost
remote_user: root
tasks:
- name: 创建目录
file:
path: "{{ path }}"
state: directory
owner: root
group: root
mode: 0775
loop:
- /opt/tools
- /etc/docker
loop_control:
loop_var: path
- name: 下载软件包
get_url:
url: https://download.docker.com/linux/static/stable/x86_64/docker-19.03.9.tgz
dest: /opt/tools
- name: 解压软件包
unarchive:
src: /opt/tools/docker-19.03.9.tgz
dest: /usr/local
copy: no
mode: 0755
- name: COPY 执行文件
shell:
chdir: /usr/local
cmd: cp -rf docker/* /usr/bin
- name: docker 镜像加速
shell:
cmd: |
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
- name: service 管理docker
shell:
cmd: |
cat > /lib/systemd/system/docker.service << EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
EOF
- name: 启动docker
systemd:
daemon_reload: yes
name: docker
state: restarted
enabled: yes
- name: 删除/usr/local/docker
file:
path: /usr/local/docker
state: absent
- name: 查看状态
shell: docker info
register: docker
- debug:
var: docker.stdout_lines
开始安装
# 检查yaml 文件是否存在语法格式错误
ansible-playbook --check --syntax-check docker.yaml
# 模拟运行 -C 或者 --check
ansible-playbook -C docker.yaml
# 没有问题的话执行安装
ansible-playbook docker.yaml
检查安装
docker info