最近在部署类似私有云项目,需要在本地用ansible
自动化工具去创建启动相关db
,看了一下比较不错的ansible
模块docker_container
role
项目tag
#``repo``源更新
#可在清华大学镜像站进行镜像源更新操作
cat /etc/yum.repos.d CentOS-Base.repo
[base]
name=Centos-7.4-Base
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/7.5.1804/os/x86_64/
enabled=1
gpgcheck=0
[update]
name=Centos-7.4-Updates
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/7.5.1804/updates/x86_64/
enabled=1
gpgcheck=0
[extras]
name=Centos-7.4-Extras
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/7.5.1804/extras/x86_64/
enabled=1
gpgcheck=0
[centosplus]
name=Centos-7.4-Centosplus
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos/7.5.1804/centosplus/x86_64/
enabled=1
gpgcheck=0
[docker-ce]
name=Docker-Ce-7.4
baseurl=http://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/
enabled=1
gpgcheck=0
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/
failovermethod=priority
enabled=1
gpgcheck=0
#安装相关包及依赖
yum clean all && yum repolist
yum -y install ansible docker-ce python-pip
pip install docker-py
#启动服务
systemctl start docker
#下载相关镜像
docker pull docker.io/redis:latest
docker pull docker.io/mongo:latest
docker pull docker.io/mysql:latest
roles
项目及yaml
编写#当前roles目录结构
tree roles/
├── mongo
│ ├── defaults
│ ├── files
│ │ └── create.sh
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
├── mysql
│ ├── defaults
│ ├── files
│ ├── handlers
│ ├── meta
│ ├── tasks
│ │ └── main.yaml
│ ├── templates
│ └── vars
│ └── main.yaml
└── redis
├── defaults
├── files
├── handlers
├── meta
├── tasks
│ └── main.yaml
├── templates
└── vars
└── main.yaml
MySQL yaml
#roles/mysql/tasks/main.yaml
- name: 'install mysql database:mysql'
docker_container:
name: mysql #启动时 --name的名字
image: "docker.hobot.cc/mysql:5.7.23" #镜像名称
pull: no #不需要从外部拉取
volumes:
- "/etc/mysql/conf:/etc/mysql" #本地映射目录
- "/etc/mysql/data:/var/lib/mysql"
env:
MYSQL_ROOT_PASSWORD: "{{ password }}" #MySQL密码(vars/main.yaml定义)
ports:
- "3306:3306" #端口映射
state: started #启动
restart_policy: always #指定退出后可进行启动
Redis yaml
- name: 'install redis database:05b3651ee24e'
docker_container:
name: redis #启动时 --name的名字
image: "docker.hobot.cc/redis:5.0" #镜像名称
pull: no #不需要从外部拉取
volumes:
- "/etc/redis/conf:/usr/local/etc/redis" #本地映射目录
- "/etc/redis/data:/data"
env:
REDIS_PASSWORD: "{{ password }}" #redis密码(vars/main.yaml定义)
ports:
- "6379:6379" #端口映射
state: started #启动
restart_policy: always
mongo yaml
#说说这里,mongo这块的权限有点坑,我下个文章会专门讲一下mongo权限这块,如果你在env设置账号密码,那么库必须是admin
#mongo_user模块中是可以创建用户和密码以及相关的role权限,但是database也必须是admin
#你可以完全把mongo_user那部分注视,然后将env也注视,然后默认就是免密登录,
#然后在利用脚本进行创建你需要的账号密码以#及连接的库
- name: 'install mongo database:05b3651ee24e'
docker_container:
name: mongo
image: "docker.hobot.cc/mongo:3.6.8"
env:
MONGO_INITDB_ROOT_USERNAME: admin #初始化用户名
MONGO_INITDB_ROOT_PASSWORD: asdjcvkjxbcv #初始化用户密码
MONGO_INITDB_DATABASE: admin #相关的库,官网上说的意思是.js文件可创建
pull: no
volumes:
- "/etc/mongo/configdb:/data/configdb"
- "/etc/mongo/db:/data/db"
ports:
- "27017:27017"
state: started
restart_policy: always
- mongodb_user:
login_user: super #登录的用户
login_password: 123456 #登录的密码
database: admin #需要进入的数据库(目前测试必须是admin)
name: pangu #需要创建的用户
password: 123 #需要创建的密码
state: present #创建状态
roles: #申请权限(给pangu用户的aitc_matrix库的所有者权限)
- db: aitc_matrix
role: root
- name: "copy create user script to tmp "
copy:
src=create.sh #按我上面说的话,如果注视了,那么就执行个脚本
dest=/tmp/create.sh
- name: "run create user script"
command:
/bin/bash "/tmp/create.sh"
mongo
脚本及执行ansible-playbook
#roles/mongo/files/create.sh
#!/bin/bash
mongo aitc_matrix --eval "db.createUser({user:\"pangu\",pwd:\"12345\",roles:[{role:\"dbOwner\",db:\"aitc_matrix\"},{role:\"dbOwner\",db:\"aitc_matrix1\"}]})"
#/etc/mongo/configdb/mongo.conf
security:
authorization: enabled
#inventory.ini
[global]
10.31.11.154
#site.yaml
- name: entry main
hosts: 10.31.11.154
roles:
- mongo
- redis
- mysql
#run playbook
ansible-playbook -i inventory.ini site.yaml -vvv