设为首页 收藏本站
查看: 1251|回复: 0

[经验分享] Puppet数据类型中[数值类型,数组的使用] (十四)

[复制链接]

尚未签到

发表于 2017-10-31 16:08:24 | 显示全部楼层 |阅读模式
本文主要写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
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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"
    }
}



wKioL1nIdR7SduRWAADQxrm7Wlk682.png
agent端测试:
1
2
3
4
5
6
7
8
9
10
[iyunv@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
[iyunv@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
[iyunv@sh-web1 ~]# cat 2.pp
$foo=['one',{'two' => 2,'three' => 3}]
notice ($foo[1][two])



agent端测试更新:
1
2
3
4
[iyunv@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






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-406333-1-1.html 上篇帖子: Puppet File资源介绍(贰拾) 下篇帖子: Puppet数据类型中哈希类型和布尔类型(十五)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表