運維自動化之puppet基礎應用解析

博文綱要:

一、基礎原理介紹;

 二、單機模式安裝;

 三、資源定義說明及應用;

 四、語法詳細闡述;

 五、Master/Agent安裝;

一、基礎原理介紹

Puppet是開源的基於Ruby的系統配置管理工具依賴於C/S的部署架構。有兩種使用環境單機使用、master/agent這兩種環境在配置文件上沒有區別可能在運行機制上有着一定的區別的。

puppet是一個IT基礎設施自動化管理工具它能夠幫助系統管理員管理基礎設施的整個生命週期供應provisioning與cobbler中不一樣這裏是保證軟件包安裝的、配置configuration、聯動orchestration及報告reporting。也就是說它可以幫助系統管理員更加方便的完成多臺服務器的安裝升級軟件包、管理配置文件、系統服務、cron任務、添加新的配置、修復錯誤等重複工作和快速部署關鍵性的應用以及在本地或雲端完成自主管理變更和快速擴展架構規模等。

puppet2.7.0之前遵循GPL協議是基於ruby語言開發的也就是說在部署時需要部署ruby環境但是在puppet2.7.0以後不再使用GPL協議而是使用apache 2.0 license開源協定。

puppet對於系統管理員是抽象的只依賴於ruby與facter進行工作所以說對於管理員只要能夠配置好ruby的運行環境能夠定製或者根據facter來檢索各節點特性來定製好一個配置管理或者說配置文件就可以了。

puppet能夠管理多達40多種的資源比如file、user、group、host、package、service、cron、exec、yum repo等等所以說適合管理整個軟件生命週期。

puppet的工作模型puppet通過聲明性、基於模型的方法進行IT自動化管理

定義通過puppet的聲明性配置語言定義基礎設置配置的目標狀態

模擬強制應用改變的配置之前先進行模擬性的應用

強制自動、強制應用部署達成目標狀態糾正任何偏離的配置

報告報告當下狀態及目標狀態的不同以及達成目標狀態所進行的任何強制性改變

puppet的抽象模型三種模型

配置語言層有自己獨立的配置語言;

事務層用來管理各資源之間有依賴關係的資源應用時彼此之間誰先誰後等聯動的

資源抽象層用來定義管理的資源對象的並且將每一個應該定義的資源屬性抽象出來而且提供了基本的實例化接口。

puppet從以下三個維度來對資源完成抽象(1)相似的資源被抽象成同一種資源類型如程序包資源、用戶資源及服務資源等(2)將資源屬性或狀態的描述與其實現方式剝離開來如僅說明安裝一個程序包而不用關心其具體是通過yum、pkgadd、ports、或者其他方式實現(3)僅描述資源的目標狀態也即期望其實現的結果而不是其具體過程如“確定nginx運行起來”而不是具體描述爲“運行nginx命令將其啓動起來”這三個也被稱作puppet的資源抽象層。

模塊中包含類、文件、模板、使用樣例模塊是通用的調用時只能調用其中的類所以也就是說每個模塊就是導入一大堆的類以及部分類所依賴的外部文件。

>>問題什麼是模塊 >>答案模塊就是包含了一大堆.pp結尾的文件的目錄。

>>問題怎麼調用其中的類 >>答案定義節點每個節點上定義其要應用的類。

>>問題資源和類定義的什麼地方 >>答案首先說明這個地方不是位置是保存至文件中還是數據庫中。資源必須只能定義在文件中而這個定義時所在的文件叫做資源清單manifest而這些清單文件通常都以.pp結尾。

二、單機安裝熟悉應用

###########首先有些系統上裝有puppet將其卸載重新安裝############

[root@node ~]# rpm -q puppet       #查看是否安裝程序

puppet-2.6.18-3.el6.noarch         #版本太老已被廢棄

[root@node ~]# rpm -e puppet       #卸載

warning: /etc/puppet/puppet.conf saved as /etc/puppet/puppet.conf.rpmsave    #默認保留了配置文件

[root@node ~]# rm -rf /etc/puppet/ #將其目錄刪除

###########然後下載兩個程序文件#######################

puppet-2.7.23-1.el6.noarch.rpm

facter-1.7.3-1.el6.x86_64.rpm

###########升級faster#################################

[root@node ~]# rpm -Uvh facter-1.7.3-1.el6.x86_64.rpm

###########安裝puppet#################################

[root@node ~]# yum -y localinstall puppet-2.7.23-1.el6.noarch.rpm

三、資源定義說明和應用

<一>定義

puppet的代碼主要就是由這些資源和資源的屬性構成. 每個資源都包含有類型、標題和一些其他屬性的列表。

這是一個典型的resource資源的結構

type {'title':  

      attribute => value,

    }

(1)、type定義資源的類型使用花括號標起來它是有規定的並不是自己隨便寫的在puppet的官方文檔中有所有的可用type。

(2)、title定義標題標題可以使用單引號或雙引號標記起來不同的title表示不同的資源。

(3)、attribute定義參數屬性每一個參數都有一個值是用來告訴puppet怎樣去配置資源的。

(4)、屬性和值得結構attribute => 'value'。

(5)、每一個屬性鍵值對後面都要有一個逗號最後一個鍵值對後面可以使用逗號也可以不使用。

(6)、如果一個資源只有一個屬性它可以被聲明成一行像這樣:file { "/etc/group": owner =>"root" }

(7)、多個資源可以被配置在一個資源類型裏面如

file {

"/etc/passwd":

ensure => present;

"/etc/group" :

owner => "root",

group => "root";

}

<二>應用

查看常見資源類型

1

[root@node ~]# puppet describe -l

查看資源的定義格式

1

[root@node ~]# puppet describe user

常見的資源類型的定義方式

1、使用puppet定義通知

#########查詢此資源定義方式###############

[root@node ~]# puppet describe notify

#########定義資源###########################

[root@node puppets]# vim notify.pp

notify {'notice':

    message => "hell puppet",

}

#########本地執行查看資源定義結果#########

[root@node puppets]# puppet apply  notify.pp

notice: hell puppet         #顯示信息

notice: /Stage[main]//Notify[notice]/message: defined 'message' as 'hell puppet'       #生成的報告

notice: Finished catalog run in 0.05 seconds    #執行過程所用時間

2、使用puppet安裝軟件

package資源管理系統的軟件包安裝默認是yum源/etc/yum.repo.d/的通過puppet來自動安裝的軟件包均是通過yum來安裝的所以需要配置好yum源,該資源的主要屬性是ensure;設置該軟件包應該在什麼狀態. installed 表示要安裝該軟件。

