Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32289
  • 博文数量: 9
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 100
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-27 11:03
文章分类

全部博文(9)

文章存档

2015年(9)

我的朋友

分类: LINUX

2015-10-31 16:31:50

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 命名规则
  • 变量(variable)
小写字母,数字,下划线,$aA_12, $class::name::variable_name
  • 类和定义类型(Classes and Defined Types)
小写字母,数字,下划线,必须以小写字母开头,$aa_s2, $class::$aa_s3
init.pp 是保留文件名
  •  模块(module)
和上面相同模块
  •  参数(parameter)
以$开头,第一个字母为小写字母,其它字符可以为小写字母数字和下划线
  • 标签(Tags)
以小写字母,数字,或下划线开头,包含小写字母,数字,下划线,冒号,点,连字符
  •  资源(Resources)
任意字符,但是受限于底层实现,比如用户名
  •  节点(Nodes)
没有限制
  •  环境(Environment)
仅能包含小写字母和数字

Puppet 数据类型
  • 数值类型
  •  字符串类型
  •  数组类型
  •  hash数组


点击(此处)折叠或打开

  1. [root@node3 manifests]# vi var.pp

  2. $a=1
  3. $b="it is a string"
  4. $c=["one","two","three"]
  5. $d={a=>"1",b=>"2"}

  6. warning($a)
  7. warning($b)
  8. warning($c)
  9. warning($d)
  10. ~
  11. ~
  12. ~
  13. ~
  14. "var.pp" 9L, 118C written
  15. [root@node3 manifests]# puppet apply var.pp
  16. Warning: Scope(Class[main]): 1
  17. Warning: Scope(Class[main]): it is a string
  18. Warning: Scope(Class[main]): one two three
  19. Warning: Scope(Class[main]): a1b2
  20. Notice: Compiled catalog for node3 in environment production in 0.05 seconds
  21. 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 变量和变量赋值
  •  facts和内置变量
$osfamily, $::osfamily, $facts[‘osfamily’]
$module_name, $environment
stringify_facts = false, trusted_node_data = true
  •  变量的数据类型
  •  变量赋值
  •  追加赋值
限制1:字符串, 数组, hash数组
限制2:必须是相同数据类型

点击(此处)折叠或打开

  1. [root@node3 manifests]# vi var.pp

  2. $a=1

  3. $b="it is a string"
  4. $c=["one","two","three"]
  5. $d={"a"=>"1","b"=>"2"}

  6. class test{
  7. $b += " not a number "
  8. $c += ["four"]
  9. $d += {"c"=>"3"}

  10. warning($b)
  11. warning($c)
  12. warning($d)
  13. }

  14. include test
  15. ~
  16. ~
  17. ~
  18. ~
  19. ~
  20. "var.pp" 16L, 190C written
  21. [root@node3 manifests]# puppet apply var.pp
  22. Warning: Scope(Class[Test]): it is a string not a number
  23. Warning: Scope(Class[Test]): one two three four
  24. Warning: Scope(Class[Test]): c3a1b2
  25. Notice: Compiled catalog for node3 in environment production in 0.05 seconds
  26. Notice: Finished catalog run in 0.02 seconds


Puppet 变量作用域
  • 变量作用域
局部可以访问全局
全局不能访问局部
局部变量覆盖全局变量

点击(此处)折叠或打开

  1. [root@node3 manifests]# vi var.pp


  2. class test{
  3. $a='class scope'
  4. warning($a)
  5. }

  6. node 'node3'{
  7. $a="node scope"
  8. warning($a)
  9. include test
  10. }

  11. warning($a)
  12. ~
  13. ~
  14. ~
  15. ~
  16. ~
  17. ~
  18. ~
  19. ~
  20. "var.pp" 13L, 132C written
  21. [root@node3 manifests]# puppet apply var.pp
  22. Warning: Scope(Class[main]):
  23. Warning: Scope(Node[node3]): node scope
  24. Warning: Scope(Class[Test]): class scope
  25. Notice: Compiled catalog for node3 in environment production in 0.05 seconds
  26. 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-stdlib
puppet module install puppetlabs-stdlib

