ERB介紹
全稱是Embedded RuBy,意思是嵌入式的Ruby,是一種文本模板技術,用過JSP的話,會發現兩者語法很像。
我們項目中一般用ERB來產生各模塊的配置文件。ERB模板也可以用來產生Web頁面(之前搞過一段時間ROR開發,模板用的haml),也可以用來產生其他文件。
<% %>與<%= %>
<%Ruby腳本%>,一般是Ruby的邏輯腳本,但是不會寫入到目標文件中。
<%= Ruby腳本%> ,腳本的執行結果會寫入到目標文件中。
舉例如下(代碼來源):
這段代碼是從Hash中讀取信息創建sql語句保存到文件中。
require "erb"
domains = {...}
sqlTemplate = ERB.new %q{
<%for organization in domains.keys%>
insert into org_domain(Domain, organization) values('<%=domains[organization]%>','<%=organization%>');
<%end%>
}
sqlFile = File.new("./sql.sql", "w")
sqlFile.puts sqlTemplate.result
使輸出緊湊
<%= -%>
You can trim line breaks after expression-printing tags by adding a hyphen to the closing tag delimiter.
- -%> — If the tag ends a line, trim the following line break.
意思是如果<%= -%>表達式中,-%>是一行的結尾,會忽略后面的換行。
<%- -%>
You can trim whitespace surrounding a non-printing tag by adding hyphens (-) to the tag delimiters.
- <%- — If the tag is indented, trim the indentation.
- -%> — If the tag ends a line, trim the following line break.
意思是<%-前面是空白符時,會刪除這些空白符;-%> 含義同上。
puppet中一般怎么使用erb
舉例如下:
目錄結構
~/tmp# tree -L 2 puppet
puppet
├── manifests
│ ├── nodes
│ └── site.pp
└── modules
├── xxx
└── nginx
這里是大的目錄結構。
manifests的內容如下:
~/tmp/puppet# tree -L 2 manifests
manifests
├── nodes
│ ├── controller-192-41-1-185.pp
│ ├── controller-192-41-1-186.pp
│ ├── controller-192-41-1-187.pp
│ ├── controller-192-41-1-191.pp
│ └── controller-192-41-1-202.pp
└── site.pp
這里每個.pp文件是對應每個agent節點的定義,具體內容見下一節。
每個模塊下的內容類似,現在只以nginx舉例。
~/tmp/puppet/modules# tree -L 3 nginx/
nginx/
├── files
│ ├── xxxClientCert.jks
│ ├──xxxServerCert.jks
│ ├── README.md
│ ├── server.crt
│ └── server.key
├── manifests
│ ├── config.pp
│ ├── init.pp
│ ├── params.pp
│ └── service.pp
└── templates
└── conf.d
├── nginx.conf.erb
├── ssl_restart_oms.sh.erb
├── web-hedex.xml.erb
└── web.xml.erb
具體模塊的manifests目錄下是各個類的定義,templates定義了各個模板,模板會在manifests下的類中被引用。
類的定義
~/tmp/puppet/manifests# cat site.pp
import 'nodes/*.pp'
$puppetserver='controller-192-41-1-191'
下面是一個node的定義:
~/tmp/puppet/manifests/nodes# cat controller-192-41-1-191.pp
node 'controller-192-41-1-191' {
$xxx = "true"
#這里是各種變量的定義
Class['aaa']->Class['bbb']->Class['ccc']->Class['nginx']->Class['ddd']
include aaa,bbb,ccc,nginx,ddd
}
模板中使用的變量就是來自這里的定義。
總結如下:
nginx/manifests下的各個pp文件是各個類的定義,類的定義中使用了nginx/templates中定義的模板,這些類被controller-192-41-1-191.pp 文件引用,controller-192-41-1-191.pp 文件中定義了模板中使用的變量。
訪問puppet 變量
模板可以訪問puppet的變量,這是模板的主要數據來源。
一個ERB模板有自己的本地作用域,它的父作用域是引用該模板的類。這意味着,模板可以使用短名稱訪問父類中的變量,但是不能向父類中添加新的變量。
在ERB模板中,有兩種方式訪問變量。
- @variable
- scope['variable'] (舊的形式為:scope.lookupvar('variable'))
@variable
原文:
All variables in the current scope (including global variables) are passed to templates as Ruby instance variables, which begin with “at” signs (@). If you can access a variable by its short name in the surrounding manifest, you can access it in the template by replacing its $ sign with an @. So $os becomes @os, $trusted becomes @trusted, etc.
當前作用域的所有變量(包括全局變量)都會以Ruby實例變量的形式傳給模板。如果你在模板中可以使用短名稱訪問一個變量,你就可以使用"@"代替"$"。所以,$os就變成了@os,$trusted就變成了@trusted。
這是訪問變量最直接清晰的方式,但是這種方式不能訪問其他作用域的變量,需要訪問其他作用域的變量時,你需要使用下面的方式。
scope['variable'] or scope.lookupvar('variable')
Puppet可以使用對象scope來以散列表的方式訪問變量。例如,訪問$ntp::tinker你可以使用scope['ntp::tinker']這種形式。
還有另一種方式使用scope對象,就是通過它的lookupvar方法來訪問變量,該方法需要傳入要訪問的變量名,例如,scope.lookupvar('ntp::tinker')。這和上面的方式效果是一樣的,但是使用起來沒有scope['ntp::tinker']更簡潔直觀。
puppet類型和Ruby類型的對應關系
這部分表格,就不翻譯了,直接貼圖。

未定義變量
如果一個puppet變量沒有定義的話,它的值是undef,意思是,在模板中它的值是nil。
含義是Puppet變量值為undef,對應Ruby變量值為nil。
在模板中使用puppet函數
- 所有的函數都以scope對象的方法調用;
- 函數名的開頭都必須加上前綴"function_";
- 函數的參數必須以數組的形式傳遞,即使只有一個參數。
例如,在一個模板中引用另一個模板:
<%= scope.function_template(["my_module/template2.erb"]) %>
