ad-hoc实战

要求:利用Ansible搭建一个简易的作业网站,web端文件上传目录共享至nfs端,nfs的数据同步至backup

环境准备

主机名 主机角色 外网IP 内网IP
m01 ansible管理端 10.0.0.61 172.16.1.61
backup ansible被管理端、rsync服务端、nfs服务端 10.0.0.41 172.16.1.41
nfs ansible被管理端、rsync客户端、nfs服务端 10.0.0.31 172.16.1.31
web03 ansible被管理端、部署提交作业代码,挂载上传目录即可 10.0.0.9 172.16.1.9

Ansible管理端环境准备

# 定义管理清单
[root@m01 ~]$ vim /etc/ansible/hosts
[zy_php]
web03 ansible_ssh_host=172.16.1.9
backup ansible_ssh_host=172.16.1.41
nfs ansible_ssh_host=172.16.1.31 # 下发私钥
[root@m01 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.9
[root@m01 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41
[root@m01 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31

Rsync服务部署

常规步骤

# 1.创建统一用户
groupadd www -g 666
useradd www -u 666 -g 666 -s /sbin/nologin -M # 2.安装rsync
yum install -y rsync # 3.修改配置文件
[root@backup ~]$ vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#--------------------------------------------------------------
[zy_backup]
comment = welcome to oldboyedu backup!
path = /backup # 4.创建备份目录
mkdir /backup # 5.修改备份目录权限
chown www.www /backup # 6.创建虚拟用户密码文件
echo 'rsync_backup:123' > /etc/rsync.passwd # 7.修改密码文件权限为600
chmod 600 /etc/rsync.passwd # 8.启动服务
systemctl start rsyncd # 9.加入开机自启
systemctl enable rsyncd

Ansible步骤

# 1.创建统一用户
ansible backup -m group -a 'name=www gid=666 state=present'
ansible backup -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no state=present' # 2.安装rsync
ansible bakcup -m yum -a 'name=rsync state=present' # 3.修改配置文件
ansible backup -m copy -a 'src=/root/rsync.mb dest=/etc/rsyncd.conf owner=root group=root mode=0644' vim /root/rsync_mb
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#--------------------------------------------------------------
[zy_backup]
comment = welcome to oldboyedu backup!
path = /backup # 4.创建备份目录并更改属组属主
ansible backup -m file -a 'path=/backup owner=www group=www mode=0755 state=directory' # 5.创建密码文件并更改权限为600
ansible backup -m copy -a 'content="rsync_backup:123" dest=/etc/rsync.passwd owner=root group=root mode=0600' # 6.启动服务并加入开机自启
ansible backup -m service -a 'name=rsyncd state=started enabled=yes'

NFS服务部署

常规步骤

# 1.创建统一用户
groupadd www -g 666
useradd www -u 666 -g 666 -s /sbin/nologin -M # 2.安装NFS服务
yum -y install nfs-utils rpcbind # 3.编辑共享存储配置文件
vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666) # 4.创建共享目录
mkdir /data # 5.修改共享目录属主属组
chown www.www /data # 6.启动服务
systemctl start nfs-server # 7.加入开机自启
systemctl enable nfs-server

Ansible步骤

# 1.创建统一用户
ansible nfs -m group -a 'name=www gid=666 state=present'
ansible nfs -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no state=present' # 2.安装nfs
ansible nfs -m yum -a 'name=nfs-utils state=present'
ansible nfs -m yum -a 'name=rpcbind state=present' # 3.编辑共享存储配置文件
ansible nfs -m copy -a "content='/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)' dest=/etc/exports" # 4.创建出共享目录并修改属主属组
ansible nfs -m file -a 'path=/data owner=www group=www mode=0755 state=directory' # 5.启动服务并加入开机自启
ansible nfs -m service -a 'name=nfs-server state=started enabled=yes'

Http服务部署

常规步骤

# 1.创建统一用户
groupadd www -g 666
useradd www -u 666 -g 666 -s /sbin/nologin -M # 2.安装http服务
yum install -y httpd php # 3.统一用户
vim /etc/httpd/conf/httpd.conf
1)修改前
User apache
Group apache
2)修改后
User www
Group www # 4.进入站点目录部署代码
cd /var/www/html
rz # 5.解压代码文件
unzip kaoshi.zip # 6.修改php代码,更改上传目录
vim /var/www/html/upload_file.php
$wen="/var/www/html/pic"; # 7.上传图片至/var/www/html/pic文件中
cd /var/www/html/pic
rz # 8.更改站点目录属主属组
chown www.www /var/www/html/ # 9.启动服务
systemctl start httpd

