本文主要写puppet的数据类型中的数值类型和数组的使用,博主puppet为3.8版本,puppet数组的追加功能测试没有成功,官网也没有给出示例,确定是否已经优化或者取消.官网数据类型连接地址
https://docs.puppet.com/puppet/3.8/lang_datatypes.html
puppet的数据类型
目前puppet支持字符串类型、数字、数组、哈希、布尔型、undef和正则表达式.
一、字符串类型
定义字符串需要以双引号("")或单引号('')进行声明,在puppet中默认的数据类型就是字符串类型.
字符窜类型声明注意:
1、不能使用puppet关键字.
2、字符串类型由字母[a-z][A-Z]、数字[0-9]、连接符(-)和下划线(_)组成.
3、建议字符串类型使用utf-8的字符集.
示例(上篇文章的变量定义):
1
2
|
$content=
"this is file"
$content=
'this is file'
|
注释:单双引号都可以.
puppet中通过单双引号声明的字符串中,有些特殊字符串定义尾部含有"\",需要使用反斜杠屏蔽字符特殊意义.
如path => c:\data\ #尾部有'\',需要转义.
示例如下:
1
2
|
$content=
'c:\data\\'
notify {
"hostip is $content"
:}
|
注意:$content='c:\data\\'后如果尾部没有反斜杠'\'转义,会报语法错误.
常见的需要转义的符号:
1
|
\$,\",\',\\,\n,\r,\t,\s
|
二、数值类型
puppet中支持数值类型,数值类型是指定义成的数值形式的数据,这种数据可以进行加、减、乘、除、与、或、取反等数学运算.在puppet中数值类型包括整数类型、浮点数,当然也支持整数类型运算和浮点数类型运算.
整数类型的计算:
加法运算:
1
2
|
$content=7 + 4
notify {
"hostip is $content"
:}
|
agent端更新:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 ~]
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-web1.localdomain
Info: Applying configuration version
'1505927717'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice: hostip is 11
Notice:
/Stage
[main]
/Main/Node
[sh-proxywebd]
/Notify
[hostip is 11]
/message
: defined
'message'
as
'hostip is 11'
Notice: Finished catalog run
in
0.26 seconds
|
乘法运算:
1
2
|
$content=7 * 4
notify {
"hostip is $content"
:}
|
agent端更新:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 ~]
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-web1.localdomain
Info: Applying configuration version
'1505927786'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice: hostip is 28
Notice:
/Stage
[main]
/Main/Node
[sh-proxywebd]
/Notify
[hostip is 28]
/message
: defined
'message'
as
'hostip is 28'
Notice: Finished catalog run
in
0.29 seconds
|
取余运算:
1
2
|
$content=7 % 4
notify {
"hostip is $content"
:}
|
agent端更新:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 ~]
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-web1.localdomain
Info: Applying configuration version
'1505927969'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice: hostip is 3
Notice:
/Stage
[main]
/Main/Node
[sh-proxywebd]
/Notify
[hostip is 3]
/message
: defined
'message'
as
'hostip is 3'
Notice: Finished catalog run
in
0.27 seconds
|
除法运算:
1
2
|
$content=7 / 4
notify {
"hostip is $content"
:}
|
agent端更新:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 ~]
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-web1.localdomain
Info: Applying configuration version
'1505928069'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice: hostip is 1
Notice:
/Stage
[main]
/Main/Node
[sh-proxywebd]
/Notify
[hostip is 1]
/message
: defined
'message'
as
'hostip is 1'
Notice: Finished catalog run
in
0.29 seconds
|
浮点数类型运算:
1
2
|
$content=7 + 0.4
notify {
"hostip is $content"
:}
|
agent端更新:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 ~]
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-web1.localdomain
Info: Applying configuration version
'1505927886'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice: hostip is 7.4
Notice:
/Stage
[main]
/Main/Node
[sh-proxywebd]
/Notify
[hostip is 7.4]
/message
: defined
'message'
as
'hostip is 7.4'
Notice: Finished catalog run
in
0.27 seconds
|
注意:符号不能同时使用;浮点型数据以数字加小数点的形式表达.
错误的示例:
1
2
|
$content=7 / .4
notify {
"hostip is $content"
:}
|
1
2
|
$content=7 / +4
notify {
"hostip is $content"
:}
|
三、数组
数组的定义:
数组就是将同一类事物按照一定的顺序放到一个集合中,通过定义这个集合来完成对数组中所有的事物的定义,puppet中数组通过方括号来定义,数组中的数据通过","分隔.
puppet通过["value1","value2"]的形式来定义数组,并将定义后的数组赋值给变量.
注意:下面示例包含两种正确写法和一种错误写法,网络有很多博文都在传错误的写法,不知道他们是照抄还是都没自己去写写.
示例:
正确写法(一)
这种算中规中矩的写法,网上很多这样的例子.
1
2
3
4
5
|
class php {
package {[
"php"
,
"php-devel"
]:
ensure=>
"installed"
}
}
|
agent端测试:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 yum.repos.d]
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-web1.localdomain
Info: Applying configuration version
'1506074734'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice:
/Stage
[main]
/Php/Package
[php]
/ensure
: created
Notice:
/Stage
[main]
/Php/Package
[php-devel]
/ensure
: created
Notice: Finished catalog run
in
12.77 seconds
|
正确写法(二)
下面这种写法也是突发奇想,$packages既然等于数组的元素,那么下面再加[],就又是数组了,这样总是对的吧,测试果然可行.
1
2
3
4
5
6
|
class php {
$packages=[
"php"
,
"php-devel"
,]
package {[$packages]:
ensure=>
"installed"
}
}
|
agent端更新:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 yum.repos.d]
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-web1.localdomain
Info: Applying configuration version
'1506076818'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Notice:
/Stage
[main]
/Php/Package
[php]
/ensure
: created
Notice:
/Stage
[main]
/Php/Package
[php-devel]
/ensure
: created
Notice: Finished catalog run
in
31.33 seconds
|
错误写法:网络很多数组的定义都是下面这种写法,官网并没有给出数组的调用案例只给了定义数组.
声明php类,安装php软件包.
1
2
3
4
5
6
|
class php {
$packages = [
"php"
,
"php-devel"
]
package {
"$packages"
:
ensure=>
"installed"
}
}
|
agent端测试:
1
2
3
4
5
6
7
8
9
10
|
[root@sh-web1 yum.repos.d]
# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog
for
sh-web1.localdomain
Info: Applying configuration version
'1506077242'
Notice:
/Stage
[main]
/Admin/Exec
[selinux]
/returns
: executed successfully
Error: Execution of
'/usr/bin/yum -d 0 -e 0 -y list phpphp-devel'
returned 1: Error: No matching Packages to list
Error:
/Stage
[main]
/Php/Package
[phpphp-devel]
/ensure
: change from absent to present failed: Execution of
'/usr/bin/yum -d 0 -e 0 -y list phpphp-devel'
returned 1: Error: No matching Packages to list
Notice: Finished catalog run
in
1.01 seconds
|
注意报错:puppet把php和php-devel连一起了phpphp-devel.
puppet数组的取值
puppet数组和其他编程语言中的数组一样,下标从0开始.可以将数组赋值给变量,并通过[数组下标]的方式进行取值.
1
2
3
|
# cat 2.pp
$foo=[
'one'
,
'two'
,
'three'
]
notice ($foo[1])
#通过notice函数打印$foo数组two的值.
|
本地应用:
1
2
3
4
|
[root@sh-web1 ~]
# puppet apply 2.pp
Notice: Scope(Class[main]): two
Notice: Compiled catalog
for
sh-web1.localdomain
in
environment production
in
0.02 seconds
Notice: Finished catalog run
in
0.01 seconds
|
嵌套数组
puppet通过嵌套数组来模拟多维数组,并通过索引的方式访问嵌套数组的值.
通过notice函数打印$foo数组索引为1的two的值.
1
2
3
|
[root@sh-web1 ~]
# cat 2.pp
$foo=[
'one'
,{
'two'
=> 2,
'three'
=> 3}]
notice ($foo[1][two])
|
agent端测试更新:
1
2
3
4
|
[root@sh-web1 ~]
# puppet apply 2.pp
Notice: Scope(Class[main]): 2
Notice: Compiled catalog
for
sh-web1.localdomain
in
environment production
in
0.02 seconds
Notice: Finished catalog run
in
0.02 seconds
|