以自动化形式配置网口并动态获取ipv4和ipv6地址
前提条件:已经部署完dhcpv4和dhcpv6服务器。
### 介绍下手动配置,如果使用下面playbook自动配置,可以忽略该框框内容。
[root@serverc ~]# rm /etc/machine-id
[root@serverc ~]# systemd-machine-id-setup
[root@serverc ~]# systemctl restart NetworkManager
[root@serverc ~]# nmcli connection add con-name dhcp-conn type ethernet ifname eth1 ipv4.method auto
[root@serverc ~]# nmcli connection up dhcp-conn
[root@serverc ~]# nmcli connection show | grep eth1
dhcp-conn 10e01b13-ad6f-4c34-81b4-c3203bf50c7c ethernet eth1
[root@serverc ~]# uuidgen
770c3835-3877-453e-b47d-7a3480b1bafd
[root@serverc ~]# echo 770c3835-3877-453e-b47d-7a3480b1bafd | \
> sed -e 's/-//g' -e 's/\(..\)/:\1/g' -e 's/^://'
77:0c:38:35:38:77:45:3e:b4:7d:7a:34:80:b1:ba:fd
[root@serverc ~]# nmcli connection modify dhcp-conn ipv6.dhcp-duid '77:0c:38:35:38:77:45:3e:b4:7d:7a:34:80:b1:ba:fd' ipv6.method auto
[root@serverc ~]# nmcli connection up dhcp-conn
4.1 编辑host_vars/serverc.lab.example.com来定义serverc的network_connections变量。使接口动态获取IPv4和IPv6地址。
在host_vars/serverc.lab.example.com的实例中文件中包含以下变量内容。
[student@workstation host_vars]$ vim serverc.lab.example.com
---
network_connections:
# Create the ethernet connection
- name: dhcp-conn
type: ethernet
interface_name: eth1
state: up
ip:
dhcp4: yes
auto6: yes
4.2 在config-network.yml中修改hosts指令。playbook使用ansible清单文件中的servers组包含的serverc。
4.2.1 先来看下这个inventory中的主机配置。
[student@workstation cr-network]$ cat inventory
[control_node]
workstation.lab.example.com
[master_dns]
servera.lab.example.com
[caching_dns]
serverb.lab.example.com
[servers]
servera.lab.example.com
serverb.lab.example.com
serverc.lab.example.com
serverd.lab.example.com
4.2.2 生成的config-network.yml应包含以下内容。
[student@workstation cr-network]$ vim config-network.yml
---
- name: Configure network interface
hosts: servers
become: true
roles:
- rhel-system-roles.network
4.3 运行playbook来配置serverc的第二个网络接口。
[student@workstation cr-network]$ ansible-playbook config-network.yml
4.4 确认在serverc上设置了IPv4和IPv6地址。IPv6地址将从DHCP服务器提供的动态池中选择,并且在您的系统上可能与以下示例不同。
[root@serverc ~]# ip addr show dev eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 52:54:00:01:fa:0c brd ff:ff:ff:ff:ff:ff
inet 192.168.62.12/24 brd 192.168.62.255 scope global dynamic noprefixroute eth1
valid_lft 418sec preferred_lft 418sec
inet6 fc62:5265:6448:6174::60/128 scope global dynamic noprefixroute
valid_lft 421sec preferred_lft 196sec
inet6 fe80::2fd0:b7cd:c5c7:7c50/64 scope link noprefixroute
valid_lft forever preferred_lft forever
4.5 获取serverc的机器ID,以便可以配置DHCPv6服务器,使其始终为该主机提供相同的IPv6地址。在servera上,通过检查DHCPv6服务器的日志来获取serverc的DUID。
[root@servera ~]# journalctl -u dhcpd6.service | grep duid
May 18 15:13:51 servera.lab.example.com dhcpd[26740]: Reply NA: address fc62:5265:6448:6174::60 to client with duid 00:04:d0:04:6b:df:ee:fe:39:6a:a4:f9:72:a7:d4:4a:65:48 iaid = 713252315 valid for 600 seconds
注意:这次输出的IPv6地址是DHCPv6服务器在上步骤运行playbook中分配给serverc的地址。
4.6 编辑/etc/dhcp/dhcpd6.conf文件,在文件末尾添加一个主机部分,将fc62:5265:6448:6174::c地址与在上一步找到的DUID相关联。
authoritative;
subnet6 fc62:5265:6448:6174::/64 {
range6 fc62:5265:6448:6174::20 fc62:5265:6448:6174::60;
option dhcp6.name-servers fc62:5265:6448:6174::b;
option dhcp6.domain-search "pvt.example.net", "lab.example.com", "example.com";
default-lease-time 600;
max-lease-time 7200;
}
host serverc {
host-identifier option
dhcp6.client-id 00:04:d0:04:6b:df:ee:fe:39:6a:a4:f9:72:a7:d4:4a:65:48;
fixed-address6 fc62:5265:6448:6174::c;
}
4.7 运行dhcpd -t -6命令以验证**/etc/dhcp/dhcpd6.conf**的语法。
[root@servera ~]# dhcpd -t -6
4.8 重新启动dhcpd6服务。
[root@servera ~]# systemctl restart dhcpd6
4.9 在serverc上,重新启动dhcp-conn的NetworkManager连接。确认已为网络接口分配了正确的IPv6地址。
[root@serverc ~]# nmcli connection down dhcp-conn
[root@serverc ~]# nmcli connection up dhcp-conn
[root@serverc ~]# ip -6 addr show dev eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 fc62:5265:6448:6174::c/128 scope global dynamic noprefixroute
valid_lft 595sec preferred_lft 370sec
inet6 fe80::2fd0:b7cd:c5c7:7c50/64 scope link noprefixroute
valid_lft forever preferred_lft forever
文章评论