利用ansible批量管理windows2008 R2 以上dns服务器



准备:安装dns服务器net3.5  PowerShell DnsShell插件用到。DnsShell插件附件里面 ansible 支持环境也要安装的

ansible 目录结构说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.
├── DnsShell  #PowerShell dns支持模块 cp to C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules 正在文件夹
│   ├── DNSShell.dll
│   ├── DnsShell.Format.ps1xml
│   ├── DnsShell.psd1
│   └── en-US
│       └── DnsShell.dll-help.xml
├── dns.yml  # ansible 使用脚本
├── hosts  # 客户机ip存放文件
└── roles
     └── dns
         ├── files
         │   └── dns.ps1  # PowerShell  脚本
         └── tasks
             └── main.yml  # ansible 使用脚本
1
2
3
4
5
6
7
8
9
10
dns.yml 说明:
- hosts: dns  # 执行客户机列表 可设置变量
   vars:  #默认参数
     parameter: dnszone  #创建主域 (dnszone 添加主域名 A 记录 创建已有域名A 记录 CNAME 添加域名cname 记录 delete 删除域名记录 update 更新域名 )
     Domain:  test .com  # test.com
     name: A  # a记录
     ipaddress: 192.168.1.1  # 记录ip
     ipaddress2: 192.168.1.1   # 修改域名才生效
   roles: 
     - dns  # ansible 执行任务
1
2
3
4
5
6
7
8
main.yml 说明:
- name: sc dns
   script: dns.ps1 {{parameter}} {{Domain}} {{name}} {{ipaddress}} {{ipaddress2}}
   #参数说明parameter 执行标签判断 例如:添加域名,删除域名,修改域名等
   Domain 要操作的域名
   name 域名记录 a记录 mx记录等
   ipaddress a记录的IP cname 域名
   ipaddress2  修改域名解析使用
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
dns.ps1 说明
# ************************************************************ 
# *                                                          * 
# *                 Powershell部署DNS脚本                    * 
# *                                                          * 
# ************************************************************ 
#set-executionpolicy remotesigned 
# 修改dns所用参数
################################################################################################# 
#参数申明
param($a,$b,$c,$d,$e)
$parameter=$a
$Domain=$b
$name=$c
$ipaddress=$d
$ipaddress2=$e
#################################################################################################
import -module dnsshell  # 导入dnsshell 模块
##创建主域
if  ($parameter - eq  "dnszone"
{
new-dnszone -zonename $Domain -zonetype primary
}
## 添加A记录
elseif ($parameter - eq  "A" )
{
new-dnsrecord -zonename $Domain -name $name -recordtype A -ipaddress $ipaddress
}
##添加CNAME记录
elseif ($parameter - eq  "CNAME" )
{
new-dnsrecord -zonename  $Domain -name $name -recordtype CNAME - hostname  $ipaddress
}
## 删除dns记录
elseif ($parameter - eq  "delete" )
{
$ARecord = Get-WmiObject -Namespace root\MicrosoftDNS -class MicrosoftDNS_ResourceRecord -filter  "containername='$Domain' AND OwnerName='$name.$Domain' and RecordData='$ipaddress'"
$ARecord.delete()
}
## 更新dns记录
elseif ($parameter - eq  "update" )
{
$ARecord = Get-WmiObject -Namespace root\MicrosoftDNS -class MicrosoftDNS_ResourceRecord -filter  "containername='$Domain' AND OwnerName='$name.$Domain' and RecordData='$ipaddress'"
$ARecord.Modify($ARecord.TTL, "$ipaddress2" )
}
else
{
exit
}

其它说明:

1
2
3
4
5
6
7
8
9
10
11
12
添加主域名:ansible-playbook -i hosts dns.yml -verbose --extra-vars  "parameter=dnszone Domain=test.com"
添加 A记录
ansible-playbook -i hosts dns.yml -verbose --extra-vars  "parameter=A Domain=test.com name=www ipaddress=192.168.1.1"
添加 cname记录
ansible-playbook -i hosts dns.yml -verbose --extra-vars "parameter=cname Domain= test .com name= test  ipaddress=
删减记录:
  ansible-playbook -i hosts dns.yml -verbose --extra-vars  "parameter=deleteDomain=test.com name=www ipaddress=192.168.1.1"
   ansible-playbook -i hosts dns.yml -verbose --extra-vars "parameter=delete Domain= test .com name= test  ipaddress=www. test .com.  # 点不能取消 
  更新记录
  ansible-playbook -i hosts dns.yml -verbose --extra-vars  "parameter=update Domain=test.com name=www ipaddress=192.168.1.1 ipaddress2=192.168.1.2"
  ansible-playbook -i hosts dns.yml -verbose --extra-vars  "parameter=update Domain=test.com name=test ipaddress=www.test.com. ipaddress2=www.test2www.test.com.com."
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值