1、Puppet條件語句和類node
Puppet2.7系列的版本支持使用3種條件判斷語句,包括:if,case,selector,puppet也支持使用class是用於通用目標的一組資源,所以,它是命名的代碼塊,在某位置建立以後可在puppet全局使用,相似於其餘編程語言中類的功能,puppet的class能夠被繼承,也能夠包含子類。linux
2、Puppet模塊nginx
到目前爲止,資源申報、定義類、聲明類等全部功能都只能再一個manifest文件中實現,但這卻非最有效的基於puppet管理IT基礎架構的方式,實踐中,通常須要把manifest文件分解成易於理解的結構,例如將類文件,配置文件、模塊文件等分類存放,而且經過某種機制在必要時將它們整合起來,這種機制就是「模塊」,它有助於以結構化、層次化的方式使用puppet,而puppet則基於「模塊自動裝載器」完成模塊裝載,模塊實際就是一個按約定的、預約義的結構存放了多個文件或子目錄的目錄,目錄裏的這些文件或子目錄必須遵循其命名規範,puppet會按此種規範在特定位置查找所須要的模塊文件,不過,這些特定的目錄也能夠經過puppet的配置參數modulepath定義。web
首先來演示下if語句的使用,if又包括單分支,雙分支,多分支語句apache
[root@node1 ~]# vi test6.pp if $operatingsystem =~ /(?i-mx:^(centos|fedora|redhat))/ { $webserver = 'httpd' } elsif operatingsystem =~ /(?i-mx:^(debian|ubuntu))/ { $webserver = 'apache2' } else { $webserver = undef notice('Unkown OS') } package {"$webserver": ensure => installed, } [root@node1 ~]# puppet apply test6.pp notice: /Stage[main]//Package[httpd]/ensure: created notice: Finished catalog run in 2.91 seconds [root@node1 ~]# rpm -q httpd httpd-2.2.15-30.el6.centos.x86_64 # 使用多分支if判斷語句,operatingsystem頂級變量,根據當前系統安裝所要安裝的程序包
case語句,相似if語句,case語句會從多個代碼塊中選擇一個分支執行,這跟其餘編程語言中的case語句功能一致,case語句會接受一個控制表達式和一組case代碼塊,並執行第一個匹配到控制表達式的塊編程
[root@node1 ~]# vi test7.pp case $operatingsystem { 'Solaris': {notice("Welcome to Solaris")} 'RedHat','CentOS': {notice("Welcome to RedHat OSFamily")} /^(Debian|Ubuntu)$/: {notice("Welcome to $1 linux")} default: {notice("Welcome,alien *_*")} } [root@node1 ~]# puppet apply test7.pp notice: Scope(Class[main]): Welcome to RedHat OSFamily notice: Finished catalog run in 0.01 seconds
selector語句,selector只能用於指望出現直接值的地方,這包括變量賦值,資源屬性,函數參數,資源標題,其餘selector的值及表達式,selector不能用於一個已經嵌套於selector的case中,也不能用於一個已經嵌套於case的case語句中
ubuntu
[root@node1 ~]# vi test8.pp $webserver = $operatingsystem ? { /(?i-mx:ubuntu|debian)/ => 'apache2', /(?i-mx:centos|fedora|redhat)/ => 'httpd', } notify {"$webserver": message => "install $webserver", } [root@node1 ~]# puppet apply test8.pp notice: install httpd notice: /Stage[main]//Notify[httpd]/message: defined 'message' as 'install httpd' notice: Finished catalog run in 0.02 seconds
class的使用
centos
[root@node1 ~]# vi test9.pp class nginx { package {'nginx': ensure => installed, before => File['/etc/nginx/nginx.conf'], } file {'/etc/nginx/nginx.conf': ensure => file, mode => 0644, owner => 'root', group => 'root', source => '/tmp/nginx.conf', notify => Service['nginx'], } service { 'nginx': ensure => running, } } include nginx # 使用include來調用class [root@node1 ~]# puppet apply test9.pp notice: /Stage[main]/Nginx/Package[nginx]/ensure: created notice: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4' notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: /Stage[main]/Nginx/Service[nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 2.16 seconds # 也可使用如下方式調用,這種方式能夠傳遞參數 class nginx { package {'nginx': ensure => installed, before => File['/etc/nginx/nginx.conf'], } file {'/etc/nginx/nginx.conf': ensure => file, mode => 0644, owner => 'root', group => 'root', source => '/tmp/nginx.conf', notify => Service['nginx'], } service { 'nginx': ensure => running, } } class {'nginx':} [root@node1 ~]# puppet apply test9.pp notice: /Stage[main]/Nginx/Package[nginx]/ensure: created notice: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4' notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: /Stage[main]/Nginx/Service[nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 2.06 seconds
使用class方法調用類,還能夠傳遞參數實現,根據參數來安裝不一樣的服務,好比想安裝tengine
bash
[root@node1 ~]# vi test9.pp class webserver ($wbsvr='nginx') { package {"$wbsvr": ensure => installed, before => File['/etc/nginx/nginx.conf'], } file {'/etc/nginx/nginx.conf': ensure => file, mode => 0644, owner => 'root', group => 'root', source => '/tmp/nginx.conf', notify => Service['nginx'], } service { 'nginx': ensure => running, } } class {'webserver': wbsvr => 'nginx', } [root@node1 ~]# puppet apply test9.pp notice: /Stage[main]/Webserver/Package[nginx]/ensure: created notice: /Stage[main]/Webserver/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4' notice: /Stage[main]/Webserver/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: /Stage[main]/Webserver/Service[nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 2.09 seconds [root@node1 ~]# vi test9.pp class webserver ($wbsvr='nginx') { package {"$wbsvr": ensure => installed, before => File['/etc/nginx/nginx.conf'], } file {'/etc/nginx/nginx.conf': ensure => file, mode => 0644, owner => 'root', group => 'root', source => '/tmp/nginx.conf', notify => Service['nginx'], } service { 'nginx': ensure => running, } } class {'webserver': wbsvr => 'tenginx', } [root@node1 ~]# puppet apply test9.pp err: /Stage[main]/Webserver/Package[tenginx]/ensure: change from absent to present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y install tenginx' returned 1: Error: Nothing to do notice: /Stage[main]/Webserver/File[/etc/nginx/nginx.conf]: Dependency Package[tenginx] has failures: true warning: /Stage[main]/Webserver/File[/etc/nginx/nginx.conf]: Skipping because of failed dependencies notice: /Stage[main]/Webserver/Service[nginx]: Dependency Package[tenginx] has failures: true warning: /Stage[main]/Webserver/Service[nginx]: Skipping because of failed dependencies notice: Finished catalog run in 0.41 second
類也能夠被繼承,實現根據不一樣狀況調用不一樣的類架構
[root@node1 ~]# vi test10.pp class nginx { package {'nginx': ensure => installed, } } class nginx::websvr inherits nginx { file {'/etc/nginx/nginx.conf': ensure => file, mode => '0644', owner => 'root', group => 'root', require => Package['nginx'], source => '/tmp/nginx.conf', } service {'nginx': ensure => running, } } class nginx::rproxy inherits nginx { file {'/etc/nginx/nginx.conf': ensure => file, mode => '0644', owner => 'root', group => 'root', require => Package['nginx'], source => '/tmp/nginx-proxy.conf', } service {'nginx': ensure => running, } } include nginx::websvr [root@node1 ~]# puppet apply test10.pp notice: /Stage[main]/Nginx/Package[nginx]/ensure: created notice: /Stage[main]/Nginx::Websvr/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4' notice: /Stage[main]/Nginx::Websvr/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: Finished catalog run in 1.81 seconds # 調用nginx::rproxy類 [root@node1 ~]# vi test10.pp class nginx { package {'nginx': ensure => installed, } } class nginx::websvr inherits nginx { file {'/etc/nginx/nginx.conf': ensure => file, mode => '0644', owner => 'root', group => 'root', require => Package['nginx'], source => '/tmp/nginx.conf', } service {'nginx': ensure => running, } } class nginx::rproxy inherits nginx { file {'/etc/nginx/nginx.conf': ensure => file, mode => '0644', owner => 'root', group => 'root', require => Package['nginx'], source => '/tmp/nginx-proxy.conf', } service {'nginx': ensure => running, } } include nginx::rproxy [root@node1 ~]# puppet apply test10.pp notice: Finished catalog run in 0.15 seconds
puppet模塊的建立和使用
# 安裝puppet-server [root@node1 ~]# rpm -ivh puppet-server-2.7.23-1.el6.noarch.rpm
# 建立模塊目錄結構 [root@node1 ~]# mkdir -pv /etc/puppet/modules/nginx/{manifests,files,lib,templates,tests,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/lib' mkdir: created directory `/etc/puppet/modules/nginx/templates' mkdir: created directory `/etc/puppet/modules/nginx/tests' mkdir: created directory `/etc/puppet/modules/nginx/spec' [root@node1 ~]# puppet module list /etc/puppet/modules └── nginx (???) /usr/share/puppet/modules (no modules installed)
MODULE NAME:模塊名稱,只能以小寫字母開頭,也能夠包含小寫字母、數字和下劃線,但不能使用「main」或者「settings」做爲模塊名;
manifests目錄:包含當前模塊的全部manifest文件;每一個mainfest文件必須包含一個類或一個定義的類,此文件訪問路徑格式爲"ModuleName::[SubDircetoryName::]ManifestFileName",注意manifest文件不須要其後綴.pp;
init.pp:只能包含一個單獨的類定義,且類的名稱必須與模塊名稱相同;
files目錄:包含了一組靜態文件,這些文件可被節點下載使用,每一個文件的訪問路徑遵循
puppet:///modules/MODULE_NAME/filename路徑格式;
lib目錄:插件目錄,經常使用於自定義fact及自定義資源類型等;
templates目錄:存儲了manifest用到的模板文件,其訪問路徑遵循template('ModulName/TemplateName')格式;
tests目錄:當前模塊的使用幫助或使用範例文件,相似如何聲明當前模塊中的類定義的類型等;
spec目錄:相似於tests目錄的功能,只不過,其是爲lib目錄中定義的各插件提供使用的範例的;
在nginx模塊中定義init.pp
[root@node1 ~]# vi /etc/puppet/modules/nginx/manifests/init.pp class nginx { package {'nginx': ensure => installed, } }
定義nginx_web.pp文件
[root@node1 ~]# vi /etc/puppet/modules/nginx/manifests/nginx_web.pp class nginx::nginx_web inherits nginx { file {'/etc/nginx/nginx.conf': ensure => file, source => 'puppet:///modules/nginx/nginx-web.conf', mode => '0644', owner => 'root', group => 'root', notify => Service['nginx'], require => Package['nginx'], } service {'nginx': ensure => running, } } # 準備source文件 [root@node1 ~]# cp /tmp/nginx.conf /etc/puppet/modules/nginx/files/nginx-web.conf
定義nginx_proxy.pp文件
[root@node1 ~]# vi /etc/puppet/modules/nginx/manifests/nginx_proxy.pp 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', notify => Service['nginx'], require => Package['nginx'], } service {'nginx': ensure => running, } } # 準備source文件 [root@node1 ~]# cp /tmp/nginx.conf /etc/puppet/modules/nginx/files/nginx-proxy.conf
建立site.pp文件調用前面定義的class
[root@node1 ~]# vi /etc/puppet/manifests/site.pp node 'node1.luojianlong' { include nginx::nginx_web }
執行操做
[root@node1 ~]# puppet apply /etc/puppet/manifests/site.pp notice: /Stage[main]/Nginx/Package[nginx]/ensure: created notice: /Stage[main]/Nginx::Nginx_web/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4' notice: /Stage[main]/Nginx::Nginx_web/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: /Stage[main]/Nginx::Nginx_web/Service[nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 2.05 seconds
這樣就是使用nginx-web.conf配置文件啓動的nginx
也可使用nginx-proxy.conf,啓動
[root@node1 ~]# vi /etc/puppet/manifests/site.pp node 'node1.luojianlong' { include nginx::nginx_proxy } [root@node1 ~]# yum -y remove nginx [root@node1 ~]# puppet apply /etc/puppet/manifests/site.pp notice: /Stage[main]/Nginx/Package[nginx]/ensure: created notice: /Stage[main]/Nginx::Nginx_proxy/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4' notice: /Stage[main]/Nginx::Nginx_proxy/Service[nginx]/ensure: ensure changed 'stopped' to 'running' notice: /Stage[main]/Nginx::Nginx_proxy/Service[nginx]: Triggered 'refresh' from 1 events notice: Finished catalog run in 2.11 seconds # 執行成功
到此,Puppet條件語句,class,module的使用就介紹這麼多了。