一、puppet变量
1、名称必须以$开头:赋值使用=,支持追加赋值+=
2、变量名称引用的格式:简短名称和FQN
$scope::v
ariable
puppet的数据类型
1、直接字串(字串可以用引号也可以不用,单双都可以但是单引号是强引用)(换行,符为\n,Windows中为\r\n)
2、布尔型 : ture false,有些类型会自动转换为布尔型譬如:非空为ture,空为false,任何未定义的都是false,任何hash都是ture,任何资源引用都是ture
3、数值:有整数和浮点数可以使用("-"负号)
4、属组:保存在中括号内的值,属组中的元素可以是任意能够接受的puppet数值类型
$array=['red','blue','green']
$array[0]=red,$array[1]=blue,$array[3]=green
例子:package {['nginx','php','mysq']:
}
5、hash:key只能使用字串,value可以是puppet支持的任意类型
{key1 => value1,key2 => value2,key3 => value3,key4 => value4,key5 => value5,}
例子:$dic = {1 => 'monday', 2 => 'tuesday'}
$dic[1]=monday
6、undey:未定义的,不能加引号
puppet支持的变量
自定义变量
facter变量:可以在语言中直接调用
内置变量:
1)、agent端内置变量
$clientcert
$clientversion
2)、master端内置变量
$servername
$serverip
$serverversion
$module_name
二、puppet表达式
1、puppet的操作符:
$osfamily == "CentOS" 毕竟表达式
$kernel in ['Linux','solaris','freebsd'] 判断kernel是不是这三种中的一种
2、puppet的条件判断语句:
puppet2.7中有"if"、"case"、"selectors",而在3.7中引入了"unless"
1、if语句
2、case语句
类似if语句,case语句会从多个代码块中选择一个分支执行,这跟 其他语言中的case语句功能一致
case语句会接受一个控制表达式和一组case代码块,并执行打一个匹配到的控制表达式的块
1)、控制表达式可以是puppet代码中的任何代码片段(可以是单个变量,可以是一个表达式例如算法表达式,可以是一个有返回值的函数)
2)、各case分支可以是一个直接值、可以是个变量、可以是一个有返回单个值的函数、可以是一个正则表达式、可以是默认值default
3、selector语句
selector只能用于期望出现直接值(planin value)的地方,这包括变量赋值、资源属性、函数参数、资源标题、其他selector的值及表达式
selector不能用于一个已经嵌套于selector的case中,也不能用于一个已经嵌套于case的case语句中
1)、整个selector语句会被当作一个单独的值,puppet会将控制变量按列出的次序与每个case进行比较,并在遇到一个匹配的case后,将其值作为整个语句的值进行返回,并忽略后其他的case
2)、控制变量与各case比较的方式与case遇见相同,但如果没有任何一个case与控制变量匹配时,puppet在编译时会将返回一个错误,因此实践中其必须提供defeat case
3)、selector的控制变量只能是变量或有返回值的函数,切记不能使用表达式
4)、其各case可以是直接值(需加引号)、 变量、能调用返回值的函数、正则表达式模式或default
5)、但与case语句所不同的是 selector的各case不能使用列表
6)、selector的各case的值可以是一个除了hash以外的直接值、变量、能调用返回值的函数或其他selector
4、unless语句
基本没什么可讲的和if单分支语句取反一个意思
演示 & 示例
[[email protected] /tmp/puppet]# cat if.pp if $operatingsystem == 'CentOS' { notice("Welcome to CentOS.") } elsif $operatingsystem == 'Ubuntu' { notice("Welcome to Ubuntu.") } elsif $operatingsystem == 'RedHat' { notice("Welcome to RedHat.") } else { notice("Welcome, ET.") } [[email protected] /tmp/puppet]# puppet apply if.pp Notice: Scope(Class[main]): Welcome to CentOS. Notice: Compiled catalog for legion100.dwhd.org in environment production in 0.02 seconds Notice: Finished catalog run in 0.07 seconds [[email protected] /tmp/puppet]#
[[email protected] /tmp/puppet]# cat if2.pp if $operatingsystem =~ /^(?i-mx:(centos|ubuntu|debian|redhat))/ { notice("Welcome to $1.") } [[email protected] /tmp/puppet]# puppet apply if2.pp Notice: Scope(Class[main]): Welcome to CentOS. Notice: Compiled catalog for legion100.dwhd.org in environment production in 0.02 seconds Notice: Finished catalog run in 0.05 seconds [[email protected] /tmp/puppet]#
[[email protected] /tmp/puppet]# cat case.pp case $operatingsystem { 'Solaris': { notice("Welcome to Solaris.") } /^(Ubuntu|Debian)$/: { notice("Welcome to Debian OSFamily.") } 'CentOS','RedHad': { notice("Welcome to RedHat OSFamily.") } default: { notice("Welcome, ET *_*") } } [[email protected] /tmp/puppet]# puppet apply case.pp Notice: Scope(Class[main]): Welcome to RedHat OSFamily. Notice: Compiled catalog for legion100.dwhd.org in environment production in 0.02 seconds Notice: Finished catalog run in 0.08 seconds [[email protected] /tmp/puppet]#
[[email protected] /tmp/puppet]# service nginx stop 停止 nginx: [确定] [[email protected] /tmp/puppet]# cat apache.pp $webserver = $operatingsystem ? { /(?i-mx:ubuntu|debian)/ => 'apache2', /(?i-mx:centos|redhat)/ => 'httpd', } package {"$webserver": ensure => present, } [[email protected] /tmp/puppet]# puppet apply apache.pp 2>/dev/null Notice: Compiled catalog for legion100.dwhd.org in environment production in 0.32 seconds Notice: Finished catalog run in 0.28 seconds [[email protected] /tmp/puppet]# rpm -q httpd httpd-2.2.15-39.el6.centos.x86_64 [[email protected] /tmp/puppet]#
[[email protected] /tmp/puppet]# cat lamp.pp $webserver = $operatingsystem ? { /(?i-mx:ubuntu|debian)/ => 'apache2', /(?i-mx:centos|redhat)/ => 'httpd', } package {["$webserver","php","mysql-server"]: ensure => present, } service {['mysqld',httpd]: ensure => true, enable => true, } [[email protected] /tmp/puppet]# puppet apply lamp.pp 2>/dev/null Notice: Compiled catalog for legion100.dwhd.org in environment production in 0.38 seconds Notice: /Stage[main]/Main/Package/ensure: created Notice: /Stage[main]/Main/Service[httpd]/ensure: ensure changed 'stopped' to 'running' Notice: /Stage[main]/Main/Package[mysql-server]/ensure: created Notice: /Stage[main]/Main/Service[mysqld]/ensure: ensure changed 'stopped' to 'running' Notice: Finished catalog run in 18.07 seconds [[email protected] /tmp/puppet]# ss -tnlp | grep mysql LISTEN 0 50 *:3306 *:* users:(("mysqld",61841,10)) [[email protected] /tmp/puppet]# ss -tnlp | grep http LISTEN 0 511 :::80 :::* users:(("httpd",61590,4),("httpd",61594,4),("httpd",61595,4),("httpd",61596,4),("httpd",61597,4),("httpd",61598,4),("httpd",61600,4),("httpd",61601,4),("httpd",61603,4)) [[email protected] /tmp/puppet]#
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