puppet erb模板

        在日常运维工作中有很多需求,他们各自有各自相对独立的差异化配置,如果要管理这些差异配置的话,就可以通过erb模板和模板语言来管理这些差异化服务器配置,这就是erb模板的主要应用场景.


ERB模板的体现就是一个文本文件,它以.erb作为扩展名来标示它的用途,代码如下:

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


注意:这里模板文件的路径可以使用相对路径也可以使用绝对路径,相对路径通常用于C/S架构,绝对路径通常用于单机.


    file资源中的source属性,与template函数调用模板形式,两者相比都可以实现同步文件的功能,但是过程和结果却不一样,file资源的source属性同步文件通过puppet的文件协议,将文件由源路径同步到目的路径,但是它并不能更改文件中的内容,从而可以实现根据需求来定制同步文件与内容。


file中的source示例:

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

puppet erb模板文件示例:


模板文件支持变量传参:

示例:

master端puppet代码:

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


模板文件:

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


agent端更新:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[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


1
2
3
4
5
[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 %>作为结束.


模板文件内容:

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

注释:模板使用if条件语句.


agent端更新测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[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代码如下:

1
2
3
4
5
$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'),
    }
}

模板文件内容:

1
2
3
4
5
6
; generated by /sbin/dhclient-script
search localdomain
<% arry_value.each do |val| -%>
nameserver <%= val %>
<% end -%>
nameserver 192.168.30.2


agent端更新:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[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

本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1979532