Puppet erb模板介紹(三十二)


puppet erb模板php

        在平常運維工做中有不少需求,他們各自有各自相對獨立的差別化配置,若是要管理這些差別配置的話,就能夠經過erb模板和模板語言來管理這些差別化服務器配置,這就是erb模板的主要應用場景.linux


ERB模板的體現就是一個文本文件,它以.erb做爲擴展名來標示它的用途,代碼以下:web

file { "/etc/php-fpm.d/www.conf":
ensure  => file,
content => template("php/wwwproxy.conf.erb"),
}


注意:這裏模板文件的路徑能夠使用相對路徑也能夠使用絕對路徑,相對路徑一般用於C/S架構,絕對路徑一般用於單機.bash


    file資源中的source屬性,與template函數調用模板形式,二者相比均可以實現同步文件的功能,可是過程和結果卻不同,file資源的source屬性同步文件經過puppet的文件協議,將文件由源路徑同步到目的路徑,可是它並不能更改文件中的內容,從而能夠實現根據需求來定製同步文件與內容。服務器


file中的source示例:架構

file {'/etc/haproxy/haproxy.cfg':
    ensure => present,
    source => 'puppet:///modules/haproxy/haproxy.cfg',
    notify => Exec['/etc/init.d/haproxy restart'],
}

puppet erb模板文件示例:運維


模板文件支持變量傳參:dom

示例:ide

master端puppet代碼:函數

$ip_1="8.8.8.8"
file {'/etc/resolv.conf':
    content => template('admin/etc/resolv.conf.erb'),
}


模板文件:

; generated by /sbin/dhclient-script
search localdomain
nameserver 192.168.30.2
nameserver <%= ip_1 %>


agent端更新:

[root@sh-web1 ~]# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1509984468'
Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully
Notice: /Stage[main]/Admin/File[/etc/resolv.conf]/content: 
--- /etc/resolv.conf2017-09-01 18:10:30.036376971 +0800
+++ /tmp/puppet-file20171106-37158-4dbt1c-02017-11-06 16:07:47.101648953 +0800
@@ -1,3 +1,4 @@
-; generated by /sbin/dhclient-script
-search localdomain
-nameserver 192.168.30.2
+; generated by /sbin/dhclient-script
+search localdomain
+nameserver 192.168.30.2
+nameserver 8.8.8.8
\ No newline at end of file
Info: Computing checksum on file /etc/resolv.conf
Info: /Stage[main]/Admin/File[/etc/resolv.conf]: Filebucketed /etc/resolv.conf to puppet with sum a880aa161449e4801222f39b9087ad0b
Notice: /Stage[main]/Admin/File[/etc/resolv.conf]/content: content changed '{md5}a880aa161449e4801222f39b9087ad0b' to '{md5}d64e778c4dd698a73b399c6430cd7fc4'
Notice: Finished catalog run in 0.30 seconds


[root@sh-web1 ~]# cat /etc/resolv.conf 
; generated by /sbin/dhclient-script
search localdomain
nameserver 192.168.30.2
nameserver 8.8.8.8


puppet模板中的if...else...fi條件判斷語句

if...elsif...else...fi條件語句須要放入以<%做爲開始,以%>做爲結束的符號內.另外須要注意<%if 條件表達式%>最後要以<% end %>做爲結束.


模板文件內容:

; generated by /sbin/dhclient-script
search localdomain
<% if hostname =~ /sh-web\d/ %>
nameserver <%= ip_1 %>
<% end %>
nameserver 192.168.30.2

註釋:模板使用if條件語句.


agent端更新測試:

[root@sh-web1 ~]# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1509985283'
Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully
Notice: /Stage[main]/Admin/File[/etc/resolv.conf]/content: 
--- /etc/resolv.conf2017-11-06 16:20:16.706631342 +0800
+++ /tmp/puppet-file20171106-37995-5hvzbe-02017-11-06 16:21:21.839631273 +0800
@@ -1,7 +1,6 @@
 ; generated by /sbin/dhclient-script
 search localdomain
 
-$ip_1="22.22.22.22"
+nameserver 8.8.8.8
 
 nameserver 192.168.30.2
-nameserver 8.8.8.8
\ No newline at end of file
Info: Computing checksum on file /etc/resolv.conf
Info: /Stage[main]/Admin/File[/etc/resolv.conf]: Filebucketed /etc/resolv.conf to puppet with sum b53bde035cf20d095306e09e84334fbc
Notice: /Stage[main]/Admin/File[/etc/resolv.conf]/content: content changed '{md5}b53bde035cf20d095306e09e84334fbc' to '{md5}da4ff6163dd0c7c1108bb4d1838295d3'
Notice: Finished catalog run in 0.23 seconds
[root@sh-web1 ~]# cat /etc/resolv.conf 
; generated by /sbin/dhclient-script
search localdomain
nameserver 8.8.8.8
nameserver 192.168.30.2


each循環

each循環語句語法以<%做爲開始,以 -%>做爲結束.


puppet代碼以下:

$arry_value=['192.168.1.2','192.168.1.3','192.168.1.4']
file {'/etc/resolv.conf':
    content => template('admin/etc/resolv.conf.erb'),
    }
}

模板文件內容:

; generated by /sbin/dhclient-script
search localdomain
<% arry_value.each do |val| -%>
nameserver <%= val %>
<% end -%>
nameserver 192.168.30.2


agent端更新:

[root@sh-web1 ~]# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1509986539'
Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully
Notice: /Stage[main]/Admin/File[/etc/resolv.conf]/content: 
--- /etc/resolv.conf2017-11-06 16:21:21.846631274 +0800
+++ /tmp/puppet-file20171106-38795-2fa1gc-02017-11-06 16:42:17.130787147 +0800
@@ -1,6 +1,10 @@
 ; generated by /sbin/dhclient-script
 search localdomain
 
-nameserver 8.8.8.8
+nameserver 192.168.1.2
+
+nameserver 192.168.1.3
+
+nameserver 192.168.1.4
 
 nameserver 192.168.30.2
Info: Computing checksum on file /etc/resolv.conf
Info: /Stage[main]/Admin/File[/etc/resolv.conf]: Filebucketed /etc/resolv.conf to puppet with sum da4ff6163dd0c7c1108bb4d1838295d3
Notice: /Stage[main]/Admin/File[/etc/resolv.conf]/content: content changed '{md5}da4ff6163dd0c7c1108bb4d1838295d3' to '{md5}d6824dac14f7cbbe73317e5a4034c2bf'
Notice: Finished catalog run in 0.24 seconds
相關文章
相關標籤/搜索