Ansible步骤

# 1.创建统一用户
ansible web03 -m group -a 'name=www gid=666 state=present'
ansible web03 -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no state=present' # 2.安装http服务
ansible web03 -m yum -a 'name=httpd state=present'
ansible web03 -m yum -a 'name=php state=present' # 3.修改配置文件,统一用户
ansible web03 -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=0644'
vim /root/httpd.conf
ServerRoot "/etc/httpd" Listen 80 Include conf.modules.d/*.conf User www
Group www ServerAdmin root@localhost <Directory />
AllowOverride none
Require all denied
</Directory> DocumentRoot "/var/www/html" <Directory "/var/www">
AllowOverride None Require all granted
</Directory> <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted
</Directory> <IfModule dir_module>
DirectoryIndex index.html
</IfModule> <Files ".ht*">
Require all denied
</Files> ErrorLog "logs/error_log" LogLevel warn <IfModule log_config_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common <IfModule logio_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule> CustomLog "logs/access_log" combined
</IfModule> <IfModule alias_module> ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" </IfModule> <Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory> <IfModule mime_module> TypesConfig /etc/mime.types AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule> AddDefaultCharset UTF-8 <IfModule mime_magic_module> MIMEMagicFile conf/magic
</IfModule> EnableSendfile on IncludeOptional conf.d/*.conf # 4.部署代码至站点目录
ansible web03 -m unarchive -a 'src=/root/kaoshi.zip dest=/var/www/html/' # 5.在站点目录下创建用户上传目录
ansible web03 -m file -a 'path=/var/www/html/pic state=directory' # 6.修改php代码,更改上传目录
ansible web03 -m copy -a 'src=/root/upload.conf dest=/var/www/html/upload_file.php owner=root group=root mode=0644'
vim /root/upload.conf
<?php header("Content-type:text/html;charset=utf-8"); ini_set('date.timezone','Asia/Shanghai'); $wen=/var/www/html/pic; $pre=preg_match("/^([0-9])+_/",$_FILES['file']["name"][0]);
$size =$_FILES['file']["size"][0]; if (!is_dir($wen.'/')) { mkdir($wen.'/', 0777); } // foreach($_FILES['file']['error'] as $k=>$v){ if ($_FILES["file"]["error"][0] > 0 ) {
echo "上传失败!请查看是否选择了需要上传的文件!";
}else if($pre==0){ echo "上传失败!文件名有误,请修改文件名为你的编号加下划线开头<br/>例如:33_蒋某某.docx"; }else if ($size<10) { echo "上传失败!文件为空文件!";
}else{ $tmp_name = $_FILES["file"]["tmp_name"][0];
$name =$_FILES["file"]["name"][0]; if (file_exists($wen."/" . $name))
{
echo "上传失败,文件".$_FILES["file"]["name"][0] . " 已经存在 ";
}
else
{
move_uploaded_file($tmp_name,$wen."/".$name);
echo "文件".$_FILES["file"]["name"][0]."上传成功";
} } // }
?> # 7.更改站点目录属主属组
ansible web03 -m file -a 'path=/var/www/html state=file owner=www group=www mode=0755 state=directory' # 8.启动服务
ansible web03 -m service -a 'name=httpd state=started'

挂载web端上传目录至nfs共享目录,并利用backup备份

常规步骤

-web端操作
# 1.安装nfs
yum -y install nfs-utils # 2.挂载上传目录至nfs共享目录
mount -t nfs 172.16.1.31:/data /var/www/html/pic -nfs端操作
# 1.安装rsync
yum install -y rsync # 1.创建密码文件
echo '123' > /etc/rsync.passwd # 2.给密码文件授权
chmod 600 /etc/rsync.passwd # 3.备份共享目录至backup端
rsync -avz --delete /data rsync_backup@172.16.1.41::zy_backup --password-file=/etc/rsync.passwd

Ansible步骤

-web端
# 1.安装nfs
ansible web03 -m yum -a 'name=nfs-utils state=present' # 2.挂载上传目录至nfs共享目录
ansible web03 -m mount -a 'path=/var/www/html/pic src=172.16.1.31:/data fstype=nfs state=mounted' -nfs端
# 1.安装rsync
ansible nfs -m yum -a 'name=rsync state=present' # 1.创建密码文件,并指定权限
ansible nfs -m copy -a 'src=/root/mima dest=/etc/rsync.passwd mode=600'
vim /root/mima
123 # 2.备份共享目录至backup端
ansible nfs -m shell -a 'rsync -avz --delete /data rsync_backup@172.16.1.41::zy_backup --password-file=/etc/rsync.passwd' ansible nfs -m cron -a 'name="共享目录备份" minute=*/1 job="/usr/bin/rsync -avz --delete /data rsync_backup@172.16.1.41::zy_backup --password-file=/etc/rsync.passwd &>/dev/null"'

