本文主要寫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端更新:
[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
乘法運算:
$content=7 * 4
notify {"hostip is $content":}
[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
取餘運算:
$content=7 % 4
notify {"hostip is $content":}
[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
除法運算:
$content=7 / 4
notify {"hostip is $content":}
[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
浮點數類型運算:
$content=7 + 0.4
notify {"hostip is $content":}
[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
注意:符号不能同時使用;浮點型資料以數字加小數點的形式表達.
錯誤的示例:
$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端測試:
[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既然等于數組的元素,那麼下面再加[],就又是數組了,這樣總是對的吧,測試果然可行.
class php {
$packages=["php","php-devel",]
package {[$packages]:
ensure=> "installed"
}
}
[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軟體包.
class php {
$packages = ["php","php-devel"]
package {"$packages":
ensure=> "installed"
}
}
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLygjNrx2V30mc4FFRBF0VSVHZTdjUklkbxw0bpt0dvw1Qw8CX2E0LcBDMN9CXyAzcml3dvwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
[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開始.可以将數組指派給變量,并通過[數組下标]的方式進行取值.
# cat 2.pp
$foo=['one','two','three']
notice ($foo[1]) #通過notice函數列印$foo數組two的值.
本地應用:
[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通過嵌套數組來模拟多元數組,并通過索引的方式通路嵌套數組的值.
[root@sh-web1 ~]# cat 2.pp
$foo=['one',{'two' => 2,'three' => 3}]
notice ($foo[1][two])
[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