puppet採用C/S星狀的結構,全部的客戶端和一個或幾個服務器交互。每一個客戶端週期的(默認半個小時)向服務器發送請求,得到其最新的配置信息,保證和該配置信息同步。每一個puppet客戶端每半小時(能夠設置)鏈接一次服務器端, 下載最新的配置文件,而且嚴格按照配置文件來配置客戶端. 配置完成之後,puppet客戶端能夠反饋給服務器端一個消息. 若是出錯,也會給服務器端反饋一個消息.html
~]# puppet descript [-s|--short] [-p|--providers] [-l|--list] [-m|--meta] [type] //使用查詢 -l:列出全部資源類型 -s:顯示指定類型的簡要幫助信息 -m:顯示指定類型的元參數 Usage: puppet [subcommand] [options] [action] [options]
package {'nginx': name => 'nginx', ensure => latest, # before => Service['nginx'] } service {'nginx': name => 'nginx', enable => true, require => Package['nginx'] } 須要事先定義package資源,並且該資源必須是服務所依賴的資源,在表示須要依賴上面的package才能運行service.
在引用時,資源的首字母必須大寫,如:java
Type['title'] 如 Service['nginx']
file {'/tmp/test': name => '/tmp/test', ensure => file, content => 'Hello World', # notify => Exec['monitor'] } exec {'monitor': name => 'monitor', command => 'echo "/tmp/test has changed" >> /tmp/change', subscribe => File['/tmp/test'], refreshonly => true, path => '/bin:/sbin:/usr/bin' } 每一次應用都會觸發Exec['monitor']此處不寫refresh,其也是能夠執行的file每被應用一次,其就會往monitor中寫入相應的文件。若加入此句refreshonly=true,其會只在file資源改變時寫入內容(觸發exec)
package {'nginx': name => 'nginx', ensure => latest, } -> file {'/etc/nginx/nginx.conf': path => '/etc/nginx/nginx.conf', source => '/root/puppet/nginx.conf', } -> service {'nginx': ensure => true, enable => true, } Package['nginx'] -> File['/etc/nginx/nginx.conf'] ~> Service['nginx'] ->:用於定義次序鏈,即nginx安裝以後纔會處理配置文件~>:用戶定義通知鏈,配置文件處理好了以後會通知服務也能夠在定義資源時直接定義
可用今生成密碼:openssl passwd -1 -salt `openssl rand -hex 4`node
group {'mysqll': ensure => present, gid => 1200, } -> user {'ming': name => "ming", ensure => present, uid => 1200, groups => 'mysqll', home => "/home/dai", password => '$1$ce1a71ce$c05P/h2f9bwImXX0WILlU0', shell => "/bin/bash", comment => "You is my creat", }
package { 'nginx': provider => yum, name => 'nginx', ensure => latest }
file {'/test': ensure => file, content => "This is test", owner => 'ming', group => 'mysqll', mode => 777, } file {'/directory': ensure => directory, } file {'/test.link': ensure => link, target => '/test' } file { "jdk-8u25-linux-x64.rpm": ensure => file, path => '/tmp/jdk-8u25-linux-x64.rpm', source => 'puppet:///modules/jdk8/jdk-8u25-linux-x64.rpm'; "java.sh": ensure => file, path => '/etc/profile.d/java.sh', source => 'puppet:///modules/jdk8/java.sh'; } 建立文件建立目錄建立連接可傳送多個文件,可分開定義,但最後一個條件得是;結尾
exec {'clean log': command => "/bin/bash cleanning.sh", cwd => "/usr/local/cripts", provider => shell, logoutput => true, onlyif => "/usr/bin/test `du /var/log/message | cut -f1 -gt 102400`" } 執行命令的內容指定命令在哪一個目錄下執行爲true時,表示無論命令執行成功與否都輸出日誌onlyif參數控制了命令執行的條件,如上只有當message文件大小超過100M時,才執行command的參數指定的cleanning.sh腳本
未添加控制參數的exec資源將變得很是危險,由於它會在客戶端每次從服務端更新資源時被執行。須要交互的命令一搬都不能執行成功mysql
cron{'timesync': command => '/usr/sbin/ntpdate 10.1.0.1 &> /dev/null', ensure => present, minute => '*/3', user => 'root', }
$variable_name=value:引用變量時能夠直接 使用或者使用雙引號linux
做用域:nginx
引用路徑:web
變量的賦值符號:正則表達式
(?i-mx:PATTERN),不能賦值給變量,僅能用在接受=~或!~操做符的位置。-mx就是表示不使用m和xsql
if CONDITION { .... } else { .... } ex: if $osfamily =~ /(?i-mx:(debian|ubuntu))/ { $webserver = 'apache2' } else { $webserver = 'httpd' } package {"$webserver": ensure => latest, } file {'httpd.conf': path => '/etc/httpd/conf/httpd.conf', source => '/root/puppet/httpd.conf', ensure => file, require => Package["$webserver"], # notify => Service['httpd'] } service {'httpd': ensure => true, enable => true, restart => 'systemctl restart httpd.service', subscribe => File['httpd.conf'] } CONDITION的給定方式:變量,比較表達式,有返回值的函數只要當配置文件發生改變時,纔會觸發重啓
case CONTROL_EXPRESSION { case1: { ... } case2: { ... } case3: { ... } default: { ... } } ex: case $osfamily { "Redhat": { $webserver='httpd' } /(?i-mx:debian)/: { $webserver='apache2' } default: { $webserver='httpd'} } CONTROL_EXPRESSION:變量,表達式,有返回值的函數各case的給定方式:直接字符串,變量,有返回值的函數,正則表達式模式,default
CONTROL_VARIABLE ? { case1 => value1, case2 => value2, ... default => valueN, } ex: $pkgname = $operatingsystem ? { /(?i-mx:(unbuntu|debian))/ => 'apache2', /(?i-mx:)(redhat|defora|centos))/ => 'httpd', defautl => 'httpd' } package{"$pkgname": ensure => installed, } CONTROL_VARIABLE的給定方法:變量,有返回值的函數各case的給定方式:直接字符串,變量,有返回值的函數,正則表達式模式,default注意:不能使用列表格式,但能夠是其它的selector
爲了實現通用目標或目的組織在一塊兒的一個或多個資源,即命名爲代碼塊shell
類的分類:
調用方式:
類的聲明方式一:
class apache2 { $webserver = $operatingsystem ? { /(?i-mx:(centos|redhat|fedora))/ => 'httpd', /(?i-mx:(ubuntu|debian))/ => 'apache2', default => 'httpd' } package {'httpd': ensure => latest, before => File['httpd.conf'] } file {'httpd.conf': ensure => file, path => '/etc/httpd/conf/httpd.conf', source => '/root/puppet/httpd.conf', notify => Service['httpd'] } service {'httpd': ensure => true, enable => true, restart => 'systemctl restart httpd.service', } } include apache2 直接調用apache2類
類的聲明方式二:
class apache2($webserver='apache2') { package {"$webserver": ensure => latest, before => File['httpd.conf'] } file {'httpd.conf': ensure => file, path => '/etc/httpd/conf/httpd.conf', source => '/root/puppet/httpd.conf', notify => Service['httpd'] } service {'httpd': ensure => true, enable => true, restart => 'systemctl restart httpd.service', } } class{'apache2': webserver => 'httpd' } 沒有傳遞參數,將會使用默認httpd參數自定義實例變量結果,而且上變量得有默認值,不然不予執行
類的繼承:
一般將公共功能定義爲基類,須要增長的功能定義爲子類,其繼承一個已有的類,並實現覆蓋資源屬性,或向資源屬性追加額外值
Type['title'] { attribute => value, ... } 引用並修改
Type['title'] { attribute +> value, ... } 添加新值
class apache2($webserver='apache2') { # $webserver = $operatingsystem ? { # /(?i-mx:(centos|redhat|fedora))/ => 'httpd', # /(?i-mx:(ubuntu|debian))/ => 'apache2', # default => 'httpd' # } package {"$webserver": ensure => latest, before => File['httpd.conf'] } service {'httpd': ensure => true, enable => true, restart => 'systemctl restart httpd.service', } } class apache2::web inherits apache2 { Service['httpd'] { subscribe => File['httpd.conf'] } file {'httpd.conf': ensure => file, path => '/etc/httpd/conf/httpd.conf', source => '/root/puppet/httpd.conf', notify => Service['httpd'] } } class {'apache2': webserver => 'httpd', } include apache2::web 子類繼承父類的方式如上:父類::子類 inherits 父類
基於ERB(擴展ruby語言)模板語言,在靜態文件中使用變量等編程元素生成適用於多中不一樣環境的文本文件(配置文件),主要用於實如今文本文件中嵌入ruby代碼,原來的文件信息不會被改變,但ruby代碼會被執行,執行結果將直接替換原來代碼
模板代碼的添加方式:
其它更多添加方式請參考官方文檔:
https://docs.puppet.com/puppet/latest/lang_template_erb.html在模板文件中可使用變量,包括puppet的任意可用變量,但變量名需以@開頭。.erb可用可不用,在配置文件後加.erb只是爲了好分辨
~]# mv nginx.conf nginx.conf.erb ~]# vim nginx.conf.erb worker_processes class nginx { package {'nginx': provider => yum, ensure => latest, } } class nginx::web inherits nginx { file {'nginx': ensure => file, content => template('/root/puppet/nginx.conf.erb'), path => '/etc/nginx/nginx.conf', require => Package['nginx'], notify => Service['nginx'] } service {'nginx': ensure => true, enable => true, restart => 'systemctl reload nginx.service', subscribe => File['httpd.conf'] } } include nginx::web <%= @processorcount %>若是定義好配置文件,使用了ERB的配置語法,文件複製不能用source,而是須要使用content參數,利用template函數生成對應的配置文件,此爲固定格式用法(將使用template函數生成文本內容,再導入至對應的配置文件)
~]# tree /nginx/ /nginx/ ├── files ├── lib ├── manifests │ └── init.pp ├── spec ├── tempaltes │ └── nginx.conf.erb └── tests
puppet URL: puppet:///modules/MODULE_NAME/FILE_NAME
模塊管理工具
注意:puppet3.8之後的版本中,資源清單文件名要與文件類名保持一致,例如某子類名爲「nginx::web」,其文件名應該爲web.pp。不在支持 import
~]# puppet apply -d -v -e 'include classes' --noop array 此處的-e後得是模塊名,也就是在/etc/puppet/modules/下的模塊名
master/agent強依賴於DNS服務(證書籤署是對FQDN作證書頒發的),由master端定義好功能模塊,再到/etc/puppet/manifests/定義site.pp文件,定義站點所須要的資源。master端經過自建CA並簽發證書給各站點,使用證書驗證客戶端的身份,當站點發出請求時Master端將查找site.pp文件中定義的資源,編譯成catalog,發送給客戶端。 agent默認每隔30分鐘向Master發送node_name和facts,並請求catalog,在本地執行catalog代碼。master(監聽8140端口)與agent(監聽在8139)兩者之間基於https協議通訊,其遠程過程調用方式爲xmlrpc機制。
Info: Creating a new SSL key for ca Info: Creating a new SSL certificate request for ca ... Notice: Signed certificate request for ca Info: Creating a new certificate revocation list Info: Creating a new SSL key for localhost Info: csr_attributes file loading from /etc/puppet/csr_attributes.yaml Info: Creating a new SSL certificate request for localhost ... Notice: localhost has a waiting certificate request Notice: Signed certificate request for localhost ...CertificateRequest localhost at '/var/lib/puppet/ssl/ca/requests/localhost.pem' ...CertificateRequest localhost at '/var/lib/puppet/ssl/certificate_requests/localhost.pem' Notice: Starting Puppet master version 3.6.2 ~]# puppet master --no-daemonize -v詳細的初始化過程生成一個私鑰生成一個證書請求自簽證書生成吊銷列表
Info: Creating a new SSL key for node6.iofunction.com Info: Caching certificate for ca Info: csr_attributes file loading from /etc/puppet/csr_attributes.yaml Info: Creating a new SSL certificate request for node6.iofunction.com ... ~]# puppet agent --server master --no-daemonize -v 引處的server用的主機名稱,主機名不會變,ip地址會變,作實驗我就直接寫在/etc/hosts文件裏的建立一個證書籤署請求發往master端,等待簽署完成的證書
一、配置master端 ~]# puppet master --no-daemonize -v #首次之前臺模式啓動,確認無誤再運行爲後端 ~]# systemctl start puppetmaster.service ~]# systemctl enable puppetserver.service ~]# ss -tnlp : 8140/tcp 二、定義站點配置文件 # cd /etc/puppet/manifests/ # vim site.pp #必須先爲站點定義好站點文件,否則agent端啓動時會報錯 node /^centos7.pc\d+/ { include nginx::webserver } 三、配置agent端(發送證書籤署請求給Master) # puppet agent --server=master_hostname --no-daemonize --noop --test -v #建議首次啓動時之前臺模式運行,確認OK後,再將運行爲後端 ~]# systemctl start puppetagent.service 四、在master端爲客戶端簽署證書 ~]# puppet cert list #首先查看未簽署的證書列表 ~]# puppet cert sign node_name 或者 ~]# puppet cert sign -all 五、以守護進程方式啓動agent ~]# systemctl start puppet ####必要是清除客戶端請求 ~]# puppet cert list -all : 查看已經簽署的客戶端證書 ~]# puppet cert clean node_name : 清除一個Node的簽署證書 rm -rf /var/lib/puppet/ssl : 移除ssl證書
~]# puppet help kick 雖然要被棄用了,但仍是有人在用 puppet kick [-a|--all] [-c|--class <class>] [--host <host>] ~]# vim /etc/puppet/puppet.conf [main] ... listen = true [agent] server = master ~]# tail -10 /etc/puppet/auth.conf path /run method save auth any allow master.iofunction.com # deny everything else; this ACL is not strictly necessary, but # illustrates the default policy. path / auth any agent端:master是誰容許主像agent推送消息
site.pp文件是puppet讀取全部模塊pp文件的開始
~]# vim /etc/puppet/manifests/site.pp node /^node\d+\.iofunction\.com/ { 主機名匹配 include chrony, jdk8 } 類名同module名相同,方可執行