整合Ansible脚本

Ansible管理端需要准备的环境

# 1.准备好rsync配置文件
vim /root/rsync_mb
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#--------------------------------------------------------------
[zy_backup]
comment = welcome to oldboyedu backup!
path = /backup # 2.准备好web端http配置文件
vim /root/httpd.conf
ServerRoot "/etc/httpd" Listen 80 Include conf.modules.d/*.conf User www
Group www ServerAdmin root@localhost <Directory />
AllowOverride none
Require all denied
</Directory> DocumentRoot "/var/www/html" <Directory "/var/www">
AllowOverride None Require all granted
</Directory> <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted
</Directory> <IfModule dir_module>
DirectoryIndex index.html
</IfModule> <Files ".ht*">
Require all denied
</Files> ErrorLog "logs/error_log" LogLevel warn <IfModule log_config_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common <IfModule logio_module> LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule> CustomLog "logs/access_log" combined
</IfModule> <IfModule alias_module> ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" </IfModule> <Directory "/var/www/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory> <IfModule mime_module> TypesConfig /etc/mime.types AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule> AddDefaultCharset UTF-8 <IfModule mime_magic_module> MIMEMagicFile conf/magic
</IfModule> EnableSendfile on IncludeOptional conf.d/*.conf # 3.准备好站点配置文件
vim /root/upload.conf
<?php header("Content-type:text/html;charset=utf-8"); ini_set('date.timezone','Asia/Shanghai'); $wen=/var/www/html/pic; $pre=preg_match("/^([0-9])+_/",$_FILES['file']["name"][0]);
$size =$_FILES['file']["size"][0]; if (!is_dir($wen.'/')) { mkdir($wen.'/', 0777); } // foreach($_FILES['file']['error'] as $k=>$v){ if ($_FILES["file"]["error"][0] > 0 ) {
echo "上传失败!请查看是否选择了需要上传的文件!";
}else if($pre==0){ echo "上传失败!文件名有误,请修改文件名为你的编号加下划线开头<br/>例如:33_蒋某某.docx"; }else if ($size<10) { echo "上传失败!文件为空文件!";
}else{ $tmp_name = $_FILES["file"]["tmp_name"][0];
$name =$_FILES["file"]["name"][0]; if (file_exists($wen."/" . $name))
{
echo "上传失败,文件".$_FILES["file"]["name"][0] . " 已经存在 ";
}
else
{
move_uploaded_file($tmp_name,$wen."/".$name);
echo "文件".$_FILES["file"]["name"][0]."上传成功";
} } // }
?> # 4.在本地准备rsync客户端密码文件
vim /root/mima
123

脚本整合

[root@m01 ~]$ cat sb.sh
# 1.所有机器上创建统一用户
ansible zy_php -m group -a 'name=www gid=666 state=present'
ansible zy_php -m user -a 'name=www uid=666 group=666 shell=/sbin/nologin create_home=no state=present' # 2.所有机器安装对应服务
ansible bakcup -m yum -a 'name=rsync state=present'
ansible nfs -m yum -a 'name=nfs-utils state=present'
ansible nfs -m yum -a 'name=rpcbind state=present'
ansible nfs -m yum -a 'name=rsync state=present'
ansible web03 -m yum -a 'name=httpd state=present'
ansible web03 -m yum -a 'name=php state=present'
ansible web03 -m yum -a 'name=nfs-utils state=present' # 3.准备所有服务需要的文件
## rsync
ansible backup -m copy -a 'src=/root/rsync_mb dest=/etc/rsyncd.conf owner=root group=root mode=0644'
## nfs
ansible nfs -m copy -a "content='/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)' dest=/etc/exports"
## httpd
ansible web03 -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=0644'
## php代码
nsible web03 -m copy -a 'src=/root/upload.conf dest=/var/www/html/upload_file.php owner=root group=root mode=0644'
## rsync客户端密码文件
ansible nfs -m copy -a 'src=/root/mima dest=/etc/rsync.passwd mode=600' # 4.backup端:
## 创建备份目录并更改属组属主
ansible backup -m file -a 'path=/backup owner=www group=www mode=0755 state=directory'
## 创建密码文件并更改权限为600
ansible backup -m copy -a 'content="rsync_backup:123" dest=/etc/rsync.passwd owner=root group=root mode=0600'
## 启动服务并加入开机自启
ansible backup -m service -a 'name=rsyncd state=started enabled=yes' # 5.nfs端:
## 创建出共享目录并修改属主属组
ansible nfs -m file -a 'path=/data owner=www group=www mode=0755 state=directory'
## 启动服务并加入开机自启
ansible nfs -m service -a 'name=nfs-server state=reloaded enabled=yes' # 6.web端:
## 部署代码至站点目录
ansible web03 -m unarchive -a 'src=/root/kaoshi.zip dest=/var/www/html/'
## 在站点目录下创建用户上传目录
ansible web03 -m file -a 'path=/var/www/html/pic state=directory'
## 更改站点目录属主属组
ansible web03 -m file -a 'path=/var/www/html state=file owner=www group=www mode=0755 state=directory'
## 启动服务
ansible web03 -m service -a 'name=httpd state=started' # 7.做共享和备份
## 挂载上传目录至nfs共享目录
ansible web03 -m mount -a 'path=/var/www/html/pic src=172.16.1.31:/data fstype=nfs state=mounted'
## 备份共享目录至backup端
ansible nfs -m shell -a 'rsync -avz --delete /data rsync_backup@172.16.1.41::zy_backup --password-file=/etc/rsync.passwd'

ad-hoc实战的更多相关文章

  1. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  2. Ad hoc sql

    SQL Server如何启用Ad Hoc Distributed Queries? 2011-08-11 14:53 wangdingbang CSDN博客 字号:T | T   本文主要介绍了SQL ...

  3. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问

    消息 15281,级别 16,状态 1,第 2 行SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/Open ...

  4. XE7 & IOS开发之开发账号(3):证书、AppID、设备、授权profile的申请使用,附Debug真机调试、Ad hoc下iPA文件生成演示(XCode5或以上版本推荐,有图有真相)

    网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 注意,以下讨论都是以&q ...

  5. XE7 & IOS开发之开发账号(2):发布证书、发布授权profile的申请使用,附Ad hoc真机调试、生成ipa文件演示(XCode所有版本通用,有图有真相)

    网上能找到的关于Delphi XE系列的移动开发的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 注意,以下讨论都是以&q ...

  6. 启用与关闭 Ad Hoc Distributed Queries

    在数据库里执行以下脚本: 启用: exec sp_configure 'show advanced options',1reconfigureexec sp_configure 'Ad Hoc Dis ...

  7. Sql导出数据报错-->SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服 ...

  8. 解除SQL对组件"Ad Hoc Distributed Queries"的"STATEMENT'OpenRowset OpenDatasource"的访问

      SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为 ...

  9. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。

    今天单位一ASP.NET网站,里面有个功能是导出数据,发现一导出就报错,报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT ...

  10. ns3模拟无线Ad hoc 网络通信

    Ad hoc网络 Ad hoc网是一种多跳的.无中心的.自组织无线网络,又称为多跳网(Multi-hop Network).无基础设施网(Infrastructureless Network)或自组织 ...

随机推荐

  1. Hive启动留下的RunJar进程不能使用Kill -9 杀不掉怎么办?

    1.问题示例 [Hadoop@master Logs]$ jps 3728 ResourceManager 6976 RunJar 7587 Jps 4277 Master 3095 NameNode ...

  2. CC协议的诞生背景

    CC协议的诞生背景 在当今世界绝大部分国家的法律法规中,作品的版权一般都保留于创造者或拥有人手中,在没有特殊声明的情况下,任何人想要获取或使用该作品,都要事先取得版权所有者的授权,才可以进行合法的获取 ...

  3. 【学习】蓝牙的一些基础知识or什么是蓝牙

    蓝牙----Bluetooth(短距离无线通信技术)  2022-07-29   14:31:27 蓝牙技术有什么特点(体积小,易集成,低功耗,适用广,抗干扰,成本低,开放性) (1) 蓝牙模块体积很 ...

  4. Spring--AOP简介+入门案例

    AOP简介 面向切面编程:在不惊动原始设计的基础上,进行功能增强 各个要应用该功能的对象叫做连接点,那个功能叫做通知,表面上的代码没有发生变化,私下里发生变化的连接点,会出现切入点,切入点与通知通过切 ...

  5. 代码大全_V2(1,2章笔记)

    译序 这本书讲什么 代码大全 原名叫 code complete,它是什么,又不是什么? 不是IDE中的代码自动补全功能 不是软件源代码 "大全" 是 "编码完成&quo ...

  6. MySQL 某一列的值加入到另一列

    0.背景 文件url 文件名 /usr/local/img/goods/1/2021-12-22-e05bb433bc7a451ca5d7cc9d505d8ed8.jpg 酸枣糕.jpg /usr/l ...

  7. java数组排序及查找方法

    前言 在上一篇文章中,壹哥给大家讲解了数组的扩容.缩容及拷贝方式.接下来在今天的文章中,会给大家讲解更重要的数组排序及查找方法.今天的内容会有点难,希望你不要因此而退缩,挺过这一关,你会向上突破的! ...

  8. 使用 ApplicationContextAware 定义 SpringContextHolder 类

    需求:使用 @autowired注入一些对象,但发现不可以直接使用@Autowired,因为方法是static的,要使用该方法当前对象也必须是static,正常情况下@Autowired无法注入静态的 ...

  9. C++ 猜数字

    #include <iostream> #include <random> #include <limits> namespace random { std::ra ...

  10. mongodb安装及操作

    1.回顾 node服务器的写法 服务器 前后端分离 前后端不分离 express express生成器 ejs模版语法:变量.条件判断.循环渲染.引入 2.mongodb介绍 MongoDB 是一个基 ...