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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| [root@puppet manifests]# vim /etc/puppet/puppet.conf # 配置文件中的[main]表示全局配置段,[agent]表示只用于agent程序 [root@puppet manifests]# puppet help config # 查看配置文件帮助信息 [root@puppet manifests]# puppet config print # 打印puppet配置信息。其中的modulepath是模块的路径 [root@puppet manifests]# puppet config print modulepath /etc/puppet/modules:/usr/share/puppet/modules # 只显示modulepath一项 [root@puppet manifests]# puppet help module # 查看模块配置帮助 [root@puppet manifests]# puppet module list /etc/puppet/modules (no modules installed) /usr/share/puppet/modules (no modules installed) # 查看模块信息 [root@puppet manifests]# puppet module search nginx Notice: Searching https://forgeapi.puppetlabs.com ... # 到互联网搜索与nginx相关的模块 [root@puppet manifests]# puppet module install oris-nginx Notice: Preparing to install into /etc/puppet/modules ... Notice: Downloading from https://forgeapi.puppetlabs.com ... Notice: Installing -- do not interrupt ... /etc/puppet/modules └─┬ oris-nginx (v1.3.0) ├─┬ puppetlabs-apt (v6.3.0) │ └── puppetlabs-translate (v1.2.0) └── puppetlabs-stdlib (v5.2.0) # 安装模块oris-nginx [root@puppet ~]# mkdir modules [root@puppet ~]# cd modules/ [root@puppet modules]# mkdir -pv chrony/{manifests,files,templates,lib,spec,tests} [root@puppet modules]# cd chrony/manifests/ [root@puppet manifests]# vim init.pp # 创建清单,名字不能变,都要叫init.pp class chrony { # 这个清单中必须有一个与模块名相同的类的名字。模块名就是module目录中的目录的名字 package{'chrony': ensure => latest, } -> file{'chrony.conf': path => '/etc/chrony.conf', source => 'puppet:///modules/chrony/chrony.conf', # 这是源路径,表示/etc/puppet/modules/chrony/files目录,只是files目录是不用写,可以自动找到 } ~> service{'chronyd': ensure => running, enable => true, } } [root@puppet manifests]# cp /etc/chrony.conf /root/modules/chrony/files/ [root@puppet modules]# vim chrony/files/chrony.conf #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst # 注释三行 [root@puppet modules]# cp -a /root/modules/chrony/ /etc/puppet/modules/ # 配置好模块后,要将整个目录都复制到puppet的模块目录。上面创建的modules目录只是为了临时编辑用的,编辑好后是要复制到指定目录的,指定目录是通过puppet config print modulepath命令查看到的两个目录都可以 [root@puppet modules]# puppet module list /etc/puppet/modules ├── chrony (???) ├── oris-nginx (v1.3.0) ├── puppetlabs-apt (v6.3.0) ├── puppetlabs-stdlib (v5.2.0) └── puppetlabs-translate (v1.2.0) /usr/share/puppet/modules (no modules installed) # 现在就能看到chrony模块了 [root@puppet modules]# puppet apply -v -d --noop -e 'include chrony' # 使用-e调用模块 [root@puppet modules]# puppet apply -v -e 'include chrony' [root@puppet modules]# cat /etc/chrony.conf # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst # 可以看到配置文件已被修改 [root@puppet modules]# mv /etc/puppet/modules/nginx/ /root # 移走上面安装的nginx模块 [root@puppet modules]# mkdir -pv nginx/{manifests,files,templates,spec,lib,tests} [root@puppet modules]# vim nginx/manifests/init.pp # 定义一个基类 class nginx { package{'nginx': ensure => latest, } service{'nginx': ensure => running, enable => true, } } [root@puppet modules]# vim nginx/manifests/webproxy.pp # 定义子类。如果代码较多,可以定义多个子类,但一定要有一个与清单名一样的类名称。参考官方文档,3.8版本以后的版本中,资源清单文件的文件名要与子类名一样 class nginx::webproxy inherits nginx { file{'nginx.conf': path => '/etc/nginx/nginx.conf', source => 'puppet:///modules/nginx/nginx-webproxy.conf', } Package['nginx'] -> File['nginx.conf'] ~> Service['nginx'] } [root@puppet modules]# cp /root/manifests/nginx-webproxy.conf nginx/files/ [root@puppet modules]# cp -a nginx/ /etc/puppet/modules/ [root@puppet modules]# puppet module list /etc/puppet/modules ├── chrony (???) ├── nginx (???) [root@puppet manifests]# puppet apply -v -d --noop -e 'include nginx::webproxy' ============================================================================================== 注意: 1. puppet 3.8及以后的版本中,资源清单文件的文件名要与文件听类名保持一致,例如某子类名为“base_class::child_class”,其文件名应该为child_class.pp; 2. 无需再资源清单文件中使用import语句; 3. manifests目录下可存在多个清单文件,每个清单文件包含一个类,其文件名同类名; ============================================================================================== [root@puppet manifests]# pwd /etc/puppet/modules/nginx/manifests [root@puppet manifests]# vim web.pp class nginx::web inherits nginx { file{'nginx.conf': path => '/etc/nginx/nginx.conf', content => 'template('nginx/nginx.conf.erb')', # 这里应该是一个相对路径,也就是/etc/puppet/modules下的nginx目录,这里可以省略nginx目录中的template目录,直接写template目录中的nginx.conf.erb即可。 } Package['nginx'] -> File['nginx.conf'] ~> Service['nginx'] } [root@puppet manifests]# cp /root/manifests/nginx.conf.erb ../templates/ [root@puppet manifests]# puppet apply -v -d --noop -e 'include nginx::web'
[root@puppet manifests]# cd /root/modules/ [root@puppet modules]# mkdir -pv redis/{manifests,files,templates,spec,lib,tests} 下载一个redis-3.2.8-1.el7.x86_64.rpm到 /root/modules/redis/tests目录中 [root@puppet modules]# vim redis/manifests/init.pp class redis { $redispkg='redis-3.2.8-1.el7.x86_64.rpm' package{'redis': ensure => installed, provider => yum, source => "puppet:///modules/redis/$redispkg", } service{'redis': ensure => running, enable => true, } } [root@puppet modules]# cp -a redis/ /etc/puppet/modules/ [root@puppet modules]# puppet module list /etc/puppet/modules ├── chrony (???) ├── nginx (???) ├── puppetlabs-apt (v6.3.0) ├── puppetlabs-stdlib (v5.2.0) ├── puppetlabs-translate (v1.2.0) └── redis (???) [root@puppet modules]# puppet apply -v -d --noop -e 'include redis'
|