# RHEL 7 and later and Fedora 15 and later require the iptables-services
# package, which provides the /usr/libexec/iptables/iptables.init used by
# lib/puppet/util/firewall.rb.
if ($::operatingsystem != 'Amazon')
and (($::operatingsystem != 'Fedora' and versioncmp($::operatingsystemrelease, '7.0') >= 0)
or ($::operatingsystem == 'Fedora' and versioncmp($::operatingsystemrelease, '15') >= 0)) {
service { 'firewalld':
ensure => stopped,
enable => false,
before => Package[$package_name],
}
}
if $package_name {
package { $package_name:
ensure => $package_ensure,
before => Service[$service_name],
}
}
if ($::operatingsystem != 'Amazon')
and (($::operatingsystem != 'Fedora' and versioncmp($::operatingsystemrelease, '7.0') >= 0)
or ($::operatingsystem == 'Fedora' and versioncmp($::operatingsystemrelease, '15') >= 0)) {
...
# Redhat 7 selinux user context for /etc/sysconfig/iptables is set to unconfined_u
case $::selinux {
#lint:ignore:quoted_booleans
'true',true: {
case $::operatingsystemrelease {
/^(6|7)\..*/: { $seluser = 'unconfined_u' }
default: { $seluser = 'system_u' }
}
}
#lint:endignore
default: { $seluser = undef }
}
file { "/etc/sysconfig/${service_name}":
ensure => present,
owner => 'root',
group => 'root',
mode => '0600',
seluser => $seluser,
}
}
在上述代码中,需要理解以下新知识点:
第一点,versioncmp函数用于比较两个版本号并返回比较结果,例如:
$result = versioncmp(a, b)
a大于b,返回1
a等于b,返回0
a小于b,返回-1
第二点,要理解运算的优先级顺序,在上述代码出现了一段比较复杂的条件语句:
if ($::operatingsystem != 'Amazon')
and (($::operatingsystem != 'Fedora' and versioncmp($::operatingsystemrelease, '7.0') >= 0)
or ($::operatingsystem == 'Fedora' and versioncmp($::operatingsystemrelease, '15') >= 0))
首先()的优先级最高,因此以下表达式会优先进行计算:
($::operatingsystem != 'Amazon')
(($::operatingsystem != 'Fedora' and versioncmp($::operatingsystemrelease, '7.0') >= 0))
($::operatingsystem == 'Fedora' and versioncmp($::operatingsystemrelease, '15') >= 0))
其次,==的优先级等于!=高于>=高于and。 最后是最外层的and/or运算if statement1 and statement2 or statement 3,那么其运算顺序是哪一种?
case $facts['name'] {
'A': { include role::case1 }
'B', 'C': { include role::case2 }
/^(D|E)$/: { include role::case3 }
default: { include role::default_case }
}
2.2 type firewall
资源类型firewall用于管理防火墙规则,以下举例说明如何在真实环境中使用该类型:
2.2.1 为apache开启80和443端口
firewall { '100 allow http and https access':
dport => [80, 443],
proto => tcp,
action => accept,
}
2.2.2 丢弃FIN/RST/ACK包如果没有对应的SYN包
firewall { '002 drop NEW external website packets with FIN/RST/ACK set and SYN unset':
chain => 'INPUT',
state => 'NEW',
action => 'drop',
proto => 'tcp',
sport => ['! http', '! 443'],
source => '! 10.0.0.0/8',
tcp_flags => '! FIN,SYN,RST,ACK SYN',
}