我们有一个 puppet 模块(v3.6.2,因为我们将它用于 Satellite 6)
该模块按预期工作,但向区域添加多个源时除外。它将添加区域,然后添加一个源,然后在尝试将第二个源添加到区域时出错,并显示以下消息:
INVALID_ZONE: backup
第二次运行该模块成功添加了源 2 和 3。
该区域已成功创建并且防火墙重新加载正在触发,但几乎就像它没有完成重新加载一样,因为它没有看到新添加的“备份”区域对于第二和第三个源有效。
模块代码:
class firewalld(
$enabled = true,
$package_name = 'firewalld',
$service_name = 'firewalld',
$config_dir = '/etc/firewalld',
$zone_create = [],
$zone_remove = [],
$zone_set_default = '',
$zone_add_source = hiera_hash('firewalld::zone_add_source', { }),
$zone_add_service = hiera_hash('firewalld::zone_add_service', { }))
{
if $enabled {
$service_ensure = 'running'
$service_enable = true
$package_ensure = 'present'
$config_ensure = 'present'
Package["$package_name"] -> File["$config_dir"]
File["$config_dir"] -> Service["$service_name"]
} else {
$service_ensure = 'stopped'
$service_enable = false
$package_ensure = 'absent'
$config_ensure = 'absent'
Service["$service_name"] -> File["$config_dir"]
File["$config_dir"] -> Package["$package_name"]
}
package { "$package_name":
ensure => $package_ensure,
}
file { "$config_dir":
ensure => $config_ensure,
force => true,
}
service { "$service_name":
ensure => $service_ensure,
enable => $service_enable,
hasrestart => true,
hasstatus => true,
}
exec { 'firewalld_reload':
onlyif => 'systemctl -q is-enabled firewalld.service',
path => '/bin:/usr/bin:/sbin:/usr/sbin',
# command => "systemctl restart firewalld.service",
command => "firewall-cmd --reload",
refreshonly => true,
}
define firewalld_zone_create() {
exec { "firewalld_zone_create_${name}":
path => '/bin:/usr/bin:/sbin:/usr/sbin',
command => "firewall-cmd --permanent --new-zone=${name}",
unless => "firewall-cmd --permanent --get-zones | grep -qw ${name}",
notify => Exec['firewalld_reload'],
require => Service['firewalld'],
}
}
define firewalld_zone_add_source($zone, $source) {
exec { "firewalld_${zone}_add_source_${source}":
path => '/bin:/usr/bin:/sbin:/usr/sbin',
command => "firewall-cmd --permanent --zone=${zone} --add-source=${source}",
unless => "firewall-cmd -q --permanent --zone=${zone} --query-source=${source}",
notify => Exec['firewalld_reload'],
require => Service['firewalld'],
}
}
if $enabled {
firewalld_zone_create{ $zone_create: } -> firewalld_zone_set_default_zone{ $zone_set_default: }
create_resources('firewalld_zone_add_service', $zone_add_service)
create_resources('firewalld_zone_add_source', $zone_add_source)
}
}
我删掉了定义添加端口/目标等的部分,因为它太长了。
我使用的输入是
class { 'firewalld':
enabled => true,
zone_create => ['zone1', 'mgmt', 'backup'],
zone_add_service => {
'001' => { 'zone' => 'mgmt', 'service' => 'ssh' },
},
zone_add_source => {
'001' => { 'zone' => 'mgmt', 'source' => 'INT.x.x.x/24' },
'002' => { 'zone' => 'mgmt', 'source' => 'INT.x.x.x/24' },
'003' => { 'zone' => 'mgmt', 'source' => 'INT.x.x.0/24' },
'004' => { 'zone' => 'backup', 'source' => 'IP.1.x.x/24' },
'005' => { 'zone' => 'backup', 'source' => 'IP.2.x.0/24' },
'006' => { 'zone' => 'backup', 'source' => 'IP.3.x.0/24' },
},
zone_set_default => 'zone1',
}
出于安全目的,我更改了子网和区域名称。
如果有人能告知为什么会发生这种行为以及如何解决它,我将不胜感激。
注意:我尝试了firewall-cmd --reload 和 systemctl restartfirewalld.service 并得到了相同的结果。
欢呼吧,阿米莉亚
答案1
似乎应该创建区域前添加源,因此将此依赖项声明为资源引用:
define firewalld_zone_add_source($zone, $source) {
exec { "firewalld_${zone}_add_source_${source}":
path => '/bin:/usr/bin:/sbin:/usr/sbin',
command => "firewall-cmd --permanent --zone=${zone} --add-source=${source}",
unless => "firewall-cmd -q --permanent --zone=${zone} --query-source=${source}",
notify => Exec['firewalld_reload'],
require => Service['firewalld'],
require => Exec['firewalld_zone_create_${zone}'], # <- run after zone create
}
}