package常見定義方式

packae {"package name":

    ensure => {installed|absent|latest|version|purged},

    #installed    只要存在即可或present

    #absent     刪除無依賴當別的軟件包依賴時不可刪除

    #latest     升級到最新版本

    #version    指定安裝具體的某個版本號yum list格式ensure => "版本號",

    #purged     刪除該包和依賴包(有風險勿用)

    name => "package name",     #應該省略寫到title部分即可

}

實例演示

#############定義資源###############

[root@node puppets]# vim package.pp

package {'nginx':

    ensure => installed,

}

############本地執行##############

[root@node puppets]# puppet apply  package.pp

notice: /Stage[main]//Package[nginx]/ensure: created      #安裝成功

notice: Finished catalog run in 33.93 seconds             #使用時間

############查看安裝結果##########

[root@node puppets]# rpm -q nginx

nginx-1.0.15-5.el6.x86_64

3、使用puppet定義服務啓動服務

service常見定義方式

service {"title":                       #title爲服務名,nginx

    ensure => {running|stopped},        #當前service的狀態

    enable => {true|false},             #service是否開機啓動

    {status|start|stop|restart} => "cmd ",    #指定命令的路徑當且僅當啓動腳本不在/etc/init.d/下的才需要一般用於源碼包安裝的且命令不在/etc/init.d/下的

    hasrestart => {true|false},       #重啓service的步驟true->restart; false->stop,start

    hasstatus => {true|false},        #是從命令行查詢還是從進程表有沒有該進程中查詢service的狀態

}

實例演示

##############卸載################

[root@node puppets]# rpm -e nginx    #卸載nginx

#############定義資源#############

[root@node puppets]# vim package.pp

package {'nginx':

    ensure => installed,

}

service {'nginx':      #同一類資源不可重名不同資源可以使用同一名稱

    ensure => true,

}

#############本地執行############

[root@node puppets]# puppet apply  package.pp

notice: /Stage[main]//Package[nginx]/ensure: created

notice: /Stage[main]//Service[nginx]/ensure: ensure changed 'stopped' to 'running'

notice: Finished catalog run in 31.77 seconds

#############查看監聽端口########

[root@node puppets]# ss -tanl

LISTEN     0      128                    *:80                    *:*

小擴展如果這裏沒有定義資源package直接定義service若軟件沒有安裝會報錯由此可知資源間存在依賴關係那如何定義資源間的依賴關係可使用資源引用如何進行資源引用格式爲類型['資源名稱']注意在定義時類型的首字母必須要大寫

4、定義資源間依賴關係

#######兩個關鍵字分開定義########

before  before => 資源引用      #後面的資源引用之前當前資源必須存在

require require => 資源引用     #後面的資源定義過當前資源纔可啓用

######示例###########################

[root@node puppets]# vim package.pp

package {'nginx':

    ensure => installed,

}

service {'nginx':

    ensure => true,

    require => Package['nginx'],

}

############手動停止服務############

[root@node puppets]# service nginx stop

Stopping nginx:                                            [  OK  ]

############執行####################

[root@node puppets]# puppet apply  package.pp

notice: /Stage[main]//Service[nginx]/ensure: ensure changed 'stopped' to 'running'

notice: Finished catalog run in 0.83 seconds

[root@node puppets]# rpm -e nginx       #卸載軟件

[root@node puppets]# puppet apply  package.pp

notice: /Stage[main]//Package[nginx]/ensure: created

notice: /Stage[main]//Service[nginx]/ensure: ensure changed 'stopped' to 'running'

notice: Finished catalog run in 10.83 seconds

5、使用puppet定義組

group常用的定義方式

group {"title":                      #組名同name一樣

    name => "用戶名",               #組名可以不寫默認與title一樣

    ensure => {present|absent},     #指定組的目標狀態present 該組不存在則生成;absent 該組存在則刪除 

    gid => "值",                    #定義組的ID

}

實例演示

[root@node puppets]# vim group.pp

group {'zhao':

    ensure => 'present',

    gid => 1001,

}

[root@node puppets]# puppet apply  group.pp

notice: /Stage[main]//Group[zhao]/ensure: created     #已創建

notice: Finished catalog run in 0.38 seconds

[root@node puppets]# tail -2 /etc/group    #查看結果

nginx:x:492:

zhao:x:1001:      #由此可看出若不定義用戶名(name)會默認使用標題名

6、使用puppet定義用戶

user常用定義方式

user {"title":                      #用戶名同name一樣

    name => "用戶名",               #用戶名可以不寫默認與title一樣

    ensure => {present|absent},     #指定用戶的目標狀態present 該用戶不存在則生成;absent 該用戶存在則刪除

    uid => "值",    

    gid => "值",

    groups => "組名",       #groups該用戶所屬用戶組名可爲列表

    home => "絕對路徑",     #該用戶分配的家目錄,如沒有managehome => true, 則不會創建家目錄

    managehome => {true|false},  #是否自動生成家目錄默認爲false表示即使指定home也不會創建true表示創建home的目錄

    shell => "shell的絕對路徑",     #用戶登錄後默認分配的shell

    password => '密鑰',  #指定用戶的密碼而密碼只能使用密文加密方式在用戶定義中密碼可以通過任意的加密方式獲取加密串字符串要用引號引起否則會將其中的$當做變量名引用的。最好以‘’引起來記着用單引號。

}

實例演示

###########獲取密碼串#############

[root@node puppets]# openssl passwd -1 -salt `openssl rand -hex 4`

