顯示當前系統裝載的所有模塊信息
[root@node3 class]# puppet module list
/etc/puppet/modules (no modules installed)
/usr/share/puppet/modules (no modulesinstalled)
no modules installed說明沒有任何模塊
在puppet的官方站點已經有許多模塊提供給我們使用
第三方模塊未必使用,但是對其框架進行修改有的時候是必要的,所以精確理解模塊是什麼
而且在master/agent模型下,幾乎都是基於模塊進行實現的
如果我們想查看有哪些模塊提供使用只要使用命令puppetmodule search [關鍵字]
如下所示
[root@node3 class]# puppet module search apache
Notice: Searching https://forgeapi.puppetlabs.com ...
NAME DESCRIPTION AUTHOR KEYWORDS
example42-apache Puppet module forapache @example42 example42,apache
如果看到有需要的則進行安裝就可以了,如下所示
[root@node3 ~]# puppet module install 模塊名
模塊的查看
[root@node3 ~]# puppet module list
這個命令上面已經提到了
查看幫助信息
[root@node3 class]# puppet help master/agent
模塊的創建
假如我們想創建一個能夠管理nginx的模塊,那麼這個模塊名也叫nginx,由此:
[root@node3 class]# mkdir -p./nginx/{manifests,files,lib,templates,test,spec}
創建完其模塊對應目錄後,我們來查看模塊列表
[root@node3 ~]# mkdir -p/etc/puppet/modules/nginx/{manifests,files,lib,templates,test,spec}
[root@node3 ~]# puppet module list
/etc/puppet/modules
└──nginx (???)
/usr/share/puppet/modules (no modules installed)
在/usr/share/puppet/modules/nginx/manifests這個目錄下至少需要一個清單文件,叫init.pp 並且能有一個類,這個類還得與當前模塊同名
因此我們爲其創建一個空類
[root@node3 ~]# cd /etc/puppet/modules/nginx/manifests/
創建清單文件,文件名必須是init.pp
拷貝配置文件置modules目錄
[root@node3 manifests]# cp /etc/nginx/nginx.conf /etc/puppet/modules/nginx/files/
執行模塊
[root@node3 manifests]# puppet apply/etc/puppet/modules/nginx/manifests/init.pp
[root@node3 manifests]# cat init.pp
class nginx {
package{'nginx':
ensure =>installed,
}
}
class nginx::web inherits nginx {
file{'/etc/nginx/nginx.conf':
ensure =>file,
mode =>0644,
owner =>root,
group =>root,
source =>'puppet:///modules/nginx/nginx.conf',
require =>Package['nginx'],
}
service{'nginx':
ensure =>true,
subscribe=> File['/etc/nginx/nginx.conf'],
}
}
include nginx::web
配置站點清單
每個節點在所謂的master/agent的模式下,angent需要聯繫master,通過master爲自己生成catalog,所以在這種場景下爲某個節點定義配置的時候,需要通過站點清單來實現的
像剛纔我們定義的manifests目錄,是定義在模塊中的
而在puppet配置文件目錄下還需有一個目錄爲master/angent模式提供清單
安裝puppet-server
[root@node3 manifests]# yum install puppet-server
[root@node3 manifests]# cd /etc/puppet/
[root@node3 puppet]# ll
total 100
-rw-r--r-- 1 root root 175 Aug 21 13:54 1
-rw-r--r-- 1 root root 4178 Jun 10 05:09auth.conf
drwxr-xr-x 3 root root 4096 Aug 27 13:35environments
-rw-r--r-- 1 root root 1462 Jun 10 05:08fileserver.conf
drwxr-xr-x 2 root root 4096 Jun 10 05:09manifests
drwxr-xr-x 3 root root 4096 Aug 27 11:22 modules
-rw-r--r-- 1 root root 853 Jun 10 05:08puppet.conf
-rw-r--r-- 1 root root 63344 Aug 22 15:52 puppet.conf.rpmsave
drwxrwx--x 8 puppet puppet 4096 Aug 21 10:21 ssl
其中,/etc/puppet/manifests 爲站點清單目錄,是用來定義各節點的清單,哪個agent到底使用哪個類,就在這裏定義
在manifests目錄下創建站點清單文件
[root@node3 puppet]# cd manifests
[root@node3 manifests]# ls
site.pp
要聲明一個站點就要使用node加節點的fqdn ,確保其一定可以被解析,否則無效
[root@node3 manifests]# hostname
node3.test.com
清單文件名必須要以site.pp來命名,否則模塊是不會生效的
node 'node3' { #明確指定哪個節點使用哪個模塊
include nginx #說明文件使用哪個類
}
之後還需要定義模塊
[root@node3 manifests]# vim /etc/puppet/modules/nginx/manifests/init.pp
將include nginx 去掉,因爲模塊只有被調用的時候才執行,這樣纔是真正意義上的模塊
應用模塊
[root@node3 manifests]# puppet apply/etc/puppet/manifests/site.pp
使用繼承類
[root@node3 manifests]# pwd
/etc/puppet/modules/nginx/manifests
將其改名
[root@node3 manifests]# mv init.pp nginxweb.pp
聲明一個空類
[root@node3 manifests]# cat init.pp
class nginx {
}
而後再編輯nginxweb.pp
更改參數如下
class nginx::web inherits nginx
使其繼承父類
再創建一個類爲tengine.pp
[root@node3 manifests]# cp nginxweb.pp tengine.pp
[root@node3 manifests]# vim tengine.pp
更改參數:
class nginx {
package{'nginx':
ensure=> installed,
}
}
class nginx::tengine inherits nginx {
file{'/etc/nginx/nginx.conf':
ensure => file,
mode =>0644,
owner =>root,
group =>root,
source=> 'puppet:///modules/nginx/tengine.conf',
require=> Package['nginx'],
}
service{'nginx':
ensure=> true,
subscribe =>File['/etc/nginx/nginx.conf'],
}
}
include nginx::tengine
這樣我們想將站點1安裝nginx,而不是安裝tengine
在我們站點清單的裏聲明即可
[root@node3 manifests]# vim site.pp
node 'node3.test.com' {
includenginx::tengine
}
應用測試
puppet apply site.pp
Warning: Could not retrieve fact fqdn
Warning: Host is missing hostname and/or domain: node3
Notice: Compiled catalog for node3 in environment production in 0.92 seconds
Error: Execution of '/usr/bin/yum -d 0 -e 0 -y list tengine' returned 1: Error:No matching Packages to list
Error: /Stage[main]/Nginx::Tengine/Package[tengine]/ensure: change from absentto present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y list tengine'returned 1: Error: No matching Packages to list
Notice: /Stage[main]/Nginx::Tengine/File[/etc/nginx/nginx.conf]: DependencyPackage[tengine] has failures: true
Warning: /Stage[main]/Nginx::Tengine/File[/etc/nginx/nginx.conf]: Skippingbecause of failed dependencies
Notice: /Stage[main]/Nginx::Tengine/Service[nginx]: Dependency Package[tengine]has failures: true
Warning: /Stage[main]/Nginx::Tengine/Service[nginx]: Skipping because of faileddependencies
Notice: Finished catalog run in 1.38 seconds
很顯然我們是定義的tengine類 而且yum源中沒有這個rpm 所以是不會被安裝的
這樣一來我們可以將各個類放在不同的文件中了
假如node1運行的時候期望運行的是webserver 而另外一個節點則裝載的是反向代理的配置
很顯然它們之間的配置是不一樣的,但是它們有些東西是一樣的,比如安裝server 但是裝載的文件不一樣,因此我們可以更進一步分割類
區分兩者不同
puppet類不支持多重繼承,因此不能多次繼承,也不能直接繼承多個類
但是我們可以使init.pp進行安裝nginx ,其他模塊繼承init.pp
[root@node3 manifests]# pwd
/etc/puppet/modules/nginx/manifests
使init.pp只負責安裝
class nginx {
package {'nginx':
allow_virtual => false,
ensure=> installed,
}
}
修改nginxweb.pp使其只配置nginx web server 其父類是nginx
#只需要將之前的package類剪切至init.pp即可
[root@node3 manifests]# cat nginx_web.pp
class nginx::web inherits nginx {
file{'/etc/nginx/nginx.conf':
ensure =>file,
mode =>0644,
owner =>root,
group =>root,
source =>'puppet:///modules/nginx/nginx.conf',
require =>Package['nginx'],
}
service{'nginx':
ensure =>true,
subscribe=> File['/etc/nginx/nginx.conf'],
}
}
創建nginx_proxy.pp 文件,使其只配置proxy功能
class nginx::nginx_proxy inherits nginx {
file {'/etc/nginx/nginx.conf':
ensure=> file,
source=> 'puppet:///modules/nginx/nginx-proxy.conf',
mode =>0644,
owner =>root,
group =>root,
require=> Package['nginx'],
notify=> Service['nginx'],
}
service {'nginx':
ensure => running,
}
}
回到/etc/puppet/manifests目錄
我們讓其安裝nginx_web,如下所示:
[root@node3 manifests]# vim site.pp
node 'node3' {
include nginx::nginx_web
}
執行腳本並查看結果
[root@node3 manifests]# puppet apply site.pp
Notice: Compiled catalog for node3 in environment production in 1.25 seconds
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Notice: /Stage[main]/Nginx::Nginx_web/Service[nginx]/ensure: ensure changed'stopped' to 'running'
Notice: Finished catalog run in 21.85 seconds
定義模塊需要注意的是
site.pp 文件中的inculude 、模塊的名稱 、還有定義的class 的名稱必須保持一致 否則會報錯