RH358管理DHCP和IP地址分配–自动化DHCP配置
本章节介绍使用Ansible配置DHCP服务器和客户端。
专栏地址:https://blog.csdn.net/qq_41765918/category_11532281.html
文章目录
1. 使用Ansible部署DHCP服务器
使用Ansible部署DHCP服务器遵循一个标准流程。
安装包
使用yum Ansible模块安装dhcp-server包如下:
- name: the dhcp-server package is installed
yum:
name: dhcp-server
state: present
部署DHCP配置文件
DHCPv4的配置文件(/etc/dhcp/dhcpd.conf)和DHCPv6的配置文件(/etc/dhcp/dhcpd6 .conf)可由多个文件模块配置。
使用copy模块准备文件并复制到DHCP服务器。使用模板模块创建一个模板配置文件,Ansible可以在部署过程中使用Ansible变量和事实自动定制这个模板配置文件。
以下任务使用copy模块将本地的dhcpd.conf文件部署到dhcp服务器的/etc/ dhcp/dhcpd.conf文件中。
- name: the DHCPv4 configuration file is deployed
copy:
src: dhcpd.conf
dest: /etc/dhcp/dhcpd.conf
owner: root
group: root
mode: '0644'
setype: dhcp_etc_t
notify: reload dhcpd
启用和启动服务
使用Ansible service模块开启和启动服务。DHCPv4的服务名称为dhcpd。对于DHCPv6,服务名称为dhcpd6。
- name: the dhcpd and dhcpd6 services are enabled and started
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- dhcpd
- dhcpd6
配置防火墙规则
使用Ansible firewalld模块开启DHCPv4的dhcp服务和dhcpv6的dhcp服务。
- name: the dhcp and dhcpv6 firewall services are opened
firewalld:
service: "{{ item }}"
state: enabled
immediate: yes
permanent: yes
loop:
- dhcp
- dhcpv6
2. 使用Ansible配置DHCP Client
对于客户端系统,使用网络系统角色。对于IPv4,将dhcp4变量设置为yes。对于IPv6,将auto6变量设置为yes。
下面以配置DHCPv4和SLAAC客户端系统的网络接口为例进行说明。
- name: make sure serverb is using DHCPv4/SLAAC
hosts: serverb.lab.example.com
vars:
network_connections:
- name: dyn_net
type: ethernet
mac: 52:54:00:01:fa:0b
state: up
ip:
dhcp4: yes
auto6: yes
roles:
- rhel-system-roles.network
3. 课本练习
[student@workstation ~]$ lab dhcp-automation start
在本练习中,您将在服务器上部署DHCP服务器,为连接到辅助网络的系统提供IPv4和IPv6地址支持。
为了支持IPv6, lab命令配置serverd使用邻居发现协议(NDP)提供前缀和默认网关,并指示客户端查询DHCPv6服务器以获得其他配置。
1. 熟悉项目及其现状。
[student@workstation ~]$ cd /home/student/dhcp-automation
[student@workstation dhcp-automation]$ tree
.
├── ansible.cfg
├── dhcp-client.yml
├── dhcp-server.yml
├── files
│ ├── dhcpd6.conf
│ └── dhcpd.conf
├── host_vars
│ ├── servera.lab.example.com
│ ├── serverb.lab.example.com
│ └── serverc.lab.example.com
├── inventory
└── solution
├── dhcp-client.yml
└── dhcp-server.yml
3 directories, 11 files
[student@workstation dhcp-automation]$ cat inventory
[dhcp_servers]
servera.lab.example.com
[clients]
serverb.lab.example.com
serverc.lab.example.com
2. 检查并完成dhcp-server.yml Ansible剧本。
该脚本在服务器上部署、配置和启动DHCP服务器,以在辅助网络上提供对IPv4和IPv6地址的支持。
[student@workstation dhcp-automation]$ vim dhcp-server.yml
---
- name: Deploy a DHCPv4 and DHCPv6 server
hosts: servera.lab.example.com
become: true
vars:
network_connections:
- name: static_net
type: ethernet
mac: "{{ mac_if2 }}"
state: up
ip:
address:
- 192.168.0.10/24
- fde2:6494:1e09:2::a/64
roles:
- rhel-system-roles.network
tasks:
- name: the dhcp-server package is installed
yum:
name: dhcp-server
state: present
- name: the DHCPv4 configuration file is deployed
copy:
src: files/dhcpd.conf
dest: /etc/dhcp/dhcpd.conf
notify: reload dhcpd
- name: the DHCPv6 configuration file is deployed
copy:
src: files/dhcpd6.conf
dest: /etc/dhcp/dhcpd6.conf
notify: reload dhcpd6
- name: the dhcpd and dhcpd6 services are started and enabled
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- dhcpd
- dhcpd6
- name: the dhcp and dhcpv6 firewall services are opened
firewalld:
service: "{{ item }}"
state: enabled
immediate: yes
permanent: yes
loop:
- dhcp
- dhcpv6
handlers:
- name: reload dhcpd
service:
name: dhcpd
state: restarted
- name: reload dhcpd6
service:
name: dhcpd6
state: restarted
3. 执行剧本部署DHCP服务器。
[student@workstation dhcp-automation]$ ansible-playbook --syntax-check dhcp-server.yml
[student@workstation dhcp-automation]$ ansible-playbook dhcp-server.yml
4. 检查并完成dhcp-client.yml Ansible剧本。
在系统上客户端组配置连接到次要网络的网络接口,使用DHCP获得IPv4地址,使用SLAAC和DHCPv6获得IPv6地址。
[student@workstation dhcp-automation]$ cat dhcp-client.yml
---
- name: Configure a DHCPv4 and DHCPv6 network interface
hosts: clients
become: true
vars:
network_connections:
- name: dyn_net
type: ethernet
mac: "{{ mac_if2 }}"
state: up
ip:
dhcp4: yes
auto6: yes
roles:
- rhel-system-roles.network
# Verifying your work by testing the IPv4 and IPv6 configuration
tasks:
- name: the system can connect to servera IPv4 address
wait_for:
host: 192.168.0.10
port: 22
timeout: 10
- name: the system can connect to servera IPv6 address
wait_for:
host: fde2:6494:1e09:2::a
port: 22
timeout: 10
5. 执行剧本。
[student@workstation dhcp-automation]$ ansible-playbook --syntax-check dhcp-client.yml
[student@workstation dhcp-automation]$ ansible-playbook dhcp-client.yml
完成实验。
[student@workstation ~]$ lab dhcp-automation finish
总结
- 介绍如何使用Ansible部署DHCP服务器。
- 使用Ansible部署DHCP客户端。
- 若喜欢金鱼哥的文章,顺手点个赞。也可点个关注,因为后续会不断上干货。