点击(此处)折叠或打开

  1. [root@node3 manifests]# puppet module install puppetlabs-stdlib
  2. Notice: Preparing to install into /etc/puppet/modules ...
  3. Notice: Downloading from https://forgeapi.puppetlabs.com ...
  4. Notice: Installing -- do not interrupt ...
  5. /etc/puppet/modules
  6. └── puppetlabs-stdlib (v4.9.0)
  7. [root@node3 manifests]# ll /etc/puppet/modules/
  8. total 4
  9. drwxr-xr-x 6 root root 4096 Oct 30 20:23 stdlib

  10. [root@node3 manifests]# vi if.pp
  11. if str2bool($facts['is_virtual']) == true {
  12.         warning("it is virtual machine")
  13. }
  14. else{
  15.         waring("it is not virtual machine")
  16. }
  17. [root@node3 manifests]# vi /etc/puppet/puppet.conf
  18. #stringify_facts =false    #所有值都是字符串类型,新版本可以输出值的不同类型,打开后facts是一种hash数组的表达式
  19. trusted_node_data = true

  20. [root@node3 manifests]# puppet apply if.pp Warning: Scope(Class[main]): it is virtual machine
  21. Notice: Compiled catalog for 1yu in environment production in 0.05 seconds
  22. Notice: Finished catalog run in 0.01 seconds
  •  自定义函数
条件语句
■ if elsif else

点击(此处)折叠或打开

  1. [root@node3 manifests]# vi if.pp

  2. if $facts['is_virtual'] == true {
  3.         warning("it is virtual machine")
  4. }
  5. else{
  6.         waring("it is not virtual machine")
  7. }
  8. ~
  9.                                                  
  10. ~
  11. ~
  12. "if.pp" 6L, 116C written
  13. [root@node3 manifests]# puppet apply if.pp
  14. Warning: Scope(Class[main]): it is virtual machine
  15. Notice: Compiled catalog for node3 in environment production in 0.05 seconds
  16. Notice: Finished catalog run in 0.01 seconds
■ unless

点击(此处)折叠或打开

  1. [root@node3 manifests]# vi if.pp
  2. unless 1>2 {
  3. warning('ok')
  4. }
  5. ~
  6. ~
  7. ~
  8. "if.pp" 3L, 29C written
  9. [root@node3 manifests]# puppet apply if.pp
  10. Warning: Scope(Class[main]): ok
  11. Notice: Compiled catalog for node3 in environment production in 0.05 seconds
  12. Notice: Finished catalog run in 0.01 seconds
■ case

点击(此处)折叠或打开

  1. [root@node3 manifests]# vi if.pp
  2. case $facts['processorcount'] {
  3.         1: {warning("cpu count is 1")}
  4.         2: {warning("cpu count is 2")}
  5.         3: {warning("cpu count is 3")}
  6.         4: {warning("cpu count is 4")}
  7.         default: {warning("i don't know cpu count")}
  8. }
  9. ~
  10. ~
  11. ~
  12. ~
  13. "if.pp" 7L, 208C written
  14. [root@node3 manifests]# puppet apply if.pp
  15. Warning: Scope(Class[main]): cpu count is 4
  16. Notice: Compiled catalog for node3 in environment production in 0.05 seconds
  17. Notice: Finished catalog run in 0.01 seconds
■ selector

点击(此处)折叠或打开

  1. [root@node3 manifests]# vi if.pp

  2. $output = $facts['is_virtual'] ? {
  3.         'true' => 'string true',
  4.         'false' => 'string false',
  5.         true => 'boolean true',
  6.         false => 'boolean false'
  7. }
  8. warning($output)
  9. ~
  10.  
  11. ~
  12. "if.pp" 7L, 159C written
  13. [root@node3 manifests]# puppet apply if.pp
  14. Warning: Scope(Class[main]): string true
  15. Notice: Compiled catalog for node3 in environment production in 0.05 seconds
  16. Notice: Finished catalog run in 0.01 seconds






Puppet 配置文件语法检查

点击(此处)折叠或打开

    puppet  parser validate machine.pp



阅读(974) | 评论(0) | 转发(0) |
0

上一篇:MongoDB入门一

下一篇:Redis安装

给主人留下些什么吧!~~