9oijl1111 发表于 2016-11-29 09:55:13

利用ansible批量创建iis站点

准备: 系统必须大于等于windows2008 R2 系统 升级 PowerShell至3.0 安装iis 安装net
ansible控制台目录结构

1
2
3
4
5
6
7
8
9
.
├── hosts #ansible 存放客户机IP
├── roles
│   └── web
│       ├── files
│       │   └── web.ps1 # 创建iis站点ps脚本
│       └── tasks
│         └── main.yml #ansible 控制脚本
└── web.yml # ansible 调用脚本




web.yml 文件说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- hosts: '{{ host }}' # 客户机hosts文件节点名字
vars:
    sitePort: 9002 # 端口号 创建网站用到完成会重置80端口
    SiteName: default # 网站应用程序池名字
    SiteAppPoolsModel: Integrated # 托管管道模式 Classic 、Integrated
    managedRuntimeVersion: v4.0 # net版本号
    WebSitePath: D:\default # 网站数据存放地址
    HostHeader1:# 第一web访问域名
    HostHeader2:# 第二个访问域名
    defaultDocument: index.html #默认主页
    IISLogFile: d:\LogFiles # web网站日志存放地址
    net32Or64: false# 是否打开32模式支持 64位系统使用 True、false
roles:
    - web # ansible 执行标签





1
2
3
4
5
6
7
8
9
10
11
12
13
main.yml 文件内容说明
- name: install web site
script: web.ps1 {{sitePort}} {{SiteName}} {{SiteAppPoolsModel}} {{managedRuntimeVersion}} {{WebSitePath}} {{HostHeader1}} {{HostHeader2}} {{defaultDocument}} {{IISLogFile}} {{net32Or64}} # 客户机执行脚本及给脚本传参数
#参数 {{sitePort}} web端口号 可以默认
# {{SiteName}} # 网站应用程序池名字
# {{SiteAppPoolsModel}} 托管管道模式 Classic 、Integrated可以配置默认
# {{managedRuntimeVersion}} net版本号 可以配置默认
# {{WebSitePath}} 网站数据存放地址
# {{HostHeader1}} 第一web访问域名
# {{HostHeader2}} 第二个访问域名 默认没打开
#{{defaultDocument}} 默认主页
# {{IISLogFile}} web网站日志存放地址 可以配置成默认值
# {{net32Or64}} 是否打开32模式支持 64位系统使用可以配置成默认





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
web.ps1 文件内容说明
# ************************************************************
# *                                                          *
# *               Powershell部署web站点脚本                  *
# *                                                          *
# ************************************************************
#set-executionpolicy remotesigned
# 建立IIS站点所用参数
#################################################################################################
#参数申明
param($a,$b,$c,$d,$e,$f,$g,$h,$i,$j)
$sitePort = $a#端口
$SiteName = "$b"    #站点名
$SiteAppPools = $SiteName#进程池名
$SiteAppPoolsModel = "$c"#进程池使用通道模式
$AppPoolType = "ApplicationPoolIdentity"      #指定应用程序池要使用的帐户标识(0 >Local Service 1 >Local System2 >Network Service3 >User 4 >ApplicationPoolIdentity)
$managedRuntimeVersion = "$d"#.net版本
$WebSitePath = "$e" #站点程序路径
$HostHeader1 = "$f"      #绑定站点域名
$HostHeader2 = "$g"      #绑定站点域名
$defaultDocument = "$h"
$IISLogFile = "$i\$SiteName" #IIS日志路径
$net32Or64 = $j#是否启用.net32模式
#################################################################################################
Import-Module WebAdministration#导入IIS模块
#创建IIS应用程序池
function BuildAppPool(){
    $AppPool = "iis:\AppPools\" + $SiteAppPools
    $existAppPool = Test-Path $AppPool
    if($existAppPool -eq $false){
      #创建应用程序池
      .$Env:windir\system32\inetsrv\appcmd.exe add apppool /name:$SiteAppPools /managedRuntimeVersion:$managedRuntimeVersion/managedPipelineMode:$SiteAppPoolsModel
      #指定应用程序池要使用的帐户标识
      .$Env:windir\system32\inetsrv\appcmd.exe set config /section:applicationPools /.processModel.identityType:$AppPoolType
      #设置应用程序池使用.net版本
      .$Env:windir\system32\inetsrv\appcmd.exe add apppool /name:$SiteAppPools /managedRuntimeVersion:$managedRuntimeVersion/managedPipelineMode:$SiteAppPoolsModel
      #限制进程使用内存上限为1.5G
      .$Env:windir\system32\inetsrv\appcmd.exe set config /section:applicationPools /.recycling.periodicRestart.privateMemory:1572864
      #指定进程固定回收时间
      .$Env:windir\system32\inetsrv\appcmd.exe set apppool /apppool.name: $SiteAppPools /recycling.periodicRestart.time:1.00:00:00
      #启用.net32模式
      .$Env:windir\system32\inetsrv\appcmd.exe set config /section:applicationPools /.enable32BitAppOnWin64:$net32Or64
      #是否自动启动
      .$Env:windir\system32\inetsrv\appcmd.exe set config /section:applicationPools /.autoStart:$true
    }
}
   
#创建IIS应用站点
function BuildSite(){
    $appSitePath = "iis:\sites\"+$SiteName
    $existweb = Test-Path $appSitePath
    if(!$existweb)
    {
      New-Website -name $SiteName -port $sitePort-ApplicationPool $SiteAppPools -PhysicalPath $WebSitePath
       .$Env:windir\system32\inetsrv\appcmd.exe set site $SiteName /bindings:"http/*:80:$HostHeader1" # 默认打开一个域名加80端口 默认所有IP
       #.$Env:windir\system32\inetsrv\appcmd.exe set site $SiteName /bindings:"http/*:80:$HostHeader1,http/*:80:$HostHeader2" # 可以添加多域名访问通一个网站 重置默认端口80
       # New-WebBinding -Name $SiteName -IPAddress "*" -Port $sitePort -HostHeader $HostHeader1 # 端口号加域名访问 根据实际情况修改
      .$Env:windir\system32\inetsrv\appcmd.exe set config /section:directoryBrowse /enabled:false   
    }
    else{
    echo "'$SiteName' is Already exists"
    }
}
   
#设置IIS日志记录路径
function CreatIISLogFile(){
    .$Env:windir\system32\inetsrv\appcmd.exe set site $SiteName "-logfile.directory:$IISLogFile"
}
   
   
function RunBuild(){
    BuildAppPool
    BuildSite   
    CreatIISLogFile
    .$Env:windir\system32\inetsrv\appcmd.exe start site $SiteName
}
RunBuild
使用方式
1、可以传入所有参数
ansible-playbook -i hostsweb.yml -verbose --extra-vars "host=test sitePort=9002 SiteName=ttyykk SiteAppPoolsModel=Integrated managedRuntimeVersion=v4.0 WebSitePath=D:\werwerw HostHeader1=www.qkaxml.com defaultDocument=index.htm IISLogFile=d:\LogFiles net32Or64=false"
2、根据情况传入参数 例如
网站名字 数据存放地址 域名等   可以根据实际情况修改。
ansible-playbook -i hostsweb.yml -verbose --extra-vars "host=test SiteName=iyunvWebSitePath=D:\iyunv.com HostHeader1=
完整项目 下载附件


页: [1]
查看完整版本: 利用ansible批量创建iis站点