Password:redhat(#默認不顯示)

$1$c7d1a18e$Z2NuEe3zPmaJ9iVJt3xOg.        #隨機生成的密碼串

[root@node puppets]# vim group.pp

group {'zhao':

    ensure => 'present',

    gid => 1001,

}

user {'zhao':

   gid => 1001,     #指明基本組

   uid => 1001,     #指定用戶ID號

   groups => ['zhang','li'],  #定義附加組使用數組格式

   home => '/home/zhao',      #定義家目錄位置不指定不會自動創建

   managehome => true,        #創建家目錄默認爲false

   ensure => present,

   require => Group['zhao']

   password => '$1$c7d1a18e$Z2NuEe3zPmaJ9iVJt3xOg.',    #添加密碼串

}

##############本地執行#############

[root@node puppets]# puppet apply  group.pp

notice: /Stage[main]//User[zhao]/ensure: created     #用戶創建成功

notice: /Stage[main]//User[zhao]/password: changed password   #密碼添加成功

notice: Finished catalog run in 0.58 seconds

##########查看定義的用戶信息#######

[root@node puppets]# id zhao

uid=1001(zhao) gid=1001(zhao) groups=1001(zhao),500(li),501(zhang)

[root@node puppets]# ls /home     #查看home文件

zhao

[zhao@node ~]$ ls                 #另起終端登錄測試

7、使用puppet定義file資源

file的常用定義方式和參數解析

################定義方式############

file {"title":                      #一般是文件名

    ensure => present|absent|file|directory|link

    content => "content",   #文件內容必須有否則文件的內容爲空

    user => "username",

    group => "groupname",

    mode => 權限,       #四位八進制數

    path => "title",

    source => "puppet:///URL",  #指定到master上文件的絕對路徑或agent上本地文件絕對路徑

    target =>                   #指定目標文件用於ln -s $target $title

    recurse => true,            #遞歸

    purge => true,              #將不再資源中管理的其他數據刪除

    force => true,              #如不加則不會刪除

}

################詳細參數解析###########

content     content => "字符串",    #把文件的內容(title或name)設置爲content 參數後面的字符串, 新行,tab,空格可用 escaped syntax 表示必須有否則文件的內容爲空除非你不需要

ensure      ensure => {absent|present|file|directory|link},   #如果文件本來不存在是否要新建title名的文件其中present 檢查該文件是否存在,如果不存在就新建title名的文件absent 文件存在,就會刪除title名的文件(如果recurse => true ,就會刪除目錄)file 不存在就新建title名的文件directory 不存在就新建title名的目錄

group       group => {gid|組名},    #指定那個該文件的用戶組,值可以是gid或者組名

mode        mode => {rwx},         # mode用於設置文件的權限

owner       owner => {用戶名},     #設置文件的屬主

path        path => "文件的路徑",  #指定要管理文件的路徑,必須用引號引起來, 這也是一個資源的 namevar ,通常path 等於資源的title

source      source => {"puppet:///URL"|"完整的文件路徑"},  #拷貝一個文件覆蓋當前文件,用checksum來判斷是否有必要進行復制,可以設置的值是一個引用master或agent的完整的文件路徑,或者是URI,當前支持的URI只有puppet和file ; 這是一個對文件常用的操作,可以讓puppet修改系統的配置文件.

recurse     recurse => { true|false|inf|remote}, #設置是否以及如何進行遞歸操作即可以管理子目錄recurse,purge和force連用用於刪除子目錄中不在資源控制的文件或目錄

purge       purge => {true|false},        #將不再資源中管理的其他數據則刪除

force       force => {ture|false},        # force是否可以刪除文件或目錄與ignore相反

ignore      ignore => {文件名|正則表達式},   #當用recursion 方法複製一個目錄的時候,可以用ignore來設定過濾條件,符合過濾條件的文件不被複制或刪除. 使用ruby自帶的匹配法則.因此shell級別的過濾表達式完全支持,例如[a-g]* 與force相反

target      target => {"源文件或目錄"}      #是爲創建鏈接文件的即將target的值作爲源文件title的值作爲目標文件。如lin -s $target $title。當且僅當ensure => link, 纔可以使用

更多參數解析請參考[root@node puppets]# puppet describe file | less

實例演示

[root@node puppets]# vim file.pp

file {'/etc/nginx/nginx.conf':       #在這裏title定義全路徑完全可以替代path

      ensure => file,

      source => '/etc/backup/nginx/nginx.conf',    #指定文件來源位置

      mode => '0640',       #設置權限模型

      owner => 'root',      #屬主

      group => 'root',      #屬組

[root@node puppets]# puppet apply file.pp

notice: /Stage[main]//File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}b1de7b8f5f09371a466aa56a3e41abe7'          #內容改變

notice: /Stage[main]//File[/etc/nginx/nginx.conf]/mode: mode changed '0644' to '0640'         #權限改變

notice: Finished catalog run in 0.18 seconds

8、用puppet定義執行命令

exec常用的定義方式

exec {"title":               #一般寫上要執行的命令如不寫則需要指定command

cwd => "目錄的絕對路徑",     #在那個目錄下執行也可不要

path => "/bin:/sbin:...",    #命令執行的搜索路徑,如不要則需指定命令的絕對路徑

command => "執行的命令",     #寫上要執行的命令一般不需要可在title中寫上

creates => "文件名絕對路徑",      #當且僅當該文件名不存在命令才被執行可不要

user => "用戶名",            #定義運行命令的用戶。 注意如果你使用了這個參數那麼任何的錯誤輸出不會在當下被捕捉這是Ruby的一個bug

group => "組名"              #定義運行命令的用戶組。在不同的平臺下的運行的結果無法確定由於不同用戶運行命令的時候變量是不變的所以這是平臺的問題而不是Ruby或Puppet的問題。

logoutput => "true|false",   #是否記錄輸出,可取的值爲truefalse和其他合法的日誌等級。

onlyif => "命令",            #如onlyif中命令的執行結果爲0執行正確才執行title或command中的目錄,與unless相反

unless => "命令",            #如unless中命令的執行結果不爲0執行出錯才執行title或command中的目錄,與onlyif相反

}

注意exec資源是不太好掌控的資源如果能用腳本實現儘量寫成腳本通過file資源分發到服務器上面。然後用其他的方式來調用腳本。例如crontab。說來crontab資源囉嗦一句雖然puppet提供了crontab資源但是你完全可以用file資源來把crontab任務放到/etc/cron.d目錄下來實現crontab資源的管理。使用puppet的時候儘量用最簡單的語法越是花哨的語法也越容易出錯。

實例演示

[root@node puppets]# vim exec.pp

exec {'test':

     path => '/bin:/sbin:/usr/bin:/usr/sbin',    #定義環境變量

     command => 'mktemp /tmp/abc.XXXX',          #生成一個臨時目錄

 command => 'chkconfig -add haproxy; chkconfig haproxy on',    #添加服務啓動服務中間使用分號隔開

     user => 'root',

     group => 'root',

}

執行

[root@node puppets]# puppet apply exec.pp

notice: /Stage[main]//Exec[test]/returns: executed successfully

notice: Finished catalog run in 8.19 seconds

查看

[root@node puppets]# ls /tmp/

abc.hzdn

9、定義通知資源更改機制

當我們更改過配置文件後有些不可能立即生效需要通知重新載入

#############兩個關鍵字##############

notify定義在前資源中通知資源已更改請重載

subscribe定義在後資源中訂閱通知若有改動立即通知我

#############定義資源################

[root@node puppets]# vim file.pp

file {'/etc/nginx/nginx.conf':

      ensure => file,

      source => '/etc/backup/nginx/nginx.conf',

      mode => '0644',

      owner => 'root',

      group => 'root',

      notify => Service ['nginx'],

}

service {'nginx':

     ensure => running,

#    subscribe => File ['/etc/nginx/nginx.conf'],

}

#############本地執行###############

[root@node puppets]# puppet apply file.pp

notice: /Stage[main]//File[/etc/nginx/nginx.conf]/content: content changed '{md5}b1de7b8f5f09371a466aa56a3e41abe7' to '{md5}95f45f10386878664af2b7ccd1536ea4'

notice: /Stage[main]//File[/etc/nginx/nginx.conf]/mode: mode changed '0640' to '0644'

notice: /Stage[main]//Service[nginx]: Triggered 'refresh' from 2 events          #重新刷新服務器兩次

notice: Finished catalog run in 2.94 seconds

#############查看進程數############

[root@node puppets]# ps aux | grep nginx

root     24696  0.0  0.3  96068  1908 ?        Ss   22:46   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

nginx    24697  0.0  0.5  96420  2676 ?        S    22:46   0:00 nginx: worker process        

nginx    24698  0.0  0.5  96420  2676 ?        S    22:46   0:00 nginx: worker process    #兩個說明依然重新啓動

資源間應用次序通知鏈的定義"->"用於定義次序鏈而"~>"用於定義通知鏈的他們既可以用於資源引用間也可應用於資源申報之間。

四、puppet中語法闡述

<一>puppet的變量

無論是定義還是引用puppet的變量名稱必須以“$”開頭

變量有其作用域因爲puppet有其類、模塊所以使得其作用域受限於其類、模塊中而且puppet的每個變量都有兩個名字簡短名稱和長格式完全限定名稱(FQN)完全限定名稱的格式爲"$scope::variable"

什麼是scope其作用方式是什麼

(1)Scope是一個特定的代碼區域用於同程序中的其它代碼隔離開來。

(2)在puppet中scope可用於限定變量及資源默認屬性的作用範圍但不能用於限定資源名稱及資源引用的生效範圍。

(3)任何給定的scope都可以訪問自己的內容以及接受來自其父scope、節點scope及top scope的內容。

如圖所示




wKioL1O2XpShyxvrAAEKJnne18I473.jpg








解析top scope僅能訪問自己變量和屬性默認值

節點scope能訪問自己的及top scope的變量和屬性默認值

example::parent,example::other和example::four能訪問自己的以及節點scope和top scope的變量和默認值。

(4)如果要訪問非當前scope中的變量則需要通過完全限制名稱進行如$vhostdir = $apache::params::vhostdir

需要注意的是top 5、scope的名稱爲空因此若要引用其變量則需要使用類似“$::osfamily” 的方式進行。

<二>變量的數據類型

puppet語言支持多種數據類型以用於變量和屬性的值以及函數的參數

字符型

非結構化的文本字符串可以使用引號也可以不使用

單引號中的變量不會替換而雙引號中的能夠進行變量替換

字符型值也支持使用轉義符

數值型

可爲整數和浮點數不過puppet只有在數值上下文才把數值當數值型對待其它情況下一律以字符型處理

數組

數組值爲中括號中的以逗號分隔的項目列表最後一個項目後面可以有逗號

數組中的元素可以爲任意可用數據類型包括hash或其它數組

數組索引爲從0開始的整數也可以使用負數索引

布爾型

true和false不能加引號

if語句的測試條件和比較表達式都會返回布爾型值

另外其它數據類型也可以自動轉換爲布爾型如空字符串爲false等

undef

從未被聲明的變量的值類型即爲undef

也可手動爲某變量賦予undef值即直接使用不加引號的undef字符串

hash

即爲外鍵值數據類型鍵和值之間使用"=>"分隔鍵值對兒定義在"{}"中彼此間以逗號分隔

其鍵爲字符型數據而值可以爲puppet支持的任意數據類型

訪問hash類型的數據元素要使用“鍵”當做索引進行

正則表達式

屬於puppet的非標準數據類型不能賦值給變量僅能用於有限的幾個接受正則表達式的地方即接受使用"=~"及"!~"匹配操作符的位置通常包含case語句中的selector以及節點名稱匹配的位置

他們不能傳遞給函數或用於資源屬性的定義

puppet中的正則表達式支持使用(?<ENABLED OPTION>:<SUBP ATTERN>)和(?-<DISABLED OPTION>:<SUBP ATTERN>)兩個特殊的符號。

例如下面的示例表示做正則表達式匹配時啓用選項"i"(忽略字符大小寫)但不支持使用“m”(把.當做換行符)和"x"(忽略模式中的空白字符和註釋),這裏定義-mx就是表示不使用m和x。

  $packages = $operatingsystem ? {

        /(?i-mx:ubuntu|debian)/     => 'apache2',

/(?i-mx: centos|redhat)/    => 'httpd',

}

<三>條件判斷語句

在puppet2.7的版本中有三種條件判斷語句if,case,selector而在puppet3.0版本中支持四種條件判斷if,case,selector,unless(不常用)。

if

基本語法結構

if CONDITION {

        statement

......

}

elsif CONDITION {

        statement

......

}

else CONDITION {

        statement

......

}

條件語句的書寫方式這裏的條件通常是條件表達式而這裏語句可以使用的條件有變量、表達式、有返回值的函數。

例如

[root@node puppets]# vim if.pp

if $operatingsystem =~ /(?!-mx:^(centos|fedora|redhat))/{     #定義模式匹配

     $webserver = 'httpd'

} elsif $operationsystem =~ /(?!-mx:^(debian|ubuntu))/{

     $webserver = 'apache2'

} else {

     $webserver = undef

    notice ('Unkown OS')

}

package { "$webserver":

    ensure => installed,

}

#############查看執行結果############

[root@node puppets]# puppet apply -d -v if.pp    #詳細顯示執行過程

debug: Package[httpd](provider=yum): Ensuring => present

debug: Puppet::Type::Package::ProviderYum: Executing '/usr/bin/yum -d 0 -e 0 -y install httpd'

debug: Puppet::Type::Package::ProviderYum: Executing '/bin/rpm -q httpd --nosignature --nodigest --qf %{NAME} %|EPOCH?{%{EPOCH}}:{0}| %{VERSION} %{RELEASE} %{ARCH}

'

notice: /Stage[main]//Package[httpd]/ensure: created

debug: /Stage[main]//Package[httpd]: The container Class[Main] will propagate my refresh event

debug: /Schedule[weekly]: Skipping device resources because running on a host

debug: /Schedule[puppet]: Skipping device resources because running on a host

debug: Class[Main]: The container Stage[main] will propagate my refresh event

debug: Finishing transaction 69898370485060

debug: Storing state

debug: Stored state in 0.02 seconds

notice: Finished catalog run in 71.04 seconds

debug: Finishing transaction 69898371162860

debug: Received report to process from node.magedu.com

debug: Processing report from node.magedu.com with processor Puppet::Reports::Store

case

類似於if語句case語句會從多個代碼塊中選擇一個分支執行這跟其他編程語言中的case語句功能一致。

case語句會接受一個控制表達式和一組case代碼塊並執行第一匹配到控制表達式的塊。

基本語法結構

case CONTROL_EXPRESS {

      case1,...: {statement...}

  case2,...: {statement...}

  ... ...

  default: {statement...}

 }

CONTROL_EXPRESS可以使用的條件爲變量或表達式以及有返回值的函數。

case的指定方法可以爲一個字符串、符合條件的值、具有某個返回值的函數調用、正則表達式。

statement可以爲通知信息也可以爲變量表達式。

示例演示

[root@node puppets]# vim case.pp

case $operatingsystem {

'centos','fedora','redhat': {notice("Welcome to RedHat OSFamily")}

/^(debian|ubuntu)$/: {notice("Welcome to $1 linux")}

default: {notice("Welcome, alien *_*")}

}

##############執行結果############

[root@node puppets]# puppet apply case.pp

notice: Scope(Class[main]): Welcome to RedHat OSFamily

notice: Finished catalog run in 0.03 seconds

selector

selector和case語句很相近但是和case語句不同的是selector返回的是值而不是表達式因此selector只能用於期望出現直接值plain value的地方這包含變量賦值、資源屬性定義、函數參數傳遞、資源標題和其他selector的值和表達式中。但是selector不能用於一個已經嵌套於selector的case語句中也不能用於一個嵌套於case的case語句中。

基本語法結構

CONTROL_VARIABLE ? {

       case1 => value1

   case2 => value2

   ... ...

   default => valueN

  }

selectors的使用要點

1整個selector語句會被當作一個單獨的值puppet會將控制變量按列出的次序與每個case這裏的case不是case語句而是某種情況進行比較並在遇到一個匹配的case後將其值作爲整個語句的值進行返回並忽略後面的其它case。

2控制變量與各case比較的方式與case語句相同但如果沒有任何一個case與控制變量匹配時puppet在編譯時將會返回一個錯誤因此實踐中其必須提供default case。

3selector的控制變量只能是變量或有返回值的函數切記不能使用表達式。

4其各case可以是直接值(需要加引號)、變量、能調用返回值的函數、正則表達式模式或default。

5但與case語句所不同的是selector的各case不能使用列表。

6selector的各case的值可以是一個除了hash以外的直接值、變量、能調用返回值的函數或其它的selector。

<四>類的使用(classes):

classes是一堆命名的代碼塊裏面聲明瞭一些資源然後使用名字調用這個代碼塊類似以bash中的函數。

class是用於通用目標的一組資源可以使用多次在不同的節點上可能都會用到的因此它是命名的代碼塊在某位置創建之後可在puppet全局使用若不調用則不是使用但是注意有時候可能要指定其作用域的。這裏的類類似於其他編程語言中的類的功能puppet的class可以被繼承也可以包含子類。定義類的語法很簡單如下

class my_class {

        ... puppet code ...

}

定義類:

(1)類的名稱只能以小寫字母開頭可以包含小寫字母、數字和下劃線

(2)每個類都會引入一個新的變量scope這意味着在任何時候訪問類中的變量時都會使用其完全限定名稱不過在本地scope中的變量賦予一個新值。

(3)定義完類之後要聲明要調用聲明類的方法常用的有三種直接使用include函數聲明類使用require來聲明類類似Resource的聲明方式一樣定義類。

例如安裝nginx服務器

#############關鍵字#############

聲明類includerequireResource

#############定義###############

[root@node puppets]# vim class.pp

class nginx {

      package {'nginx':

      ensure => installed,

      before => File['/etc/nginx/nginx.conf'],

     }

      file {'/etc/nginx/nginx.conf':

      ensure => file,

      source => '/etc/backup/nginx/nginx.conf',

      mode => '0644',

      owner => 'root',

      group => 'root',

     }

     service {'nginx':

     ensure => running,

     subscribe => File ['/etc/nginx/nginx.conf'],

     }

}

include nginx

###############執行#################

[root@node puppets]# puppet apply class.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 124.15 seconds

聲明帶參數的類

常用結構

class my_class (para1='val1',para2='val2'){

 ... puppet code ...

 }

(1)同一個類在不同的OS上可能會略有不同因此需要通過獲取相應系統的fact來實現有區別對待。然而萬一相應的OS沒有輸出類所期望的fact或者是類依賴於非fact因素時此機制將無法滿足需求。此時就需要使用帶參數的類來完成此類功能同時在聲明類時爲其參數傳遞相應的值即可完成傳參功能。

(2)在定義在帶參數的類時需要將參數聲明在類名後的小括號“()”參數可以有默認值如果使用多個參數彼此間要使用逗號分隔。

在類的內部使用參數的方式同使用本地變量在聲明類時可向其傳遞參數其方式如同定義資源的屬性因此其也稱爲“資源屬性風格的類聲明”。

(3)不能在使用include聲明類時向其傳遞參數。對於帶參數的類來說如果使用其參數的默認值仍然可以使用include聲明類否則就必須使用“資源屬性風格的類聲明”。另外如果在使用不同的參數值將某個類聲明瞭多次最後生效的聲明將很難判定。

例如在有些情況下我們安裝的是nginx在有些場景下我們安裝的是tengine

##############關鍵字###########

class這裏有聲明的意思

##############定義#############

[root@node puppets]# vim class2.pp

class nginxserver ($ngserver='nginx'){

      package {"$ngserver":

      ensure => installed,

      before => File['/etc/nginx/nginx.conf'],

     }

      file {'/etc/nginx/nginx.conf':

      ensure => file,

      source => '/etc/backup/nginx/nginx.conf',

      mode => '0644',

      owner => 'root',

      group => 'root',

     }

     service {'nginx':

     ensure => running,

     subscribe => File ['/etc/nginx/nginx.conf'],

     }

}

class {'nginxserver':

      ngserver => 'tengine',

}

#############執行測試#############

[root@node puppets]# puppet apply class2.pp

err: /Stage[main]/Nginxserver/Package[tengine]/ensure: change from absent to present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y install tengine' returned 1: Error: Nothing to do        #出現此錯誤原因是我的本地yum源中沒有tengine但是說明了這種方法可以使用安裝。

notice: /Stage[main]/Nginxserver/File[/etc/nginx/nginx.conf]: Dependency Package[tengine] has failures: true

warning: /Stage[main]/Nginxserver/File[/etc/nginx/nginx.conf]: Skipping because of failed dependencies

notice: /Stage[main]/Nginxserver/Service[nginx]: Dependency Package[tengine] has failures: true

warning: /Stage[main]/Nginxserver/Service[nginx]: Skipping because of failed dependencies

notice: Finished catalog run in 21.08 seconds

類的繼承

在原有類的基礎上再添加一些屬性參數代碼使其更加具體化這就叫做類的繼承(使用inherits關鍵字)。並且一個類還可以包含多個子類。

#############關鍵字############

inherits繼承

############定義###############

[root@node puppets]# vim class3.pp

class nginx {

     package {'nginx':

          ensure => installed,

     }

}

class nginx::websrv inherits nginx {         #繼承(inherits)

    file {'/etc/nginx/nginx.conf':

     ensure => file,

     mode => '0644',

     owner => 'root',

     group => 'root',

     source => '/etc/backup/nginx/nginx.conf',

     require => Package['nginx'],

     }

    service {'nginx':

     ensure => running,

     subscribe => File['/etc/nginx/nginx.conf']

     }

}

class nginx::rproxy inherits nginx {

    file {'/etc/nginx/nginx.conf':

     ensure => file,

     mode => '0644',

     owner => 'root',

     group => 'root',

     source => '/etc/backup/nginx/nginx.conf',

     require => Package['nginx'],

     }

    service {'nginx':

     ensure => running,

     subscribe => File['/etc/nginx/nginx.conf']

     }

}

include nginx::rproxy         #調用

##############執行################

[root@node puppets]# puppet apply class3.pp

notice: /Stage[main]/Nginx::Rproxy/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'

notice: /Stage[main]/Nginx::Rproxy/Service[nginx]/ensure: ensure changed 'stopped' to 'running'

notice: /Stage[main]/Nginx::Rproxy/Service[nginx]: Triggered 'refresh' from 1 events

notice: Finished catalog run in 1.92 seconds

小擴展

在類中我們使用=>來引用資源也可使用+>在原有依賴資源的基礎上多引用幾個其他資源。

例如

class apache {

    service { 'apache':

    require => package['httpd'],

}

 }

class apache::ssl inherits apache {

  Service['apache'] {

     require +> [File['httpd.pem']],File['httpd.conf']]

 }

 }

<五>Modules概述

到目前當前筆者的概述爲止資源申報、定義類、聲明類等所有功能都只能在一個manifest清單文件中實現但是這卻非最有效的基於puppet管理IT基礎架構的方式

實踐中一般需要把manifest文件分解成易於理解的結構例如將類文件、配置文件甚至包括後面將提到的模塊文件等分類存放並且通過某種機制在必要時將他們整合起來

這種機制即“模塊”它有助於以結構化、層次化的方式使用puppet而puppet則基於“模塊自動裝載器”完成模塊裝載。

從另一個角度來說模塊實際上就是一個按約定的、預定義的結構存放了多個文件或子目錄的目錄目錄裏的這些文件或子目錄必須遵循其命名規範。

puppet會按此種規範在特定的位置查找所需的模塊文件不過這些特定的目錄也可以通過puppet的配置參數modulepath定義。

查詢模塊裝載路徑

[root@node ~]# puppet master --genconfig | grep modulepath

 modulepath = /etc/puppet/modules:/usr/share/puppet/modules #是由冒號隔開的兩個目錄

查看已安裝模塊

[root@node ~]# puppet module list

/etc/puppet/modules (no modules installed)      #顯示模塊搜索路徑

/usr/share/puppet/modules (no modules installed)

官方給出的模塊目錄結構

首先模塊名稱MODULE NAME也就是目錄名稱模塊名稱只能以小寫字母開頭可以包含小寫字母、數字和下劃線但是不能使用“main”或“settings”作爲模塊名在這個目錄下存在有manifests、files、templates、lib、tests、spec這些子目錄

manifests是清單目錄包含當前模塊的所有manifest文件每一個manifest文件必須包含一個類或一個定義的類也就是說裏面存放的是以.pp結尾處的文件其中必須有一個init.pp的文件這個文件只能包含一個單獨的類定義且類的名稱必須與模塊名稱相同

files目錄包含一組的靜態文件這些文件可被節點下載使用每個文件的訪問路徑遵循puppet:///modules/MODULE_NAME/filename路徑格式

lib目錄插件目錄常用於自定義fact及自定義資源類型等

templates目錄存儲了manifest用到的模塊文件其訪問路徑遵循template'ModuleName/TemplateName'格式

tests目錄當前模塊的使用幫助或使用範例文件類似如何聲明當前模塊中的類及定義的類型等

spec目錄類似於tests目錄的功能只不過其是爲lib目錄中定義的各插件提供使用範例的

查詢網上提供的第三方模塊前提是能夠訪問互聯網

1

2

[root@node ~]# puppet module search apache #關於apache的模塊

[root@node ~]# puppet module install MODULE_NAME #指定模塊名稱

使用模塊的步驟

1.創建模塊目錄和文件的方式

1

2

mkdir -pv /etc/puppet/modules/模塊名/{manifests,files,lib,templates,spac,test}

touch /etc/puppet/modules/模塊名/manifests/init.pp

2.在init.pp中導入別的pp文件(也可直接在init.pp中寫上puppet的語法)

1

2

vim /etc/puppet/modules/模塊名/manifests/init.pp

    import "*"          #相當於node … { import模塊名}

3.寫上puppet的語法,完成某些功能最好是單獨寫利於管理

1

vim 模塊名.pp

4.根據“模塊名.pp或init.pp”的內容考慮是否需要在files目錄下創建所需的文件。


實例演示

模塊清單手動創建一個模塊使其能夠運行起來

[root@node ~]# cd /etc/puppet/modules/             #進入模塊清單目錄下

[root@node modules]# mkdir -pv nginx/{manifests,files,lib,templates,spac,test}     #創建一個Nginx模塊目錄

mkdir: created directory `nginx'

mkdir: created directory `nginx/manifests'

mkdir: created directory `nginx/files'

mkdir: created directory `nginx/lib'

mkdir: created directory `nginx/templates'

mkdir: created directory `nginx/spac'

mkdir: created directory `nginx/test'

[root@node modules]# puppet module list             #查看已安裝的模塊

/etc/puppet/modules

?..? nginx (???)              #有亂碼顯示博主沒有消除掉若看客們有辦法請留言告知謝謝。

/usr/share/puppet/modules (no modules installed)

[root@node modules]# cd nginx/manifests/

[root@node manifests]# vim init.pp          #編輯init.pp

class nginx {

      package {'nginx':

      ensure => installed,

      before => File['/etc/nginx/nginx.conf'],

     }

      file {'/etc/nginx/nginx.conf':

      ensure => file,

      source => 'puppet:///modules/nginx/nginx.conf',            #靜態文件目錄

      mode => '0644',

      owner => 'root',

      group => 'root',

     }

     service {'nginx':

     ensure => running,

     subscribe => File ['/etc/nginx/nginx.conf'],

     }

}

include nginx

[root@node manifests]# cp /etc/backup/nginx/nginx.conf /etc/puppet/modules/nginx/files/       #靜態文件

[root@node manifests]# service nginx stop    

Stopping nginx:                                            [  OK  ]

[root@node manifests]# rpm -e nginx

warning: /etc/nginx/nginx.conf saved as /etc/nginx/nginx.conf.rpmsave

[root@node manifests]# rm -rf /etc/nginx/

[root@node manifests]# pwd          #查詢當前文件

/etc/puppet/modules/nginx/manifests

[root@node manifests]# puppet apply init.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 64.02 seconds

站點清單

首先安裝puppet-server使其生成manifests目錄(/etc/puppet/manifests),這個目錄就是站點清單也就是定義各節點的清單。

node(節點)的作用區分不同的客戶端並且給不同的服務端分配manifest。

一般在/etc/puppet/manifests創建一個名叫nodes.pp專門用來存放node信息的

vim /etc/puppet/manifests/nodes.pp      #建立節點文件

node 'FQDN' {               #FQDN 完全合格域名

    變量的聲明              #聲明格式$變量名="值" 引用格式 ${變量名}

    include class           #已定義好的類class

}

實例演示

###############安裝puppet-server#############

[root@node ~]# rpm -ivh puppet-server-2.7.23-1.el6.noarch.rpm

[root@node ~]# hostname            #當前主機名

node.magedu.com

[root@node ~]# ping node.magedu.com     #測試通信

PING node.magedu.com (172.16.18.6) 56(84) bytes of data.

64 bytes from node.magedu.com (172.16.18.6): icmp_seq=1 ttl=64 time=0.300 ms

64 bytes from node.magedu.com (172.16.18.6): icmp_seq=2 ttl=64 time=0.035 ms

##############定義類###############

[root@node manifests]# pwd

/etc/puppet/modules/nginx/manifests

[root@node manifests]# vim init.pp

class nginx {

}

[root@node manifests]# vim nginxserver.pp

class nginx::nginxserver inherits nginx {

      package {'nginx':

      ensure => installed,

      before => File['/etc/nginx/nginx.conf'],

     }

      file {'/etc/nginx/nginx.conf':

      ensure => file,

      source => 'puppet:///modules/nginx/nginx.conf',

      mode => '0644',

      owner => 'root',

      group => 'root',

     }

     service {'nginx':

     ensure => running,

     subscribe => File ['/etc/nginx/nginx.conf'],

     }

}

[root@node manifests]# cp nginxserver.pp tengine.pp

[root@node manifests]# vim tengine.pp

class nginx::tengine inherits nginx {

      package {'tengine':

      ensure => installed,

      before => File['/etc/nginx/nginx.conf'],

     }

      file {'/etc/nginx/nginx.conf':

      ensure => file,

      source => 'puppet:///modules/nginx/nginx.conf',

      mode => '0644',

      owner => 'root',

      group => 'root',

     }

     service {'nginx':

     ensure => running,

     subscribe => File ['/etc/nginx/nginx.conf'],

     }

}

###############編輯節點文件#############

[root@node manifests]# cd /etc/puppet/manifests/

[root@node manifests]# vim nodes.pp 

node 'node.magedu.com'{            #編輯節點信息由於當前沒有使用節點使用本機。

   include nginx::nginxserver         #加載類

}

###############nginx服務################

[root@node manifests]# service nginx stop   

Stopping nginx:                                            [  OK  ]

[root@node manifests]# rpm -e nginx

warning: /etc/nginx/nginx.conf saved as /etc/nginx/nginx.conf.rpmsave

[root@node manifests]# rm -rf /etc/nginx/

#############測試執行一###########

[root@node manifests]# puppet apply nodes.pp

notice: /Stage[main]/Nginx::Nginxserver/Package[nginx]/ensure: created

notice: /Stage[main]/Nginx::Nginxserver/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'

notice: /Stage[main]/Nginx::Nginxserver/Service[nginx]/ensure: ensure changed 'stopped' to 'running'

notice: /Stage[main]/Nginx::Nginxserver/Service[nginx]: Triggered 'refresh' from 1 events

notice: Finished catalog run in 13.79 seconds

##########測試執行二#############

         ###服務###

[root@node manifests]# service nginx stop

Stopping nginx:                                            [  OK  ]

[root@node manifests]# rpm -e nginx

warning: /etc/nginx/nginx.conf saved as /etc/nginx/nginx.conf.rpmsave

[root@node manifests]# rm -rf /etc/nginx/

         ###更改###

[root@node manifests]# vim nodes.pp

node 'node.magedu.com'{

   include nginx::tengine        #調用teengine類

}

         ###執行###

[root@node manifests]# puppet apply nodes.pp

err: /Stage[main]/Nginx::Tengine/Package[tengine]/ensure: change from absent to present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y install tengine' returned 1: Error: Nothing to do

notice: /Stage[main]/Nginx::Tengine/File[/etc/nginx/nginx.conf]: Dependency Package[tengine] has failures: true

warning: /Stage[main]/Nginx::Tengine/File[/etc/nginx/nginx.conf]: Skipping because 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 failed dependencies

notice: Finished catalog run in 2.28 seconds

############測試執行三############

[root@node manifests]# vim nodes.pp

node 'node1.magedu.com'{                #更改爲一個無法獲取信息的地址

     include nginx::nginxserver

}

[root@node manifests]# puppet apply nodes.pp

Could not find default node or by name with 'node.magedu.com, node.magedu, node' on node node.magedu.com    #找不到

####因此表明我們可以通過域名節點實現類的調用。

總結

puppet類不支持多重繼承因此不能多次繼承也不能繼承自多個類。

五、Master/Agent安裝:

<一>節點信息:

###########master端:############

node:172.16.18.6

[root@node ~]# hostname

node.magedu.com

###########agent端一:##########

node1:172.16.18.7

[root@node1 ~]# hostname

node1.magedu.com

<二>更改master端配置文件

[root@node ~]# cd /etc/puppet/

[root@node puppet]# ls

auth.conf  fileserver.conf  manifests  modules  puppet.conf

[root@node puppet]# puppet master --genconfig > puppet.conf           #重新生成配置文件

[root@node puppet]# vim puppet.conf        #修改配置文件

    rundir = /var/run/puppet           #645行

    pidfile = $rundir/master.pid       #364行

<三>初始化啓動服務器端

[root@node puppet]# puppet master --no-daemonize --verbose --debug    #首次啓動,輸出詳細信息:創建CA,簽發證書等;其中--no-daemonize表示工作在前端 --verbose表示顯示詳細信息 --debug表示調試

debug: Finishing transaction 70216505862080

info: Creating a new SSL key for ca      

info: Creating a new SSL certificate request for ca

info: Certificate Request fingerprint (md5): 01:47:FD:C5:2F:8F:4F:10:EE:4B:11:94:95:1B:47:44

notice: Signed certificate request for ca

notice: Rebuilding inventory file

debug: Using cached certificate for ca

info: Creating a new certificate revocation list

info: Creating a new SSL key for node.magedu.com

debug: Using cached certificate for ca

info: Creating a new SSL certificate request for node.magedu.com

info: Certificate Request fingerprint (md5): CD:23:B4:59:94:17:F1:4A:37:46:94:59:BF:6E:8C:A0

notice: node.magedu.com has a waiting certificate request

debug: Using cached certificate for ca

debug: Using cached certificate_request for node.magedu.com

notice: Signed certificate request for node.magedu.com

notice: Removing file Puppet::SSL::CertificateRequest node.magedu.com at '/etc/puppet/ssl/ca/requests/node.magedu.com.pem'

notice: Removing file Puppet::SSL::CertificateRequest node.magedu.com at '/etc/puppet/ssl/certificate_requests/node.magedu.com.pem'

notice: Starting Puppet master version 2.7.23

debug: Finishing transaction 70216504311360

然後使用Ctrl+c取消,使用service進行啓動

[root@node puppet]# service puppetmaster start

Starting puppetmaster:                                     [  OK  ]

[root@node puppet]# ss -tanl

State      Recv-Q Send-Q      Local Address:Port        Peer Address:Port

LISTEN     0      5                       *:8140                   *:*        #端口已啓動

<四>安裝agent端

[root@node1 ~]# yum -y localinstall facter-1.7.3-1.el6.x86_64.rpm puppet-2.7.23-1.el6.noarch.rpm

<五>修改配置文件

########agent端一########

[root@node1 ~]vim /etc/puppet/puppet.conf

[main]

server = node.magedu.com     #指定puppet服務器主機名

<六>初始化agent端(需要連接服務器)

########agent端一##########

[root@node1 puppet]# puppet agent --server=node.magedu.com --no-daemonize --verbose --debug

debug: Finishing transaction 69956847467440

info: Creating a new SSL key for node1.magedu.com

info: Caching certificate for ca

info: Creating a new SSL certificate request for node1.magedu.com

info: Certificate Request fingerprint (md5): 4C:7F:E3:B8:1A:24:23:4E:4E:CB:B9:ED:FF:12:D2:49

出現此命令顯示說明初始化成功等待服務器端認證通過

########master端認證後,正在簽署(需要一段時間)####

info: Caching certificate for node1.magedu.com

notice: Starting Puppet client version 2.7.23          #啓動客戶端

debug: Finishing transaction 69956847116860

debug: catalog supports formats: b64_zlib_yaml dot pson raw yaml; using pson

debug: Using cached certificate for ca

debug: Using cached certificate for node1.magedu.com

info: Caching certificate_revocation_list for ca

info: Caching catalog for node1.magedu.com

debug: Creating default schedules

info: Applying configuration version '1382754147'

debug: /Schedule[daily]: Skipping device resources because running on a host

debug: /Schedule[monthly]: Skipping device resources because running on a host

debug: /Schedule[hourly]: Skipping device resources because running on a host

debug: /Schedule[never]: Skipping device resources because running on a host

debug: /Schedule[weekly]: Skipping device resources because running on a host

debug: /Schedule[puppet]: Skipping device resources because running on a host

debug: Finishing transaction 69956847346700

debug: Storing state

info: Creating state file /var/lib/puppet/state/state.yaml

debug: Stored state in 0.01 seconds

notice: Finished catalog run in 0.04 seconds

注意:如果此前曾以其它主機名或各種原因啓動過puppet客戶端進程並完成過初始化(輸出信息:debug: Using cached certificate for ca等),其證書文件將無法符合本此啓動的需要;此時,需要先清空/var/lib/puppet/ssl/目錄方可完成後續的初始化操作。

[root@node1 puppet]# rm -rf /var/lib/puppet/ssl/*

<七>簽署證書

[root@node ~]# puppet cert list        #查看需簽署證書列表

  "node1.magedu.com" (4C:7F:E3:B8:1A:24:23:4E:4E:CB:B9:ED:FF:12:D2:49)

[root@node ~]# puppet cert sign node1.magedu.com    #爲agent1簽署證書

notice: Signed certificate request for node1.magedu.com

notice: Removing file Puppet::SSL::CertificateRequest node1.magedu.com at '/etc/puppet/ssl/ca/requests/node1.magedu.com.pem'

若有多個節點等待簽署證書可使用#puppet cert sign --all 進行全部簽署

<八>啓動服務

[root@node1 puppet]service puppet start

至此Master/Agent端就安裝完成了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章