puppet模板模块
puppet类
1、定义类
vim:从当前行至倒数第一行,在非空白字符行前面加空格
:.,$-1s/^[^[:space:]]/ &/g
声明类
[root@master1 manifests]# vim test13.pp
class nginx {
$webserver=nginx
package{$webserver:
ensure => latest,
}
file{'/etc/nginx/nginx.conf':
ensure => file,
source => '/root/modules/nginx/files/nginx.conf',
require => Package['nginx'],
notify => Service['nginx'],
}
service{'nginx':
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
# restart => 'systemctl reload nginx.service',
require => [ Package['nginx'],File['/etc/nginx/nginx.conf'] ]
}
}
[root@master1 manifests]# puppet apply -v test13.pp
Notice: Compiled catalog for master1.master1.com in environment production in 0.07 seconds
Info: Applying configuration version '1513291848'
Notice: Finished catalog run in 0.13 seconds
2、声明类(include)
[root@master1 manifests]# vim test13.pp
class nginx {
$webserver=nginx
package{$webserver:
ensure => latest,
}
file{'/etc/nginx/nginx.conf':
ensure => file,
source => '/root/modules/nginx/files/nginx.conf',
require => Package['nginx'],
notify => Service['nginx'],
}
service{'nginx':
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
# restart => 'systemctl reload nginx.service',
require => [ Package['nginx'],File['/etc/nginx/nginx.conf'] ]
}
}
include nginx
测试:
[root@master1 manifests]# puppet apply -v test13.pp
Notice: Compiled catalog for master1.master1.com in environment production in 2.10 seconds
Info: Applying configuration version '1513293247'
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Info: Computing checksum on file /etc/nginx/nginx.conf
Info: FileBucket got a duplicate file {md5}93bc8e01bfd45e7e18b23acc178ae25b
Info: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 93bc8e01bfd45e7e18b23acc178ae25b
Notice: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]/content: content changed '{md5}93bc8e01bfd45e7e18b23acc178ae25b' to '{md5}9b2cf71797e8ad03e9812f314ad6b198'
Info: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]: Scheduling refresh of Service[nginx]
Notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Nginx/Service[nginx]: Unscheduling refresh on Service[nginx]
Notice: Finished catalog run in 21.68 seconds
3、声明类,变量定义参数
[root@master1 manifests]# vim test14.pp
class nginx($webserver='nginx') {
package{$webserver:
ensure => latest,
}
file{'/etc/nginx/nginx.conf':
ensure => file,
source => '/root/modules/nginx/files/nginx.conf',
require => Package['nginx'],
notify => Service['nginx'],
}
service{'nginx':
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
# restart => 'systemctl reload nginx.service',
require => [ Package['nginx'],File['/etc/nginx/nginx.conf'] ]
}
}
include nginx
4、声明变量方式(2)
class nginx($webserver='nginx') {
package{$webserver:
ensure => latest,
}
file{'/etc/nginx/nginx.conf':
ensure => file,
source => '/root/modules/nginx/files/nginx.conf',
require => Package['nginx'],
notify => Service['nginx'],
}
service{'nginx':
ensure => running,
enable => true,
hasrestart => true,
hasstatus => true,
# restart => 'systemctl reload nginx.service',
require => [ Package['nginx'],File['/etc/nginx/nginx.conf'] ]
}
}
class{'nginx':
webserver => 'tengine',
}
5、类继承
root@master1 manifests]# vim test15.pp
class nginx {
package{'nginx':
ensure => latest,
} ->
service{'nginx':
enable => true,
ensure => running,
hasrestart => true,
hasstatus => true,
restart => 'service nginx reload',
}
}
class nginx::webserver inherits nginx {
file{'/etc/nginx/nginx.conf':
source => '/root/modules/nginx/files/nginx_web.conf',
ensure => file,
notify => Service['nginx'],
}
}
class nginx::proxy inherits nginx {
file{'/etc/nginx/nginx.conf':
source => '/root/modules/nginx/files/nginx_proxy.conf',
ensure => file,
notify => Service['nginx'],
}
}
include nginx::webserver
[root@master1 manifests]# puppet apply -v test15.pp
Notice: Compiled catalog for master1.master1.com in environment production in 2.05 seconds
Info: Applying configuration version '1513353078'
Notice: Finished catalog run in 12.43 seconds
[root@master1 manifests]#
切换成反代模式:
修改配置文件,类声明
include nginx::proxy
[root@master1 manifests]# puppet apply -v test15.pp
Notice: Compiled catalog for master1.master1.com in environment production in 1.92 seconds
Info: Applying configuration version '1513353772'
Info: Computing checksum on file /etc/nginx/nginx.conf
Info: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 9b2cf71797e8ad03e9812f314ad6b198
Notice: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]/content: content changed '{md5}9b2cf71797e8ad03e9812f314ad6b198' to '{md5}0ba0cf9c84b56c8a0db3bffb55da5002'
Info: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]: Scheduling refresh of Service[nginx]
Notice: /Stage[main]/Nginx/Service[nginx]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 3.95 seconds
6、覆盖父类属性,父类安装nginx,但是子类想安装t-nginx
[root@master1 manifests]# vim test16.pp
class nginx {
package{'nginx':
ensure => latest,
} ->
class nginx {
package{'nginx':
ensure => latest,
name => nginx,
} ->
service{'nginx':
enable => true,
ensure => running,
hasrestart => true,
hasstatus => true,
restart => 'service nginx reload',
}
}
class nginx::webserver inherits nginx {
Package['nginx'] {
name => tengine,
}
file{'/etc/nginx/nginx.conf':
source => '/root/modules/nginx/files/nginx_web.conf',
ensure => file,
notify => Service['nginx'],
}
}
没有tengine报错:
[root@master1 manifests]# puppet apply -v test16.pp
Notice: Compiled catalog for master1.master1.com in environment production in 1.71 seconds
Info: Applying configuration version '1513355751'
Info: Computing checksum on file /etc/nginx/nginx.conf
Info: /Stage[main]/Nginx::Webserver/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 0ba0cf9c84b56c8a0db3bffb55da5002
Notice: /Stage[main]/Nginx::Webserver/File[/etc/nginx/nginx.conf]/content: content changed '{md5}0ba0cf9c84b56c8a0db3bffb55da5002' to '{md5}9b2cf71797e8ad03e9812f314ad6b198'
Info: /Stage[main]/Nginx::Webserver/File[/etc/nginx/nginx.conf]: Scheduling refresh of Service[nginx]
Error: Could not update: Execution of '/usr/bin/yum -d 0 -e 0 -y list tengine' returned 1: Error: No matching Packages to list
Error: /Stage[main]/Nginx/Package[nginx]/ensure: change from absent to latest failed: Could not update: Execution of '/usr/bin/yum -d 0 -e 0 -y list tengine' returned 1: Error: No matching Packages to list
Notice: /Stage[main]/Nginx/Service[nginx]: Dependency Package[nginx] has failures: true
Warning: /Stage[main]/Nginx/Service[nginx]: Skipping because of failed dependencies
Notice: /Stage[main]/Nginx/Service[nginx]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 2.06 seconds
PS;
name +> tengine, #原来的nginx之外,额外装个tengine
7、puppet模板,content
修改参数:
[root@master1 files]# vim nginx_proxy.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes <%= @processorcount %>;
[root@master1 files]# facter -p | grep processorc
physicalprocessorcount => 1
processorcount => 1
[root@master1 files]#
[root@master1 manifests]# vim test17.pp
hasstatus => true,
restart => 'service nginx reload',
}
}
class nginx::webserver inherits nginx {
Package['nginx'] {
name +> varnish,
}
file{'/etc/nginx/nginx.conf':
source => '/root/modules/nginx/files/nginx_web.conf',
ensure => file,
notify => Service['nginx'],
}
}
class nginx::proxy inherits nginx {
file{'/etc/nginx/nginx.conf':
content => template('/root/modules/nginx/files/nginx_proxy.conf'),
ensure => file,
notify => Service['nginx'],
}
}
include nginx::proxy
[root@master1 manifests]# puppet apply -v test17.pp
Notice: Compiled catalog for master1.master1.com in environment production in 1.69 seconds
Info: Applying configuration version '1513360047'
Info: Computing checksum on file /etc/nginx/nginx.conf
Info: FileBucket got a duplicate file {md5}9b2cf71797e8ad03e9812f314ad6b198
Info: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 9b2cf71797e8ad03e9812f314ad6b198
Notice: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]/content: content changed '{md5}9b2cf71797e8ad03e9812f314ad6b198' to '{md5}0efa2f8bb50f170b0cf039c626dd25e3'
Info: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]: Scheduling refresh of Service[nginx]
Notice: /Stage[main]/Nginx/Service[nginx]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 4.11 seconds
验证配置文件:
[root@master1 nginx]# vim /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 1;
8、模块
8.1 列出本地模块
[root@master1 nginx]# puppet module list
/etc/puppet/modules (no modules installed)
/usr/share/puppet/modules (no modules installed)
8.2 创建模块
[root@master1 manifests]# mkdir -pv /etc/puppet/modules/nginx/{manifests,files,templates,tests,lib,spec}mkdir: created directory ‘/etc/puppet/modules/nginx’
mkdir: created directory ‘/etc/puppet/modules/nginx/manifests’
mkdir: created directory ‘/etc/puppet/modules/nginx/files’
mkdir: created directory ‘/etc/puppet/modules/nginx/templates’
mkdir: created directory ‘/etc/puppet/modules/nginx/tests’
mkdir: created directory ‘/etc/puppet/modules/nginx/lib’
mkdir: created directory ‘/etc/puppet/modules/nginx/spec’
[root@master1 manifests]# puppet module list
/etc/puppet/modules
└── nginx (???)
/usr/share/puppet/modules (no modules installed)
[root@master1 manifests]# cp test17.pp /etc/puppet/modules/nginx/manifests/init.pp
[root@master1 manifests]# cp /root/modules/nginx/files/nginx_web.conf /etc/puppet/modules/nginx/files/
[root@master1 manifests]# cp /root/modules/nginx/files/nginx_proxy.conf /etc/puppet/modules/nginx/templates/nginx_proxy.conf.erb
[root@master1 manifests]# vim init.pp
class nginx {
package{'nginx':
ensure => latest,
name => nginx,
} ->
service{'nginx':
enable => true,
ensure => running,
hasrestart => true,
hasstatus => true,
restart => 'service nginx reload',
}
}
class nginx::webserver inherits nginx {
Package['nginx'] {
name +> varnish,
}
file{'/etc/nginx/nginx.conf':
source => '/root/modules/nginx/files/nginx_web.conf',
restart => 'service nginx reload',
}
}
class nginx::webserver inherits nginx {
Package['nginx'] {
name +> varnish,
}
file{'/etc/nginx/nginx.conf':
source => 'puppet:///modules/nginx/nginx_web.conf',
ensure => file,
notify => Service['nginx'],
}
}
class nginx::proxy inherits nginx {
file{'/etc/nginx/nginx.conf':
content => template('nginx/nginx_proxy.conf.erb'),
ensure => file,
notify => Service['nginx'],
}
}
不真正执行,测试跑一遍:
[root@master1 manifests]# puppet apply --noop -v -e 'include nginx::proxy'
Notice: Compiled catalog for master1.master1.com in environment production in 1.91 seconds
Info: Applying configuration version '1513365502'
Notice: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]/ensure: current_value absent, should be file (noop)
Info: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]: Scheduling refresh of Service[nginx]
Notice: Class[Nginx::Proxy]: Would have triggered 'refresh' from 1 events
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: current_value absent, should be latest (noop)
Notice: /Stage[main]/Nginx/Service[nginx]/ensure: current_value stopped, should be running (noop)
Info: /Stage[main]/Nginx/Service[nginx]: Unscheduling refresh on Service[nginx]
Notice: Class[Nginx]: Would have triggered 'refresh' from 2 events
Notice: Stage[main]: Would have triggered 'refresh' from 2 events
Notice: Finished catalog run in 0.47 seconds
需要执行两次才成功,第一次报错:
[root@master1 manifests]# puppet apply -v -e 'include nginx::proxy'
Notice: Compiled catalog for master1.master1.com in environment production in 2.31 seconds
Info: Applying configuration version '1513378997'
Info: Computing checksum on file /etc/nginx/nginx.conf
Info: FileBucket got a duplicate file {md5}93bc8e01bfd45e7e18b23acc178ae25b
Info: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]: Filebucketed /etc/nginx/nginx.conf to puppet with sum 93bc8e01bfd45e7e18b23acc178ae25b
Notice: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]/content: content changed '{md5}93bc8e01bfd45e7e18b23acc178ae25b' to '{md5}0efa2f8bb50f170b0cf039c626dd25e3'
Info: /Stage[main]/Nginx::Proxy/File[/etc/nginx/nginx.conf]: Scheduling refresh of Service[nginx]
Notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
Info: /Stage[main]/Nginx/Service[nginx]: Unscheduling refresh on Service[nginx]
Notice: Finished catalog run in 4.67 seconds
[root@master1 manifests]#
转载于:https://blog.51cto.com/zhongle21/2089227