Puppet數據類型中[數值類型,數組的使用] (十四)
本文主要寫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的字符集.
示例(上篇文章的變量定義):
$content="this is file" $content=‘this is file‘
註釋:單雙引號都可以.
puppet中通過單雙引號聲明的字符串中,有些特殊字符串定義尾部含有"\",需要使用反斜杠屏蔽字符特殊意義.
如path => c:\data\ #尾部有‘\‘,需要轉義.
示例如下:
$content=‘c:\data\\‘ notify {"hostip is $content":}
註意:$content=‘c:\data\\‘後如果尾部沒有反斜杠‘\‘轉義,會報語法錯誤.
常見的需要轉義的符號:
\$,\",\‘,\\,\n,\r,\t,\s
二、數值類型
puppet中支持數值類型,數值類型是指定義成的數值形式的數據,這種數據可以進行加、減、乘、除、與、或、取反等數學運算.在puppet中數值類型包括整數類型、浮點數,當然也支持整數類型運算和浮點數類型運算.
整數類型的計算:
加法運算:
$content=7 + 4 notify {"hostip is $content":}
agent端更新:
[[email protected] ~]# 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
乘法運算:
$content=7 * 4 notify {"hostip is $content":}
agent端更新:
[[email protected] ~]# 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
取余運算:
$content=7 % 4 notify {"hostip is $content":}
agent端更新:
[[email protected] ~]# 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
除法運算:
$content=7 / 4 notify {"hostip is $content":}
agent端更新:
[[email protected] ~]# 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
浮點數類型運算:
$content=7 + 0.4 notify {"hostip is $content":}
agent端更新:
[[email protected] ~]# 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
註意:符號不能同時使用;浮點型數據以數字加小數點的形式表達.
錯誤的示例:
$content=7 / .4 notify {"hostip is $content":}
$content=7 / +4 notify {"hostip is $content":}
三、數組
數組的定義:
數組就是將同一類事物按照一定的順序放到一個集合中,通過定義這個集合來完成對數組中所有的事物的定義,puppet中數組通過方括號來定義,數組中的數據通過","分隔.
puppet通過["value1","value2"]的形式來定義數組,並將定義後的數組賦值給變量.
註意:下面示例包含兩種正確寫法和一種錯誤寫法,網絡有很多博文都在傳錯誤的寫法,不知道他們是照抄還是都沒自己去寫寫.
示例:
正確寫法(一)
這種算中規中矩的寫法,網上很多這樣的例子.
class php { package {["php","php-devel"]: ensure=> "installed" } }
agent端測試:
[[email protected] 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既然等於數組的元素,那麽下面再加[],就又是數組了,這樣總是對的吧,測試果然可行.
class php { $packages=["php","php-devel",] package {[$packages]: ensure=> "installed" } }
agent端更新:
[[email protected] 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軟件包.
class php { $packages = ["php","php-devel"] package {"$packages": ensure=> "installed" } }
agent端測試:
[[email protected] 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開始.可以將數組賦值給變量,並通過[數組下標]的方式進行取值.
# cat 2.pp $foo=[‘one‘,‘two‘,‘three‘] notice ($foo[1]) #通過notice函數打印$foo數組two的值.
本地應用:
[[email protected] ~]# 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的值.
[[email protected] ~]# cat 2.pp $foo=[‘one‘,{‘two‘ => 2,‘three‘ => 3}] notice ($foo[1][two])
agent端測試更新:
[[email protected] ~]# 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
本文出自 “青衫解衣” 博客,請務必保留此出處http://215687833.blog.51cto.com/6724358/1968370
Puppet數據類型中[數值類型,數組的使用] (十四)