Akemi

使用RHEL System Roles管理网络

2025/03/24

RHEL System Roles 是 Red Hat 企业 Linux(RHEL)官方提供的一组 Ansible 预定义角色,旨在帮助用户以标准化、自动化的方式管理和配置 RHEL 系统的核心功能

角色名称 功能描述
rhel-system-roles.network 配置网络接口(IP 地址、路由、VLAN、绑定等)。
rhel-system-roles.timesync 管理时间同步服务(如 Chrony、NTP)。
rhel-system-roles.selinux 配置 SELinux 模式(Enforcing/Permissive/Disabled)和策略。
rhel-system-roles.storage 管理磁盘分区、LVM、文件系统挂载等存储配置。
rhel-system-roles.postfix 配置 Postfix 邮件服务器。
rhel-system-roles.kdump 配置内核崩溃转储(Kdump)功能。
rhel-system-roles.logging 集中管理日志服务(如 Rsyslog)

roles安装、工作方法

RHEL System Roles通过预定义一组变量调用roles,roles会自动根据变量,去自动进行配置

一般来说,会将其添加至host_vars,来针对不同的主机做不同的配置

1
2
3
4
5
6
yum -y install rhel-system-roles

ansible-galaxy list |grep rhel-system-roles
- linux-system-roles.network, (unknown version) # 社区版
...
- rhel-system-roles.network, (unknown version) # 红帽版

network_connections变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vars:
network_connections:
- name: eth0-profile # nmcli con配置文件名称
type: ethernet/bridge/bond/team/vlan/macvlan
interface_name: eth0 # 根据名称选择接口
autoconnect: true
persistent_state: present/absent # 生成/删除配置文件,默认present
state: up # 启动
zone: # 加到哪个防火墙的zone中
mac: # 根据mac地址匹配接口
ip: # IP地址配置
address:
- "192.168.1.10/24"
gateway4: 192.168.1.1
dns:
- 8.8.8.8
dns_search: # dns搜索域,dns解析时,会加上这段后缀,内网环境使用
- example.com
routes: # 静态路由配置
- network: 10.0.0.0/24
gateway: 192.168.1.254
metric: 100
roles:
- rhel-system-roles.network

通过变量赋予ip

在inventory中设置(不推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[all]
servera ansible_host=10.1.1.1 node_data_address=192.168.1.1/24
serverb ansbile_host=10.1.1.2 node_data_address=192.168.1.2/24

playbook:
---
- name: set network
hosts: all
vars:
network_connections:
- name: eth1
interface_name: eth1
persistent_state: present
type: ethernet
autoconnect: yes
ip:
address:
- "{{ node_data_address }}"
state: up
roles:
- rhel-system-roles.network

在host_vars中设置(推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
vim host_vars/servera
network_connections:
- name: eth1
interface_name: eth1
persistent_state: present
type: ethernet
autoconnect: yes
ip:
address:
- 192.168.1.1/24

vim host_vars/serverb
...

playbook:
---
- name: set network
hosts: all
roles:
- rhel-system-roles.network

通过MAC地址指定网卡

在实际生产环境中,100台服务器不可能全都是同一型号的,肯定有着不同的命名规则,如果ens enp eno这种,此时就需要通过mac地址来指定网卡

一般可以通过BMC/IPMI获取网卡的mac地址

使用mac地址指定网卡来获取

土法寻找

通过loop在ansible_facts中寻找对于mac的interface

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
cat host_vars/servera
mac: "50:00:00:02:00:01"
cat host_vars/serverb
mac: "50:00:00:03:00:03"

---
- name: find interface name
hosts: servera
tasks:
- name: find intface name for specific mac address
set_fact:
the_interface: "{{ item }}"
when:
- ansible_facts[item]['macaddress'] is defined
- ansible_facts[item]['macaddress'] == mac
loop: "{{ ansible_facts['interfaces'] }}"
- debug:
var: the_interface

ansible-playbook network_facts.yml

PLAY [find interface name] ********************************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************************************
ok: [test]

TASK [find intface name for specific mac address] *********************************************************************************************************************************
skipping: [test] => (item=lo)
skipping: [test] => (item=docker0)
skipping: [test] => (item=br-64a3ddc2b328)
skipping: [test] => (item=br-25937a927369)
ok: [test] => (item=eth0)

TASK [debug] **********************************************************************************************************************************************************************
ok: [test] => {
"the_interface": "eth0"
}

PLAY RECAP ************************************************************************************************************************************************************************
test : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

通过RHEL System Roles指定mac的方式修改

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
---
- name: configure network interface
hosts: test
become: true
vars:
target_mac: "bc:24:11:3f:13:8c"
network_connections:
- name: static_net
type: ethernet
mac: "{{ target_mac }}"
state: up
ip:
dhcp4: no
address:
- 192.168.10.177/24
tasks:
- name: run NetworkManager
service:
name: NetworkManager
state: started
enabled: true

- name: Find target interface
set_fact:
the_interface: "{{ item }}"
when:
- ansible_facts[item]['macaddress'] is defined
- ansible_facts[item]['macaddress'] == target_mac
loop: "{{ ansible_facts['interfaces'] }}" # 修正变量名 interface -> interfaces

- name: Debug interface
debug:
var: the_interface

roles:
- rhel-system-roles.network

注:目标主机需要有两个及以上网卡,在inventory中定义的是管理网卡,这里配置的是数据网卡

如果只有一个,在修改完ip之后,由于ip修改,与hosts中对不上,ansible就会丢失连接,体现在tasks运行卡住

CATALOG
  1. 1. roles安装、工作方法
  2. 2. network_connections变量
  3. 3. 通过变量赋予ip
    1. 3.1. 在inventory中设置(不推荐)
    2. 3.2. 在host_vars中设置(推荐)
  4. 4. 通过MAC地址指定网卡
    1. 4.1. 土法寻找
    2. 4.2. 通过RHEL System Roles指定mac的方式修改