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
相关文章
相关标签/搜索