puppet的使用:ERB模板介紹

ERB介紹

全稱是Embedded RuBy,意思是嵌入式的Ruby,是一種文本模板技術,用過JSP的話,會發現二者語法很像。
咱們項目中通常用ERB來產生各模塊的配置文件。ERB模板也能夠用來產生Web頁面(以前搞過一段時間ROR開發,模板用的haml),也能夠用來產生其餘文件。html

<% %>與<%= %>

<%Ruby腳本%>,通常是Ruby的邏輯腳本,可是不會寫入到目標文件中。
<%= Ruby腳本%> ,腳本的執行結果會寫入到目標文件中。
舉例以下(代碼來源):
這段代碼是從Hash中讀取信息建立sql語句保存到文件中。node

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.nginx

  • -%> — If the tag ends a line, trim the following line break.

意思是若是<%= -%>表達式中,-%>是一行的結尾,會忽略後面的換行。web

<%- -%>

You can trim whitespace surrounding a non-printing tag by adding hyphens (-) to the tag delimiters.sql

  • <%- — If the tag is indented, trim the indentation.
  • -%> — If the tag ends a line, trim the following line break.

意思是<%-前面是空白符時,會刪除這些空白符;-%> 含義同上。express

puppet中通常怎麼使用erb

舉例以下:數組

目錄結構

~/tmp# tree -L 2 puppet
puppet
├── manifests
│     ├── nodes
│     └── site.pp
└── modules
    ├── xxx
    └── nginx

這裏是大的目錄結構。ruby


manifests的內容以下:dom

~/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"]) %>

參考
Language: Embedded Ruby (ERB) template syntax

相關文章
相關標籤/搜索