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),用户事先写一个 ...
随机推荐
- express 随笔
#express 1.使用Express 应用生成器 npm install express-generator -g 2.创建一个命名为 myapp 的应用 express myapp 3.安装所有 ...
- delphi调用windows自带语音功能
windows自带语音接口 SAPI.SpVoice, 接口说明如下 https://docs.microsoft.com/en-us/previous-versions/windows/deskto ...
- 系统重启后,mr程序不生成当前时间段的MRx文件问题
系统重启后,mr程序不生成当前时间段的MRx文件问题 2019-4-2 之前使用正常的MR程序,系统重启后无法生成MRE\MRO\MRS文件. 服务器有两个时钟:硬件时钟和系统时钟 硬件时钟从根本上讲 ...
- python练习题-day18
1.匹配一行文字中的所有开头的字母内容 import re s="i love you not because of who you are, but because of who i am ...
- 搭建docker私有仓库
保存镜像的地方成为仓库(registry).目前有2种仓库:公共仓库和私有仓库. 最方便的是使用公共仓库上传和下载镜像,下载不需要注册,上传需要到公共仓库注册.公共仓库网站:https://hub.d ...
- Python3学习之路~8.6 开发一个支持多用户在线的FTP程序-代码实现
作业: 开发一个支持多用户在线的FTP程序 要求: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp s ...
- python基础之 time,datetime,collections
1.time模块 python中的time和datetime模块是时间方面的模块 time模块中时间表现的格式主要有三种: 1.timestamp:时间戳,时间戳表示的是从1970年1月1日00:00 ...
- django中如何实现分页功能
1.在html页面中导入js文件和css文件 <link rel="stylesheet" href="../../../static/css/jquery.pag ...
- VS 通过局域网访问调试状态下的web应用程序
1.点击vs的启动按钮 2.在任务栏找到IIS Express的图标,点击“显示所有应用程序” 3.如果只有本地localhost访问方式,点击对应应用程序的本地URL,就会显示对应的配置文件 4.点 ...
- Bioinfo online workshop
http://icb.med.cornell.edu/index.html%3Fpage_id=2068.html 1. Datacamp https://www.datacamp.com/cours ...