linux系统ansible一键完成三大服务器基础配置(剧本)
ansible自动化管理剧本方式一键完成三大服务器基础配置
环境准备:五台服务器:管理机m01:172.16.1.61,两台web服务器172.16.1.7,172.16.1.8,nfs存储服务器172.16.1.31,备份服务器rsync172.16.1.41
要求实现:nfs服务器共享目录/data给两台web服务器
三台服务器可以通过定时任务 备份到备份服务器rsync
实时监控nfs/data目录,并实时备份到备份服务器rsync
开整:
一、配置管理机m01
1.下载基本软件
cd /server/scripts
vim benjipeizhi.sh
#!/bin/sh
yum install oppenssh oppenssl -y &&\
systemctl restart sshd &&\
systemctl enable sshd &&\
yum install opel-releare -y &&\
yum install ansible -y &&\
yum install libselinux-python -y
3.编辑主机列表,方便批量管理(1代表服务端,2代表客户端)
vim /etc/ansible/hosts
[oldboy]
172.16.1.7
172.16.1.8
172.16.1.31
172.16.1.41
[rsync]
172.16.1.41
[rsync2]
172.16.1.7
172.16.1.8
172.16.1.41
[nfs1]
172.16.1.31
[nfs2]
172.16.1.7
172.16.1.8
[sersync]
172.16.1.31
4.创建并分发公钥,实现免密连接
vim fenfa.sh
#!/bin/sh
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
for ip in 7 8 31 41
do
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
5.下载sersync实时监控软件(本人是从桌面直接拉虚拟机的)
解压然后编辑配置文件(适当修改部分即可)这一个是通过copy模块远程复制过去的,在本机编辑好
vim /server/tools/appliction/sersync/conf
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data">
<remote ip="172.16.1.41" name="backup"/>
<remote ip="172.16.1.41" name="oldboy"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
rsync服务端配置文件也是通过copy模块远程推送,所以现在本机编辑好
vim /etc/rsyncd.conf
#!/bin/sh
uid = rsync
gid = rsync
use chroot = no
fake super = yes
max connections =200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
list = false
hosts allow = 172.16.1.0/24
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = welcome to oldboyedu backup!
path = /backup/
6.连接测试
sh fenfa.sh 执行创建并分发公钥的脚本
ansible oldboy -m command -a "ifconfig" 显示四台服务器IP说明远程连接成功
重点来啦,写剧本
在/etc/ansible下创建yaml目录,剧本名为one.yml
mkdir -p /etc/ansible/yaml
vim one.yml
- hosts: nfs1
tasks:
- name: 安装nfs
yum: name=nfs-utils state=installed
- name: 安装rpc
yum: name=rpcbind state=installed
- name: 编辑nfs共享目录为/data
shell: echo "/data 172.16.1.0/24(rw,anync,all_squash)" >/etc/exports
- name: 创建目录data
file: name=/data state=dircetory mode=0755 owner=nfsnobody group=nfsnobody
- name: 写入密码
shell: echo "export RSYNC_PASSWORD=123456" >>/etc/bashrc
shell: source /etc/bashrc
shell: echo "123456" >/etc/rsync.password
- name: 启动rpc并开机自启动
systemd: name=rpcbind.service enabled=yes state=started
- name: 启动nfs并开机自启动
systemctl: name=nfs enbaled=yue state=started
- hosts: nfs2
tasks
- name: 安装nfs和rpc
shell: yum install nfs-utils -y
shell: yum install rpcbind -y
- name: 启动prc并开机自启动
systemd: name=rpcbind.service enabled=yes state=started
-name: 启动nfs
systemd: name=nfs enabled=yes start=started
- name: 写入挂载目录到fstab
mount: src='172.16.1.31:/data' path=/mnt fstype=nfs opts=rw state=present
- name: 生效fstab
shell: mount -a
- name: 写入密码
shell: echo "export RSYNC_PASSWORD=123456" >>/etc/bashrc
shell:source /etc/bashrc
shell: echo "123456" >/etc/rsync.password
- hosts:rsync1
tasks:
- name: 安装rsync
yum: name=rsync state=installed
- name: 把本地配置好的rsync服务端配置文件拷贝过去
copy: src=/etc/rsyncd.conf dest=/etc/rsyncd.conf mode=0600 backup=yes
- name: 写入密码
shell: echo "rsync_backup:123456" >/etc/rsync.password
- name: 设置密码权限
file: path=/etc/rsync.password mode=0600
- name: 创建用户
user: name=rsync
- name: 创建backup备份目录
file: name=/backup state=directory mode=0755 recurse=yes owner=rsync group=rsync
- name: 启动rsync服务
systemd: name=rsync enabled=yes state=started
- hosts: rsync2
tasks:
- name: 安装rsync
yum: name=rsync state=installed
- name: 密码写入
shell: echo "export RSYNC_PASSWORD=123456" >>/etc/bashrc
shell: source /etc/bashrc
shell: echo "123456" >/etc/rsync.password
- name: 设置密码权限
filse: name=/etc/rsync.password mode=0600
- name: 启动rsync
systemd: name=rsyncd enabled=yes state=started
- hosts: sersync
tasks:
- name: 下载监控机制
yum: name=inotify-tools state=installed
- name: 复制本地编辑好的配置文件
copy: src=/server/tools dest=/server/tools
copy: src=/server/tools/applicantion dest=/ mode=755
- name: 启动监控
shell: /application/sersync/bin/sersync -d -n 10 -o /application/sersync/conf/confxml.xml
测试执行:ansible-playbook -C one.yml
正式执行:ansible-playbook one.yml
linux系统ansible一键完成三大服务器基础配置(剧本)的更多相关文章
- linux系统ansible一键完成三大服务器基本配置
准备: 五台服务器:一台管理服务器m01:172.16.1.61.两台应用服务器web01:172.16.1.7.web02:172.16.1.8.一台存储服务器nfs:172.16.1.31.一台备 ...
- 在本机使用虚拟机安装一个linux系统,并搭建ftp服务器
一.Linux基础使用:linux服务器环境搭建(FTP服务器), 在本机使用虚拟机安装一个linux系统,并搭建ftp服务器,要求能使用ftp服务将本机文件到保存linux虚拟机上 资料: VMwa ...
- Linux系统之路——如何在服务器用U盘安装CentOS7.2(二)
Linux系统之路——如何在服务器用U盘安装CentOS7.2(一) 说明: 截止目前CentOS 7.x最新版本为CentOS 7.2.1511,下面介绍CentOS 7.2.1511的具体安装配置 ...
- Linux系统之路——如何在服务器用U盘安装CentOS7.2(一)
终于将CentOS7装上服务器(thinkserver250,不得不说联想的太烂了)了,过程无比艰辛,因为我发现网上大家提到的所有U盘安装CentOS7时碰到的问题几乎都被我碰到了,像什么: 1.刻录 ...
- linux系统下本地搭建git服务器
linux系统下如何搭建本地git服务器,用于存放团队的开发代码,如下步骤: 1.先用一台服务器来安装git,安装好linux以后,在这里选用的是Ubuntu 14.04.然后配置静态IP:172.1 ...
- 在Linux系统中如何设置APACHE服务器里的后台页面只允许某个IP地址访问
补充资料 本网络中使用LINUX服务器,web服务器是由APACHE搭建,IP地址为192.168.1.5,后台页面为/admin/login.jsp . 如何设置后台页面LOGIN.JSP只允许19 ...
- Linux系统上安装软件(ftp服务器)
一:安装ftp服务器 在安装linux系统的时候,自定义软件包安装时,我已经勾选了ftp服务器,所以已经 安装过了,如果没有勾选,需要额外下载ftp的安装包,进行安装. ftp服务器搭建过程中遇到的问 ...
- Linux系统下搭建FTP/SFTP服务器
传输文件经常使用ftp和sftp服务器.Windows下有多种可视化工具,使用快捷.Linux经常需要自行搭建这两种服务器,当然搭建熟练的话,会更加快捷. 1.检查Linux系统是否安装了vsftp和 ...
- Linux系统编程(12)——shell基础
Shell的作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行一条,这种方式称为交互式(Interactive),Shell还有一种执行命令的方式称为批处理(Batch),用户事先写一个 ...
随机推荐
- linux 的基础命令
date 查看时间 cal 查看日历 cal 2009 cal 10 2019 ls 查看目录下的内容 ls -alh tree 以树的形式查看目录内容 bc 计算器 M ...
- Multi-Projector Based Display Code ------- Home
Overview This project provides you with the tools and techniques you need to create your own large-a ...
- 平安银行在开源技术选型上的思考和实践 RocketMQ
小结: 1. https://mp.weixin.qq.com/s/z_c5D8fvHaYvHSczm0nYFA 平安银行在开源技术选型上的思考和实践 平安银行·吴建峰 阿里巴巴中间件 3月7日 随着 ...
- openLayers,常见地图实例
http://openlayers.org/en/master/examples/epsg-4326.html -- 标尺 http://openlayers.org/en/master/exampl ...
- Springboot-001-解决nested exception is org.apache.ibatis.binding.BindingException: Parameter 'env' not found. Available parameters are [arg1, arg0, param1, param2]
环境:Springboot + Mybatis + MySQL + VUE 场景: 前端发出数据比对请求,在服务后台与数据库交互时,接口提示错误信息如下所示: { "code": ...
- 使用pushstate,指定回退地址
history.pushState(null,"testname", window.location.href); window.addEventListener('popstat ...
- git使用:本地项目推送到gitlab
背景:目前公司用gitlab管理我们的项目,经常遇到的问题是,我会在其他已有项目上直接进行修改,然后用于新项目的自动化测试,但是本地推送到gitlab的时候每次都要重新查询一遍怎么操作,特意写下这篇文 ...
- request内置对象
request内置对象(JSP九大内置对象之一)简述:内置对象即已在容器内部创建完成,可以直接调用的对象.容器在接收到客户端的请求后会创建一个对象用于处理请求信息,该对象就是内置对象(属于“javax ...
- Openshift 错误解决 "修改docker cgroup driver"
一.Openshift 错误解决 "修改docker cgroup driver" 一.错误如下 failed to run Kubelet: failed to create k ...
- CSAPP:第一章学习笔记:斗之气1段
一.信息就是位+上下文:系统中的所有信息(包括磁盘文件.内存中的程序.网络上传送的数据),都是由一串比特表示,根据上下文对这些比特表示进行翻译. 二.C程序编译过程 1.源码结构 // test.c ...