Puppet 语法,数据类型,变量和函数
Puppet 关键字
-
关键字(保留字)属于puppet系统内部保留字,不能用在其它地方
and, case, class, default, define, else, elsif, false, if, in, import, inherits
node, or, true, undef, unless, attr, function, type, private
main, settings
$string, $0…$n,
$facts
Puppet 命名规则
小写字母,数字,下划线,$aA_12, $class::name::variable_name
-
类和定义类型(Classes and Defined Types)
小写字母,数字,下划线,必须以小写字母开头,$aa_s2, $class::$aa_s3
init.pp 是保留文件名
和上面相同模块
以$开头,第一个字母为小写字母,其它字符可以为小写字母数字和下划线
以小写字母,数字,或下划线开头,包含小写字母,数字,下划线,冒号,点,连字符
任意字符,但是受限于底层实现,比如用户名
没有限制
仅能包含小写字母和数字
Puppet 数据类型
-
[root@node3 manifests]# vi var.pp
-
-
$a=1
-
$b="it is a string"
-
$c=["one","two","three"]
-
$d={a=>"1",b=>"2"}
-
-
warning($a)
-
warning($b)
-
warning($c)
-
warning($d)
-
~
-
~
-
~
-
~
-
"var.pp" 9L, 118C written
-
[root@node3 manifests]# puppet apply var.pp
-
Warning: Scope(Class[main]): 1
-
Warning: Scope(Class[main]): it is a string
-
Warning: Scope(Class[main]): one two three
-
Warning: Scope(Class[main]): a1b2
-
Notice: Compiled catalog for node3 in environment production in 0.05 seconds
-
Notice: Finished catalog run in 0.01 seconds
-
undef
测试变量是否定义
-
正则表达式
non-standard data type
if $host =~ /^www(\d+)\./ {
notify { "Welcome web server #$1": }
}
Puppet 运算符
! (not)
in
* and /
- and +
<< >> 位移
== 和 !=
>=, <=, >, 和 <
and
or
Puppet 变量和变量赋值
$osfamily, $::osfamily, $facts[‘osfamily’]
$module_name, $environment
stringify_facts = false, trusted_node_data = true
限制1:字符串, 数组, hash数组
限制2:必须是相同数据类型
-
[root@node3 manifests]# vi var.pp
-
-
$a=1
-
-
$b="it is a string"
-
$c=["one","two","three"]
-
$d={"a"=>"1","b"=>"2"}
-
-
class test{
-
$b += " not a number "
-
$c += ["four"]
-
$d += {"c"=>"3"}
-
-
warning($b)
-
warning($c)
-
warning($d)
-
}
-
-
include test
-
~
-
~
-
~
-
~
-
~
-
"var.pp" 16L, 190C written
-
[root@node3 manifests]# puppet apply var.pp
-
Warning: Scope(Class[Test]): it is a string not a number
-
Warning: Scope(Class[Test]): one two three four
-
Warning: Scope(Class[Test]): c3a1b2
-
Notice: Compiled catalog for node3 in environment production in 0.05 seconds
-
Notice: Finished catalog run in 0.02 seconds
Puppet 变量作用域
局部可以访问全局
全局不能访问局部
局部变量覆盖全局变量
-
[root@node3 manifests]# vi var.pp
-
-
-
class test{
-
$a='class scope'
-
warning($a)
-
}
-
-
node 'node3'{
-
$a="node scope"
-
warning($a)
-
include test
-
}
-
-
warning($a)
-
~
-
~
-
~
-
~
-
~
-
~
-
~
-
~
-
"var.pp" 13L, 132C written
-
[root@node3 manifests]# puppet apply var.pp
-
Warning: Scope(Class[main]):
-
Warning: Scope(Node[node3]): node scope
-
Warning: Scope(Class[Test]): class scope
-
Notice: Compiled catalog for node3 in environment production in 0.05 seconds
-
Notice: Finished catalog run in 0.02 seconds
函数
在生成 catalog 期间预定义ruby代码执行某些功能
debug('debug')
info('info')
notice('notice')
alert('alert')
crit('crit')
emerg('emerg')
warning("warning")
err("err")
puppet module install puppetlabs-stdlib
-
[root@node3 manifests]# puppet module install puppetlabs-stdlib
-
Notice: Preparing to install into /etc/puppet/modules ...
-
Notice: Downloading from https://forgeapi.puppetlabs.com ...
-
Notice: Installing -- do not interrupt ...
-
/etc/puppet/modules
-
└── puppetlabs-stdlib (v4.9.0)
-
[root@node3 manifests]# ll /etc/puppet/modules/
-
total 4
-
drwxr-xr-x 6 root root 4096 Oct 30 20:23 stdlib
-
-
[root@node3 manifests]# vi if.pp
-
if str2bool($facts['is_virtual']) == true {
-
warning("it is virtual machine")
-
}
-
else{
-
waring("it is not virtual machine")
-
}
-
[root@node3 manifests]# vi /etc/puppet/puppet.conf
-
#stringify_facts =false #所有值都是字符串类型,新版本可以输出值的不同类型,打开后facts是一种hash数组的表达式
-
trusted_node_data = true
-
-
[root@node3 manifests]# puppet apply if.pp Warning: Scope(Class[main]): it is virtual machine
-
Notice: Compiled catalog for 1yu in environment production in 0.05 seconds
-
Notice: Finished catalog run in 0.01 seconds
条件语句
■ if elsif else
-
[root@node3 manifests]# vi if.pp
-
-
if $facts['is_virtual'] == true {
-
warning("it is virtual machine")
-
}
-
else{
-
waring("it is not virtual machine")
-
}
-
~
-
-
~
-
~
-
"if.pp" 6L, 116C written
-
[root@node3 manifests]# puppet apply if.pp
-
Warning: Scope(Class[main]): it is virtual machine
-
Notice: Compiled catalog for node3 in environment production in 0.05 seconds
-
Notice: Finished catalog run in 0.01 seconds
■ unless
-
[root@node3 manifests]# vi if.pp
-
unless 1>2 {
-
warning('ok')
-
}
-
~
-
~
-
~
-
"if.pp" 3L, 29C written
-
[root@node3 manifests]# puppet apply if.pp
-
Warning: Scope(Class[main]): ok
-
Notice: Compiled catalog for node3 in environment production in 0.05 seconds
-
Notice: Finished catalog run in 0.01 seconds
■ case
-
[root@node3 manifests]# vi if.pp
-
case $facts['processorcount'] {
-
1: {warning("cpu count is 1")}
-
2: {warning("cpu count is 2")}
-
3: {warning("cpu count is 3")}
-
4: {warning("cpu count is 4")}
-
default: {warning("i don't know cpu count")}
-
}
-
~
-
~
-
~
-
~
-
"if.pp" 7L, 208C written
-
[root@node3 manifests]# puppet apply if.pp
-
Warning: Scope(Class[main]): cpu count is 4
-
Notice: Compiled catalog for node3 in environment production in 0.05 seconds
-
Notice: Finished catalog run in 0.01 seconds
■ selector
-
[root@node3 manifests]# vi if.pp
-
-
$output = $facts['is_virtual'] ? {
-
'true' => 'string true',
-
'false' => 'string false',
-
true => 'boolean true',
-
false => 'boolean false'
-
}
-
warning($output)
-
~
-
-
~
-
"if.pp" 7L, 159C written
-
[root@node3 manifests]# puppet apply if.pp
-
Warning: Scope(Class[main]): string true
-
Notice: Compiled catalog for node3 in environment production in 0.05 seconds
-
Notice: Finished catalog run in 0.01 seconds
Puppet 配置文件语法检查
puppet parser validate machine.pp
阅读(974) | 评论(0) | 转发(0) |