一、ansible控制端安装和客户端主机组准备(/etc/ansible/hosts)
#rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
# yum install ansibel -y
#cat /etc/ansbileb/hosts
[front]
192.168.0.241 ansible_ssh_pass=centos
192.168.0.242 ansible_ssh_pass=centos
[static]
192.168.0.243 ansible_ssh_pass=centos
192.168.0.244 ansible_ssh_pass=centos
[KW:children]
front
static
[resin]
192.168.0.245 ansible_ssh_pass=centos
192.168.0.246 ansible_ssh_pass=centos
[redis]
192.168.0.247 ansible_ssh_pass=centos
192.168.0.248 ansible_ssh_pass=centos
[mongo]
192.168.0.9 ansible_ssh_pass=centos
192.168.0.8 ansible_ssh_pass=centos
[M3:children]
redis
mongo
[mysql]
192.168.0.251 ansible_ssh_pass=centos
192.168.0.252 ansible_ssh_pass=centos
192.168.0.254 ansible_ssh_pass=centos
192.168.0.253 ansible_ssh_pass=centos
二、ansible控制端与客户端主机的互信配置
三、mongodb yum源配置与安装
1、配置mongodb源,并下载rpm包进行yum安装
添加yum源和安装mongodb
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
[root@7cest ~]# cat /etc/yum.repos.d/MongoDB.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
[mongodb-org-2.6]
name=MongoDB 2.6 Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
[root@7cest ~]# yum clean all
[root@7cest ~]# yum makecache
直接yum安装如下:(mongodb为2.6版本,mongodb-org为3.0版本)
[root@7cest ~]# yum install -y mongodb
[root@7cest ~]# yum install -y mongodb-org
下载rpm包,包括依赖包后安装:
#依赖包下载工具包安装
[root@7cest ~]# yum install yum-utils -y
#使用方法:yum --enablerepo=[repo] install --downloadonly --downloaddir=<directory> <package-name>
#mongodb下载:(mongodb为2.6版本,mongodb-org为3.0版本)
[root@7cest mongo]# yum install --downloadonly --downloaddir=/root/mongo/ mongodb
[root@7cest mongo]# yum install --downloadonly --downloaddir=/root/mongo/mongo34/ mongodb-org
root@7cest mongo34]# ls
mongodb-org-3.4.0-1.el7.x86_64.rpm mongodb-org-shell-3.4.0-1.el7.x86_64.rpm
mongodb-org-mongos-3.4.0-1.el7.x86_64.rpm mongodb-org-tools-3.4.0-1.el7.x86_64.rpm
mongodb-org-server-3.4.0-1.el7.x86_64.rpm
[root@7cest mongo34]# rpm -ivh mongodb-org-3.4.0-1.el7.x86_64.rpm mongodb-org-shell-3.4.0-1.el7.x86_64.rpm mongodb-org-mongos-3.4.0-1.el7.x86_64.rpm mongodb-org-tools-3.4.0-1.el7.x86_64.rpm mongodb-org-server-3.4.0-1.el7.x86_64.rpm
二、mongodb启动配置
[root@7cest mongo34]# systemctl enable mongod.service
[root@7cest mongo34]# systemctl start mongod.service
[root@7cest mongo34]# ss -tunlp | grep mongod
tcp LISTEN 0 128 127.0.0.1:27017 *:* users:(("mongod",pid=3152,fd=7))
[root@7cest mongo34]# mongo #登录操作
2、通过ansible-playbook来安装mongodb
为 mongodb的部署创建一个工作目录,包括子目录和部署配置文件,安装 tree 命令,使用tree命令来查看:
[root@202-test ansiblesoft]# ls
keepalived mongoDB nginx
[root@202-test ansiblesoft]# pwd
/bak/ansiblesoft
[root@202-test ansiblesoft]# mkdir mongoDB/{file,tasks} -pv
[root@202-test ansiblesoft]# touch mongoDB/install_mongo.sh
[root@202-test ansiblesoft]# touch mongoDB/mongo_deploy.yml
[root@202-test ansiblesoft]# touch mongoDB/mongo.repo
[root@202-test ansiblesoft]# tree mongoDB/
mongoDB/
├── file
│ ├── mongodb-org-3.4.0-1.el6.x86_64.rpm
│ ├── mongodb-org-mongos-3.4.0-1.el6.x86_64.rpm
│ ├── mongodb-org-server-3.4.0-1.el6.x86_64.rpm
│ ├── mongodb-org-shell-3.4.0-1.el6.x86_64.rpm
│ └── mongodb-org-tools-3.4.0-1.el6.x86_64.rpm
├── install_mongo.sh
├── mongo_deploy.yml
├── mongo.repo
└── tasks
└── install_mongo.yml
2 directories, 9 files
2.1、创建主配置文件mongo-deploy.yml
# cat mongo-deploy.yml
# /bak/ansiblesoft/mongo/mongo-deploy.yml
- hosts : mongo
tasks :
- include : 'tasks/install_mongo.yml'
2.2、为 monogo创建部署文件和下载相关的软件包
注意:出于安全考虑,建议先下载软件包到本地服务器或ansible控制端,再通过ansible的拷贝功能,拷贝到ansible客户端进行安装。
1)、配置mongodb的yum源(客户端也需要配置mongodb的yum源,防止关联包)
# cat mongo.repo
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
2)、下载mongdb的rpm包(yum安装)
下载rpm包,包括依赖包后安装(参考上文1):
#依赖包下载工具包安装
# yum install yum-utils -y
#使用方法:yum install --downloadonly --enablerepo=[repo] --downloaddir=<directory> <package-name>
#mongodb下载:(mongodb为2.6版本,mongodb-org为3.0版本)
# yum install --downloadonly --downloaddir=/root/mongo/ mongodb
# yum install --downloadonly --downloaddir=/root/mongo/mongo34/ mongodb-o
我已经下载好在203主机了,所以通过scp拷贝到ansible本机下:
[root@202-test rpmsoft]# pwd
/bak/ansiblesoft/mongoDB/rpmsoft
[root@202-test rpmsoft]# scp root@192.168.0.203:/root/ansiblecest/rpmsoft/mongodb/* /bak/ansiblesoft/mongoDB/rpmsoft/
root@192.168.0.203's password:
mongodb-org-3.4.0-1.el6.x86_64.rpm 100% 5900 5.8KB/s 00:00
mongodb-org-mongos-3.4.0-1.el6.x86_64.rpm 100% 12MB 11.6MB/s 00:01
mongodb-org-server-3.4.0-1.el6.x86_64.rpm 100% 20MB 9.8MB/s 00:02
mongodb-org-shell-3.4.0-1.el6.x86_64.rpm 100% 11MB 11.2MB/s 00:01
mongodb-org-tools-3.4.0-1.el6.x86_64.rpm 100% 49MB 12.2MB/s 00:04
3)、创建主配置文件mongo-deploy.yml
# cat mongo-deploy.yml
# /bak/ansiblesoft/mongo/mongo-deploy.yml
- hosts : mongo
tasks :
- include : 'tasks/install_mongo.yml'
4)、创建yum安装脚本
[root@202-test mongoDB]# ls
file install_mongo.sh mongo_deploy.retry mongo_deploy.yml mongo.repo tasks
[root@202-test mongoDB]# cat install_mongo.sh
#!/bin/bash
yum install /root/ansiblerpm/mongo/file/* -y
注意:-y喔
5)、为mongo创建安装配置文件install_mongo.yml
[root@202-test mongoDB]# cat mongo_deploy.yml
# /bak/ansiblesoft/mongo/mongo-deploy.yml
- hosts : mongo
tasks :
# - include : 'tasks/install_mongo.yml'
- name : install epel
yum :
name : https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
state : installed
- name : file module (file/link/directory/hard/touch/absent)
file :
path : /root/ansiblerpm/mongo
state : directory
- name : copy module(copy mongodb-org rpm)
copy :
src : /bak/ansiblesoft/mongoDB/file
dest : /root/ansiblerpm/mongo/
mode : 0755
backup : yes
- name : copy install_mongo.sh
copy :
src : /bak/ansiblesoft/mongoDB/install_mongo.sh
dest : /root/ansiblerpm/mongo/
mode : 0755
backup : yes
- name : install mongo-org
shell : /bin/bash /root/ansiblerpm/mongo/install_mongo.sh
- name : chkconfig mongo-org
service :
name : mongod
enabled : yes
state : started
5)、mongodb服务检查
[root@202-test mongoDB]# ansible mongo -m shell -a "chkconfig --list | grep mongod"
192.168.0.9 | SUCCESS | rc=0 >>
mongod 0:off 1:off 2:off 3:on 4:off 5:on 6:off
192.168.0.8 | SUCCESS | rc=0 >>
mongod 0:off 1:off 2:off 3:on 4:off 5:on 6:off
[root@202-test mongoDB]# ansible mongo -m shell -a "ss -tunlp | grep mongod"
192.168.0.8 | SUCCESS | rc=0 >>
tcp LISTEN 0 128 127.0.0.1:27017 *:* users:(("mongod",5891,8))
192.168.0.9 | SUCCESS | rc=0 >>
tcp LISTEN 0 128 127.0.0.1:27017 *:* users:(("mongod",5773,8))