puppet支持标准的ruby正则表达式.
正则表达式的参数匹配项:
[]:用于描述范围(如[a-z],表示范围a~z之间.)
():用于包含正则表达式.
\w:用于描述字母或数字,相当于[0-9a-zA-Z]
\W:非字母或数字.
\s:匹配[\t\n\r\f],其中(\t)为制表符、(\r)为回车符、(\n)为换行符、(\f)为换页符,\s表示匹配这些符号的简写方式.
\S:匹配非空字符.
\d:匹配[0-9]数字.
\D:匹配非数字
\b:匹配退格符.
\B:非字边界.
*:前面元素出现0次或多次.
+: 前面元素出现1次或多次.
{m,n}:前面元素最少出现m次,最多出现n次.
?:前面元素最多出现1次,等价于{0,1}.
|:与前面或后面表达式匹配.
#系统facter变量:
1
2
3
4
|
[root@sh-web1 ~]
# facter | grep oper*
operatingsystem => CentOS
operatingsystemmajrelease => 6
operatingsystemrelease => 6.5
|
判断系统发行版本,安装对应的软件包.
1
2
3
4
5
6
7
8
|
[root@sh-web1 ~]
# cat apache.pp
$packages = $operatingsystem ? {
/(?i-mx:centos|redhat)/=>
'httpd'
,
/(?i-mx:ubuntu|debin)/=>
'apache2'
,
}
package {
"$packages"
:
ensure=> present,
}
|
本地应用puppet代码.
1
2
3
4
5
6
|
[root@sh-web1 ~]
# puppet apply apache.pp
Notice: Compiled catalog
for
sh-web1.localdomain
in
environment production
in
0.04 seconds
Notice:
/Stage
[main]
/Main/Package
[httpd]
/ensure
: created
Notice: Finished catalog run
in
2.31 seconds
[root@sh-web1 ~]
# rpm -qa httpd
httpd-2.2.15-60.el6.centos.5.x86_64
|
i:表示忽略大小写.
m:表示把"."当做换行符使用.
-:表示不使用某转移符号.
X:表示忽略模式中的空白字符串和注释.
其他案例:
修改文件权限,用户组等都可以:
1
2
3
4
5
6
7
8
9
|
$allgroup = $operatingsystem ? {
/(?i-mx:ubuntu|debin)/ =>
'user1'
,
default =>
'root'
,
}
file
{
"/etc/passwd"
:
ensure =>
file
,
owner =>
'root'
,
group =>
"$allgroup"
,
}
|
undef
undef是一个特殊的值,它等同于ruby语言中的nil,没有声明过但是仍然可以使用的值就属于undef值,undef用法.
1
2
3
4
5
6
7
8
9
10
|
$packages = $operatingsystem ? {
redhat =>
'httpd'
,
centos =>
'httpd'
,
#/(?i-mx:ubuntu|debin)/ => 'apache2',
/(?i):(ubuntu|debin)/ =>
'apache2'
,
default =>
'undef'
,
}
package {
"$packages"
:
ensure => present,
}
|
注释;如果没有匹配到任何操作系统版本,默认值为undef。
本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1969381