寫在前面
- 學(xué)習(xí)遇到 DNS 自動化部署的一個 Ansible 劇本,這里分享給小伙
- 部署使用
Bind9
,包括主從 DNS 構(gòu)建,緩存 DNS 構(gòu)建,緩存使用unbound
- 劇本相對簡單
- 理解不足小伙伴幫忙指正
對每個人而言,真正的職責(zé)只有一個:找到自我。然后在心中堅守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是對大眾理想的懦弱回歸,是隨波逐流,是對內(nèi)心的恐懼 ——赫爾曼·黑塞《德米安》
部署B(yǎng)IND 流程
- 安裝bind軟件包
- 創(chuàng)建BIND配置文件
- 啟用并啟動unbound服務(wù)。
- 配置防火墻以允許入站DNS通信.
涉及到的文件:
[student@workstation dns-auto]$ tree .
.
├── ansible.cfg
├── ansible-starter-files
│ └── configure_primary.yml.starter
├── files
│ ├── primary-192.168.0.zone
│ ├── primary-backend.lab.example.com.zone
│ ├── primary-named.backend.conf
│ ├── primary-named.conf
│ ├── secondary-named.backend.conf
│ └── secondary-named.conf
├── inventory
└── templates
└── unbound.conf.j2
ansible 配置文件
[student@workstation dns-auto]$ cat ansible.cfg
[defaults]
inventory=./inventory
remote_user=devops
[privilege_escalation]
become = False
become_method = sudo
become_user = root
become_ask_pass = False
主機清單文件,可以看到,當(dāng)前 DNS 部署,使用主從DNS結(jié)構(gòu),并且部署了 緩存 DNS 服務(wù)
[student@workstation dns-auto]$ cat inventory
[control_node]
workstation.lab.example.com
[caching_dns]
servera.lab.example.com
[primary_dns]
serverb.lab.example.com
[secondary_dns]
serverc.lab.example.com
[student@workstation dns-auto]$
主 DNS 相關(guān)配置
- 提權(quán)為 root
- 下載 bind9
- 復(fù)制配置文件
- 復(fù)制 zone 文件
- 需要注意 配置文件和 zone 文件的 權(quán)限,root 用戶,namde 組,0640
- 配置防火墻
- 設(shè)置開機自啟
- 添加通知,在配置文件變更時重啟服務(wù)
[student@workstation dns-auto]$ cat configure_primary.yml
---
- name: Configure primary nameserver
hosts: primary_dns
remote_user: devops
become: yes
tasks:
- name: Install BIND9
yum:
name: bind
state: present
- name: Copy primary config file
copy:
src: files/primary-named.conf
dest: /etc/named.conf
owner: root
group: named
mode: 0640
notify:
- reload_named
- name: Copy forward zone file to primary
copy:
src: files/primary-backend.lab.example.com.zone
dest: /var/named/backend.lab.example.com.zone
owner: root
group: named
mode: 0640
notify:
- reload_named
- name: Copy reverse zone file to primary
copy:
src: files/primary-192.168.0.zone
dest: /var/named/192.168.0.zone
owner: root
group: named
mode: 0640
notify:
- reload_named
- name: Copy backend config file (for zones)
copy:
src: files/primary-named.backend.conf
dest: /etc/named.backend.conf
owner: root
group: named
mode: 0640
notify:
- reload_named
- name: Allow dns service on firewall
firewalld:
service: dns
state: enabled
immediate: yes
permanent: yes
- name: Ensure named is running and enabled
service:
name: named
state: started
enabled: yes
handlers:
- name: reload_named
service:
name: named
state: reloaded
[student@workstation dns-auto]$
主DNS 對應(yīng)的 配置:
- 監(jiān)聽任意 53 端口
- 允許同步的從服務(wù)器 IP 聲明
- 允許進行 DSN 解析的 IP 聲明
- 不允許進行 遞歸
[student@workstation dns-auto]$ cat files/primary-named.conf
# /etc/named.conf (primary/secondary)
#
# For this exercise, primary and secondary name.conf files are identical but
# have separate names in the project directory to avoid confusion when
# configuring playblooks.
#
# Template file for BIND labs.
options {
listen-on port 53 { any; };
directory "/var/named";
allow-transfer { 192.168.0.12; };
allow-query { localhost; 172.25.250.254; 192.168.0.0/24; };
recursion no;
#Added the following
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
/* https://fedoraproject.org/wiki/Changes/CryptoPolicy */
include "/etc/crypto-policies/back-ends/bind.config";
};
#Added the following
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
include "/etc/named.backend.conf";
[student@workstation dns-auto]$
正向解析 zone 文件
[student@workstation dns-auto]$ cat files/primary-backend.lab.example.com.zone
$TTL 300
@ IN SOA serverb.backend.lab.example.com. root.serverb.backend.lab.example.com. (
2020041806 ;serial number
1H ;refresh secondary
5m ;retry refresh
1w ;expire zone
1m ) ;cache time-to-live for negative answers
; owner TTL CL type RDATA
600 IN NS serverb
; IN MX 10 serverb.backend.lab.example.com.
; IN A 192.168.0.11
servera IN A 192.168.0.10
serverb IN A 192.168.0.11
serverc IN A 192.168.0.12
serverd IN A 192.168.0.13
[student@workstation dns-auto]$
反向解析 zone 文件
[student@workstation dns-auto]$ cat files/primary-192.168.0.zone
$TTL 300
@ IN SOA serverb.backend.lab.example.com. root.serverb.backend.lab.example.com. (
2020041805 ;serial number
1H ;refresh secondary
5M ;retry refresh
1W ;expire zone
1M ) ;cache time-to-live for negative answers
; owner TTL CL type RDATA
600 IN NS serverb.backend.lab.example.com.
10.0.168.192.IN-ADDR.ARPA. IN PTR servera.backend.lab.example.com.
11 IN PTR serverb.backend.lab.example.com.
12 IN PTR serverc.backend.lab.example.com.
13 IN PTR serverd.backend.lab.example.com.
[student@workstation dns-auto]$
添加的 zone 對應(yīng)的 配置文件
[student@workstation dns-auto]$ cat files/primary-named.backend.conf
zone "backend.lab.example.com" IN {
type master;
file "backend.lab.example.com.zone";
forwarders {};
};
zone "0.168.192.in-addr.arpa" IN {
type master;
file "192.168.0.zone";
forwarders {};
};
[student@workstation dns-auto]$
從 DNS 相關(guān)配置
劇本步驟和主的基本一致
[student@workstation dns-auto]$ cat configure_secondary.yml
---
- name: Configure secondary nameserver
hosts: secondary_dns
remote_user: devops
become: yes
tasks:
- name: Install BIND9
yum:
name: bind
state: present
- name: Copy secondary config file
copy:
src: files/secondary-named.conf
dest: /etc/named.conf
owner: root
group: named
mode: 0640
notify:
- reload_named
- name: Copy backend config file (for zones)
copy:
src: files/secondary-named.backend.conf
dest: /etc/named.backend.conf
owner: root
group: named
mode: 0640
notify:
- reload_named
- name: Allow dns service on firewall
firewalld:
service: dns
state: enabled
immediate: yes
permanent: yes
- name: Ensure named is running and enabled
service:
name: named
state: started
enabled: yes
handlers:
- name: reload_named
service:
name: named
state: reloaded
[student@workstation dns-auto]$
對應(yīng)的 zone 文件和 配置文件
[student@workstation dns-auto]$ cat files/secondary-named.backend.conf
zone "backend.lab.example.com" IN {
type slave;
file "slaves/backend.lab.example.com.zone";
masters { 192.168.0.11; };
};
zone "0.168.192.in-addr.arpa" IN {
type slave;
file "slaves/192.168.0.zone";
masters { 192.168.0.11; };
};
[student@workstation dns-auto]$
[student@workstation dns-auto]$ cat files/secondary-named.conf
# /etc/named.conf (primary/secondary)
#
# For this exercise, primary and secondary name.conf files are identical but
# have separate names in the project directory to avoid confusion when
# configuring playblooks.
#
# Template file for BIND labs.
options {
listen-on port 53 { any; };
directory "/var/named";
allow-transfer { 192.168.0.12; };
allow-query { localhost; 172.25.250.254; 192.168.0.0/24; };
recursion no;
#Added the following
pid-file "/run/named/named.pid";
session-keyfile "/run/named/session.key";
/* https://fedoraproject.org/wiki/Changes/CryptoPolicy */
include "/etc/crypto-policies/back-ends/bind.config";
};
#Added the following
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";
include "/etc/named.backend.conf";
[student@workstation dns-auto]$
緩存 DNS 相關(guān)配置
- 涉及變量較多,所以這里我們使用了 模板文件的方法
- 裝包
- 生成配置文件
- 配置防火墻
- 啟動服務(wù)設(shè)置開機自啟
[student@workstation dns-auto]$ cat configure_caching.yml
---
- name: Install cache only nameserver
hosts: caching_dns
remote_user: devops
become: yes
vars:
interface: 0.0.0.0
interface_automatic: "yes"
access_control:
- "172.25.250.0/24 allow"
domain_insecure: example.com
forward_zone_name: .
forward_zone_addr: "172.25.250.254"
tasks:
- name: Install cache only nameserver
yum:
name: unbound
state: present
- name: Create configuration file on caching server host
template:
src: unbound.conf.j2
dest: /etc/unbound/conf.d/unbound.conf
- name: Allow dns service on firewall
firewalld:
service: dns
state: enabled
immediate: yes
permanent: yes
- name: Ensure unbound is running and enabled
service:
name: unbound
state: started
enabled: yes
handlers:
- name: restart_unbound
service:
name: unbound
state: restarted
[student@workstation dns-auto]$
[student@workstation dns-auto]$ cat templates/unbound.conf.j2
server:
interface: {{ interface }}
interface-automatic: {{ interface_automatic }}
{% for acl in access_control %}
access-control: {{ acl }}
{% endfor %}
domain-insecure: "{{ domain_insecure }}"
forward-zone:
name: "{{ forward_zone_name }}"
forward-addr: {{ forward_zone_addr }}
[student@workstation dns-auto]$
整個劇本涉及下面三個劇本
[student@workstation dns-auto]$ cat playbook.yml
---
- import_playbook: configure_primary.yml
- import_playbook: configure_secondary.yml
- import_playbook: configure_caching.yml
[student@workstation dns-auto]$
博文部分內(nèi)容參考
? 文中涉及參考鏈接內(nèi)容版權(quán)歸原作者所有,如有侵權(quán)請告知,這是一個開源項目,如果你認(rèn)可它,不要吝嗇星星哦 ??
https://www.isc.org/bind/
<RH358 授課課堂筆記>文章來源:http://www.zghlxwxcb.cn/news/detail-648427.html
? 2018-2023 liruilonger@gmail.com, All rights reserved. 保持署名-非商用-相同方式共享(CC BY-NC-SA 4.0)文章來源地址http://www.zghlxwxcb.cn/news/detail-648427.html
到了這里,關(guān)于DNS:自動化配置 主/從/緩存 DNS服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!