跳转至

Ansible Playbook 安装 Docker


2018-03-20 by dongnan

目标

使用 ansible-playbook 批量安装 docker 服务。

环境

部署及版本,请参考这里

管理节点

系统: CentOS 7.4 amd64
IP: 10.0.1.1

托管节点

系统: Ubuntu 16.04 amd64
IP: 10.0.1.11 - 13

条件

  • 已经配置 SSH KEY 方式登录。
  • 主机通信正常,能够返回 SUCCESS
ansible docker -m ping

node3 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
node1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
node2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
} 

配置文件

playbook 文件

cat install_docker.yml

---
- hosts: docker
  remote_user: root
  vars:
    lsb_release: $(lsb_release -cs)
  tasks:
    - name: install tools
      apt: name=\{{item}\} state=present
      with_items:
        - apt-transport-https
        - ca-certificates
        - software-properties-common
        - curl

    - name: add Docker GPG key
      shell: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

    - name: add Docker Repository
      command: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable"

    - name: retrieve new lists of packages
      command: apt-get update

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

    - name: get docker-compose
      shell: curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
    - name: install docker-compose
      command: chmod +x /usr/local/bin/docker-compose 

主机信息

tail  -n 5 /etc/ansible/hosts

[docker]
node1
node2
node3 

执行 playbook

ansible-playbook install_docker.yml
# ...省略
PLAY RECAP ********************************************************************************************************************************************
node1                      : ok=8    changed=7    unreachable=0    failed=0  
node2                      : ok=8    changed=7    unreachable=0    failed=0  
node3                      : ok=8    changed=7    unreachable=0    failed=0 

参考

Ansible 小试牛刀

回到页面顶部