ainila 发表于 2018-8-2 12:17:30

【5】puppet笔记 - defined类型、erb模板

  Defined Types:http://docs.puppetlabs.com/learning/definedtypes.html
  define类型和其它语言的函数差不多,把有共性的一些东东封装在一起,可以简化一些重复的代码。
  define 类型名称( [参数1,参数2,....] ) {
  各种资源类型 {
  属性1 =>参数1,
  属性2 =>参数2,
  }
  }
  调用方法:
  类型名称 {
  参数1 => 值1,
  参数2 => 值2,
  }
  例子:
# vi defi.pp  
define printstr ($arg1 ,$arg2) {
  notice "arg1 = ${arg1},$arg2 = ${arg2}"
  
}
  
printstr {
  "test":
  arg1 => "hello ,",
  arg2 => "puppet !",
  
}
  
printstr {
  "test2":
  arg1 => "hahahahahah",
  arg2 => "lkjslkfjalkdjasldkfl",
  
"defi.pp" 21L, 227C written
  
# puppet apply defi.pp
  
Notice: Scope(Printstr): arg1 = hello ,,puppet ! = puppet !
  
Notice: Scope(Printstr): arg1 = hahahahahah,lkjslkfjalkdjasldkfl = lkjslkfjalkdjasldkfl
  
Notice: Compiled catalog for pclient.onepc.com in environment production in 0.08 seconds
  
Notice: Finished catalog run in 0.05 seconds
  
#
  包含资源的例子:
define createf ($filename=$title,$content1,$owner1,$mode1) {  file {
  "/puppet/file/${filename}":
  ensure => present,
  content => $content1,
  owner =>” $owner1”,
  mode => $mode1,
  }
  
}
  
createf {
  "abc.log":
  Content1 => "hello,puppet!",
  Owner1 => "hxw",
  Mode1 => 777,
  
}
  
# puppet apply define.pp
  
Notice: Compiled catalog for pclient.onepc.com in environment production in 0.09 seconds
  
Notice: /Stage/Main/Createf/File/ensure: created
  
Notice: Finished catalog run in 0.06 seconds
  erb语法:
  1、 http://docs.puppetlabs.com/guides/templating.html#erb-template-syntax
  2、 http://docs.puppetlabs.com/learning/templates.html
  <%= @var %> @var 将替换为已知变量的值
  The tags available in an ERB file depend on the way the ERB processor is configured. Puppet always uses the same configuration for its templates (see “trim mode” below), which makes the following tags available:

[*]  <%= Ruby expression %> — This tag will be replaced with the value of the expression it contains.
[*]  <% Ruby code %> — This tag will execute the code it contains, but will not be replaced by a value. Useful for conditional or looping logic, setting variables, and manipulating data before printing it.
[*]  <%# comment %> — Anything in this tag will be suppressed in the final output.
[*]  <%% or %%> — A literal <% or %>, respectively.
[*]  <%- — Same as <%, but suppresses any leading whitespace in the final output. Useful when indenting blocks of code for readability.
[*]  -%> — Same as %>, but suppresses the subsequent line break in the final output. Useful with many lines of non-printing code in a row, which would otherwise appear as a long stretch of blank lines.
  erb用法:
  定义一个define类型或者class:
  define 名称($var1,var2) {
  file {
  "title":
  path => path,
  content => template ("x.erb"),
  }
  }
  定义erb模板:
  a = <%= @var1 %>
  b = <%= @var2 %>
  define类型的变量可以传给erb模板定义var,达到动态改变模板的值。
  erb模板例子:
  因为是单机,所以template里面使用绝对路径指定erb文件的位置。
  cathosts.pp
define hostip ($filename=$title,$hostn,$hostip) {  
file {
  "${filename}":
  path => "/puppet/file/${filename}",
  ensure => present,
  mode => 666,
  content => template("/puppet/test/hosts.erb"),
  
}
  
}
  
hostip {
  "computer1":
  hostn => "hxw168.blog.51cto.com",
  hostip => "168.168.168.168",
  
}
  
hostip {
  "computer2":
  hostn => "hxw168.com",
  hostip => "100.100.100.100",
  
}
  cat hosts.erb
# cat hosts.erb  
#erb test
  
hostname = <%= @hostn%>
  
hostaddress = <%= @hostip%>
  puppet apply hosts.pp
# puppet apply hosts.pp  
Notice: Compiled catalog for pclient.onepc.com in environment production in 0.12 seconds
  
Notice: /Stage/Main/Hostip/File/ensure: created
  
Notice: /Stage/Main/Hostip/File/ensure: created
  
Notice: Finished catalog run in 0.07 seconds
# cat computer1 computer2  
#erb test
  
hostname = hxw168.blog.51cto.com
  
hostaddress = 168.168.168.168
  
#erb test
  
hostname = hxw168.com
  
hostaddress = 100.100.100.100
页: [1]
查看完整版本: 【5】puppet笔记 - defined类型、erb模板