天天看點

深入淺出Puppet(一)

Puppet基礎

===============================================================================

概述:

運維工具

 1.運維工具(Operations)介紹

★OS Provision:
  • bare metal(裸機):pxe, cobbler
  • virutal machine(虛拟機):p_w_picpath file template
★Configuration:
  • ansible(agentless)
  • puppet(master/agent)(ruby)
  • saltstack(python)
★Command and Control:
  • ansible(playbook)
  • fabric(fab)
  • func

       ...

附圖:

 運維工具層次圖

深入淺出Puppet(一)

puppet介紹

 1.puppet簡介

  • puppet是一個IT基礎設施自動化管理工具,它能夠幫助系統管理者管理基礎設施的整個生命周期:供應(provisioning)、配置(configuration)、關聯(orchestration)及報告(reporting)
  • 基于puppet,可實作自動化重複任務、快速部署關鍵性應用以及在本地或雲端完成主動管理變更和快速擴充架構規模等;
  • 遵循GPL協定(2.7.0-),基于ruby語言研發,2.7.0以後使用(Apache 2.0 license)
  • 對于系統管理者是抽象的,隻依賴于ruby和facter 
  • 能管理多達40多種資源,例如:file、user、group、host、package、service、cron、exec、yum repo等,适合整個軟體生命周期管理

 1.puppet工作模型和工作原理

★puppet通過聲明性、基于模型的方法進行IT自動化管理
  • 定義:使用Puppet特定的語言定義基礎配置資訊。通常我們把這些資訊寫在Modules中。
  • 模闆:在配置執行之前檢測代碼,但并不真正執行。
  • 執行:定義的配置自動部署。檢測并記錄下所發生變化的部分
  • 報告:将期待的變化、實際發生的變化及任何修改發送給報告系統。
  puppet工作資料流示意圖:
深入淺出Puppet(一)
★資料流說明:
  • 首先所有的節點(Node)Node節點将Facts和本機資訊發送給Master
  • Master告訴Node節點應該如何配置,将這些資訊寫入Catalog後傳給Node。
  • Node節點在本機進行代碼解析驗證并執行,将結果回報給Master。
  • Master通過API将資料發給分析工具。報告完全可以通過開放API或與其他系統內建。
整個資料流的走向是基于SSL安全協定的,如下圖所示:
深入淺出Puppet(一)
★模闆檔案處理過程說明如下:
  • Puppet通過編譯Manifest(清單)中的内容 (即模闆中内容),将編譯好的代碼存入Catalog。在執行前先進行代碼的驗證,再執行,完成最開始所定義好的狀态。代碼編譯過程如圖所示:
深入淺出Puppet(一)
★整個puppet自動部署過程中agent和master的詳細的互動過程:
深入淺出Puppet(一)
深入淺出Puppet(一)

★過程說明:

1. Puppet用戶端Agent将節點名與facts資訊發送給Master。

2. Puppet服務端Master通過分類判斷請求的用戶端是誰,它将要做什麼。這個判斷是通過site.pp中包含的

    Node.pp配置檔案定義的。

3. Puppet服務端Master将所需要的Class類資訊進行編譯後存入Catalog并發送給Puppet用戶端Agent,到此完        成第一次互動。

4. Puppet用戶端Agent對Catalog進行代碼驗證(文法檢查及錯誤檢查)并執行。主要是代碼的驗證,并将執行

    過程的資訊及結果寫入日志。

5. Puppet用戶端Agent最終達到最開始所定義的狀态,并且将結果及任何執行資料通過開放API的形式發送給

    Puppet服務端Master。

以上就是puppet的工作原理需要注意是:因為整個過程中都是基于ssl實作的,是以首要的是保證agent和master間可以基于ssl通訊!

★puppet的工作模型:
  • 單機模型:手動應用清單;
  • master/agent:由agent周期性地向Master請求清單并自動應用于本地;

  如上就是整個puppet的工作模型和工作原理,我們主要的學習過程就集中在如何去書寫站點請求manifest(清單)。是以,以下我将首先從puppet的單機模型講起,然後再擴充到master/agent模型。

puppet安裝和配置(單機模型)

  1.puppet安裝

★puppet官方下載下傳路徑:
  • https://yum.puppetlabs.com/el/7/products/x86_64/

 2.單機模型下的puppet程式環境

★配置檔案:/etc/puppet/
  • puppet.conf 

★主程式:/usr/bin/puppet 

★puppet程式:

深入淺出Puppet(一)

示範:

  1.這是我在如上官方下載下傳的puppet相關程式的rpm包,使用yum直接安裝,會自動解決依賴到的ruby環境,如下:

[root@centos7 puppet]# ls
facter-2.4.6-1.el7.x86_64.rpm  puppet-3.8.7-1.el7.noarch.rpm  puppet-server-3.8.7-1.el7.noarch.rpm

[root@centos7 puppet]# yum install ./puppet-3.8.7-1.el7.noarch.rpm ./facter-2.4.6-1.el7.x86_64.rpm 

Dependencies Resolved # 依賴到的相關程式包如下:

=======================================================================================================================================================================
 Package                                 Arch                           Version                               Repository                                          Size
=======================================================================================================================================================================
Installing:
 facter                                  x86_64                         1:2.4.6-1.el7                         /facter-2.4.6-1.el7.x86_64                         273 k
 puppet                                  noarch                         3.8.7-1.el7                           /puppet-3.8.7-1.el7.noarch                         6.3 M
Installing for dependencies:
 hiera                                   noarch                         1:1.3.4-5.el7                         epel                                                25 k
 libselinux-ruby                         x86_64                         2.2.2-6.el7                           CDROM                                              127 k
 ruby-augeas                             x86_64                         0.5.0-1.el7                           epel                                                23 k
 ruby-shadow                             x86_64                         1.4.1-23.el7                          epel                                                14 k

Transaction Summary
=======================================================================================================================================================================
Install  2 Packages (+4 Dependent packages)      

  2.puppet程式相關指令

[root@centos7 ~]# puppet help

Usage: puppet <subcommand> [options] <action> [options]

Available subcommands:
    
  agent             The puppet agent daemon
  apply             Apply Puppet manifests locally # 在本地應用資源清單 
  ca                Local Puppet Certificate Authority management.
  catalog           Compile, save, view, and convert catalogs.
  cert              Manage certificates and requests
  certificate       Provide access to the CA for certificate management.
  certificate_request  Manage certificate requests.
  certificate_revocation_list  Manage the list of revoked certificates.
  config            Interact with Puppet's settings.
  describe          Display help about resource types
  device            Manage remote network devices
  doc               Generate Puppet documentation and references
  facts             Retrieve and store facts.
  file              Retrieve and store files in a filebucket
  filebucket        Store and retrieve files in a filebucket
  help              Display Puppet help.
  inspect           Send an inspection report
  instrumentation_data  Manage instrumentation listener accumulated data. DEPRECATED.
  instrumentation_listener  Manage instrumentation listeners. DEPRECATED.
  instrumentation_probe  Manage instrumentation probes. Deprecated
  key               Create, save, and remove certificate keys.
  kick              Remotely control puppet agent
  man               Display Puppet manual pages.
  master            The puppet master daemon
  module            Creates, installs and searches for modules on the Puppet Forge.
  node              View and manage node definitions.
  parser            Interact directly with the parser.
  plugin            Interact with the Puppet plugin system.
  queue             Deprecated queuing daemon for asynchronous storeconfigs
  report            Create, display, and submit reports.
  resource          The resource abstraction layer shell
  resource_type     View classes, defined resource types, and nodes from all manifests.
  secret_agent      Mimics puppet agent.
  status            View puppet server status.

See 'puppet help <subcommand> <action>' for help on a specific subcommand action.
See 'puppet help <subcommand>' for help on a specific subcommand.
Puppet v3.8.7      

 3.puppet apply:

★作用:
  • Applies a standalone Puppet manifest to the local system.(手動在本地應用自願清單中的配置)
★文法格式:
  • puppet apply  [-d|--debug] [-v|--verbose] [-e|--execute] [--noop] <file>

puppet資源

  • 如果把OS的所有配置,如使用者賬号、特定的檔案、檔案所屬的目錄、運作的服務、程式包以及cron任務等,看作是許多獨立原子單元的集合的話,這些所謂的“單元”就是“資源”,不過,這些資源在其大小、複雜程度以及生命周期的跨度上等多個次元上可能會各不相同。
  • 通常來說,類屬于同一種資源的屬性是相近的,如檔案都有其屬主和屬組,而使用者賬号則由使用者名、UID、GID等組成。但,即便是同一種資源,其在不同OS上的實作方式卻又可能各不相同,例如,在windows上和Linux上啟動和停止服務的方式相去甚遠。
★是以,puppet從以下三個次元來對資源完成抽象:(RAL如何抽象資源的?):
  • 相似的資源被抽象成同一種資源“類型”,如程式包資源、使用者資源及服務資源等;
  • 将資源屬性或狀态的描述與其實作方式剝離開來,如僅說明安裝一個程式包而不用關心其具體是通過yum、pkgadd、ports或是其它方式實作;
  • 僅描述資源的目标狀态,也即期望其實作的結果,而不是其具體過程,如“确定nginx運作起來”而不是具體描述為“運作nginx指令将其啟動起來”;
※這三個也被稱作puppet的資源抽象層(RAL)。RAL由type(類型)和provider(提供者,即不同OS上的特定實作)組成。

 1.puppet 資源結構

在為puppet定義一個資源時,需要為其指定所屬的類型和資源标題,并同時配置一系列的屬性和對應的值。puppet通過其特有的語言來描述和管理資源,如下面所示的資源定義。
深入淺出Puppet(一)
  • 這種文法被稱作“資源申報(resource declaration)”,它是puppet語言的核心組成部分。上述的定義中,僅描述了資源的目标狀态而沒有提到為達成目标所需要采取的任何步驟。而資源定義的核心也可以抽象為type、title、attribute和value四個部分。
  • puppet有許多内置的資源類型,而通過安裝插件還可以繼續新增額外的類型。可以通過puppet官方的類型參考頁面(http://docs.puppetlabs.com/references/latest/type.html)擷取詳細的資訊。也可以使用“puppet describe”指令來擷取puppet目前所支援的類型清單及每種類型的詳細資訊,下面給出了一個簡要的使用說明。

★puppet describe:

☉作用:

  • Prints help about Puppet resource types, providers, and metaparameters.
☉文法格式:
  • puppet describe [-h|--help] [-s|--short] [-p|--providers] [-l|--list] [-m|--meta] [type]
☉常用選項:
  • puppet describe -l:列出所有資源類型;例如puppet支援的所有資源類型及其描述資訊;
  • puppet describe -s <TYPE>:顯示指定類型的簡要幫助資訊;
  • puppet describe <TYPE>:顯示指定資源的詳細說明;
  • puppet describe -m:顯示指定類型的元參數,一般與-s一同使用;

 2.puppet 資源定義:

      如前所述,資源是puppet用于模型化系統配置的基礎單元,每個資源都從某個角度描述了系統屬性,如某程式包必須安裝或某使用者必須移除等。在puppet,用于完成此類功能的代碼也即“資源申報”。

★資源定義:

  • 向資源類型的屬性指派來實作,可稱為資源類型執行個體化;
  • 定義了資源執行個體的檔案即清單,manifest;
★定義資源的文法:
深入淺出Puppet(一)
 在定義時,資源類型必須使用小寫字元;而資源名稱(title)僅是一個字元串,但要求在同一個類型中其必須惟一,這意味着,可以同時有名為nginx的“service”資源和“package”資源,但在“package”類型的資源中隻能有一個名為“nginx”。

puppet資源類型

1.group:    

作用:

  • Manage groups.(管理組)
屬性:
  • name:組名;
  • gid:GID;
  • system:是否為系統組;
  • ensure:目标狀态,present/absent;
  • members:成員使用者;
[root@centos7 ~]# mkdir mainfests
[root@centos7 ~]# cd mainfests/
[root@centos7 mainfests]# ls

# 定義group資源
[root@centos7 mainfests]# vim group.pp
 group {'mygrp':
	name	    => 'mygrp',
	ensure      => present,
	gid         => 3000,
	system      => false,
 }
 
 # 試運作
[root@centos7 mainfests]# puppet apply --verbose --noop group.pp
Notice: Compiled catalog for centos7 in environment production in 0.32 seconds
Info: Applying configuration version '1484056729'
Notice: /Stage[main]/Main/Group[mygrp]/ensure: current_value absent, should be present (noop) #提示目前為absent,應該為present(noop)
Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Info: Creating state file /var/lib/puppet/state/state.yaml
Notice: Finished catalog run in 0.06 seconds

[root@centos7 mainfests]# puppet apply --verbose  group.pp # 真正執行
Notice: Compiled catalog for centos7 in environment production in 0.28 seconds
Info: Applying configuration version '1484057172'
Notice: /Stage[main]/Main/Group[mygrp]/ensure: created # 已經建立
Notice: Finished catalog run in 0.13 seconds

[root@centos7 mainfests]# tail /etc/group
tcpdump:x:72:
memcached:x:985:
dhcpd:x:177:
zabbix:x:984:
varnish:x:983:
haproxy:x:188:
haclient:x:189:
tomcat:x:982:
puppet:x:52:
mygrp:x:3000: # 建立的組      
2.user:  
  • Manage users.(管理使用者)
  • name:使用者名;
  • uid: UID;
  • gid:基本組ID;
  • groups:附加組,不能包含基本組;
  • comment:注釋; 
  • expiry:過期時間 ;
  • home:家目錄;
  • shell:預設shell類型;
  • system:是否為系統使用者 ;
  • ensure:present/absent;
  • password:加密後的密碼串; 
[root@centos7 mainfests]# vim user.pp 
 group {'mygrp':
	name	    => 'mygrp',
	ensure      => present,
	gid         => 3000,
	system      => false,
 }

user {'centos':
        name        => 'centos',
	ensure      =>present,
	uid         =>4000,
	groups      => 'mygrp',
 }
 
[root@centos7 mainfests]# puppet apply --verbose --noop user.pp
Notice: Compiled catalog for centos7 in environment production in 0.51 seconds
Info: Applying configuration version '1484059044'
Notice: /Stage[main]/Main/Group[mygrp]/ensure: current_value absent, should be present (noop)
Notice: /Stage[main]/Main/User[centos]/ensure: current_value absent, should be present (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 2 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.08 seconds

[root@centos7 mainfests]# puppet apply --verbose  user.pp
Notice: Compiled catalog for centos7 in environment production in 0.51 seconds
Info: Applying configuration version '1484059058'
Notice: /Stage[main]/Main/Group[mygrp]/ensure: created
Notice: /Stage[main]/Main/User[centos]/ensure: created
Notice: Finished catalog run in 0.32 seconds      
 3.package:
  • Manage packages.(管理程式包)
  • ensure:installed, present, latest, absent
  • name:包名;
  • source:程式包來源,僅對不會自動下載下傳相關程式包的provider有用,例如rpm或dpkg;
[root@centos7 mainfests]# vim package.pp
package {'nginx':
        ensure   => installed,
	name     => 'nginx'
}       # 沒有指定provider,會自動去搜尋可用的provider,本機隻有yum

[root@centos7 mainfests]# puppet apply --verbose --noop package.pp 
Notice: Compiled catalog for centos7 in environment production in 0.71 seconds
Info: Applying configuration version '1484060692'
Notice: /Stage[main]/Main/Package[nginx]/ensure: current_value absent, should be present (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.40 seconds

# 也可以使用source指定程式寶來源
[root@centos7 mainfests]# cat package.pp
package {'nginx':
        ensure   => installed,
	name     => 'nginx',
	provider => 'rpm',
	source   => '/root/nginx/nginx-1.10.0-1.el7.ngx.x86_64.rpm'
}      
  4.service:
  • Manage running services.
  • ensure:Whether a service should be running. Valid values are `stopped` (also called `false`), `running` (also called `true`).
  • enable:Whether a service should be enabled to start at boot. Valid values are `true`, `false`, `manual`.
  • name:
  • path:The search path for finding init scripts.  Multiple values should be separated by colons or provided as an array. 腳本的搜尋路徑,預設為/etc/init.d/;
  • hasrestart:ture,false
  • hasstatus:ture,false
  • restart:Specify a *restart* command manually.  If left unspecified, the service will be stopped and then started. 通常用于定義reload操作;
[root@centos7 mainfests]# vim service.pp 
package {'nginx':
        ensure   => installed,
	name     => 'nginx',
	provider => 'rpm',
	source   => '/root/nginx/nginx-1.10.0-1.el7.ngx.x86_64.rpm'
}

service {'nginx':
        ensure   => running,
	enable   => true,
} # 沒有指定啟動方式,預設為使用systemd或者service腳本啟動

[root@centos7 mainfests]# puppet apply --verbose --noop  service.pp 
Notice: Compiled catalog for centos7 in environment production in 1.01 seconds
Info: Applying configuration version '1484062595'
Notice: /Stage[main]/Main/Service[nginx]/ensure: current_value stopped, should be running (noop)
Info: /Stage[main]/Main/Service[nginx]: Unscheduling refresh on Service[nginx]
Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.48 seconds

# 我們也可以手動指定啟動
[root@centos7 mainfests]# cat service.pp 
package {'nginx':
        ensure   => installed,
	name     => 'nginx',
	provider => 'rpm',
	source   => '/root/nginx/nginx-1.10.0-1.el7.ngx.x86_64.rpm'
}

service {'nginx':
        ensure   => running,
	enable   => true,
	start    => '/usr/sbin/nginx',
	restart  => '/usr/sbin/nginx -t && /usr/sbin/nginx -s reload',
}      
★資源引用:
  • Type['title']
  • 類型的首字母必須大寫;

★資源有特殊屬性:

名稱變量(namevar):

  • name可省略,此時将由title表示;
ensure:
  • 定義資源的目标狀态;
元參數:metaparameters
  • 依賴關系:

       before:被依賴 (如:A before B ,表示A被B依賴),值為資源引用的值

       require:依賴者(如:B rrquire A ,表示B依賴A)

  • 通知關系:通知相關的其它資源進行“重新整理”操作;

       notify:通知(前資源中定義)例如:a發生改變要通知給b

       subscribe:訂閱(後資源中定義)例如:b要始終關注a的變化,a要變化b也要變化

[root@centos7 mainfests]# vim web.pp
service {'httpd':
        ensure   => running,
        enable   => ture
}

package {'httpd':
        ensure   => installed,
	before   => Service['httpd'] package資源被service所依賴
}

# 或者如下這種寫發也可以:
[root@centos7 mainfests]# cat web.pp 
service {'httpd':
        ensure   => running,
}

package {'httpd':
        ensure   => installed,
}

Package['httpd'] -> Service['httpd'] # 資源依賴鍊,左邊的先執行      
 5.file:
  • Manages files, including their content, ownership, and permissions.
※ensure:Whether the file should exist, and if so what kind of file it should be. Possible values are `present`, `absent`, `file`, `directory`, and `link`.
  • file:類型為普通檔案,其内容由content屬性生成或複制由source屬性指向的檔案路徑來建立;
  • link:類型為符号連結檔案,必須由target屬性指明其連結的目标檔案;
  • directory:類型為目錄,可通過source指向的路徑複制生成,recurse屬性指明是否遞歸複制;

※path:檔案路徑;

※source:源檔案;

※content:檔案内容;

※target:符号連結的目标檔案; 

※owner:屬主

※group:屬組

※mode:權限;

※atime/ctime/mtime:時間戳;

[root@centos7 mainfests]# cat file.pp 
file{'test.txt':
      path    => '/tmp/test.txt',
      ensure  => file,
      source  => '/etc/fstab',
}

file{'test.symlink':
      path    => '/tmp/test.symlink',
      ensure  => link,
      target  => '/tmp/test.txt',   # 連結的目标檔案
      require => File['test.txt'],  # 依賴于檔案test.txt
}

file{'test.dir':
      path    => '/tmp/test.dir',
      ensure  => directory,
      source  => '/etc/yum.repos.d/', #源檔案為目錄檔案
      recurse => true,                # 遞歸複制
}      

 2.定義通知機制,notify

[root@centos7 mainfests]# ss -tnl
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port              
LISTEN      0      25                                                      *:514                                                                 *:*                  
LISTEN      0      128                                                     *:22                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:631                                                                 *:*                  
LISTEN      0      100                                             127.0.0.1:25                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:6010                                                                *:*                  
LISTEN      0      25                                                     :::514                                                                :::*                  
LISTEN      0      128                                                    :::80                                                                 :::*                  
LISTEN      0      128                                                    :::22                                                                 :::*                  
LISTEN      0      128                                                   ::1:631                                                                :::*                  
LISTEN      0      100                                                   ::1:25                                                                 :::*                  
LISTEN      0      128                                                   ::1:6010                                                               :::*                  
LISTEN      0      128                                                    :::443                                                                :::*  

# 如上,httpd監聽在80端口,現在我定義隻要httpd的配置檔案發生改變就通知httpd重新開機服務,如下:
[root@centos7 mainfests]# cp /etc/httpd/conf/httpd.conf ./ # 首先複制配置檔案到目前目錄,并修改監聽端口為8080
[root@centos7 mainfests]# ls
file.pp  group.pp  httpd.conf  package.pp  service.pp  user.pp  web.pp
[root@centos7 mainfests]# vim web.pp
service {'httpd':
        ensure   => running,
	enable   => true,
        restart  => 'systemctl restart httpd.service',  # 重新開機指令
}

package {'httpd':
        ensure   => installed,
}

file {'httpd.conf':
        path     => '/etc/httpd/conf/httpd.conf',  # 檔案路徑
	source   => '/root/mainfests/httpd.conf',  # 源檔案
	ensure   => file,
        notify   => Service['httpd'],              #如果發生改變通知service資源
}

Package['httpd'] -> File['httpd.conf'] -> Service['httpd'] # 資源依賴鍊,從左向右依次執行

# 運作修改後的web.pp
[root@centos7 mainfests]# puppet apply --verbose web.pp 
Notice: Compiled catalog for centos7 in environment production in 0.96 seconds
Info: Applying configuration version '1484191662'
Info: Computing checksum on file /etc/httpd/conf/httpd.conf
Info: /Stage[main]/Main/File[httpd.conf]: Filebucketed /etc/httpd/conf/httpd.conf to puppet with sum 71c33697704e72bf128b75a4a3dd4ad2
Notice: /Stage[main]/Main/File[httpd.conf]/content: content changed '{md5}71c33697704e72bf128b75a4a3dd4ad2' to '{md5}21002918bf1dd3c6f1d0697ec0aa8e8b'
Info: /Stage[main]/Main/File[httpd.conf]: Scheduling refresh of Service[httpd]
Notice: /Stage[main]/Main/Service[httpd]/enable: enable changed 'false' to 'true'
Notice: /Stage[main]/Main/Service[httpd]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 2.02 seconds

[root@centos7 mainfests]# ss -tnl
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port              
LISTEN      0      25                                                      *:514                                                                 *:*                  
LISTEN      0      128                                                     *:22                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:631                                                                 *:*                  
LISTEN      0      100                                             127.0.0.1:25                                                                  *:*                  
LISTEN      0      128                                             127.0.0.1:6010                                                                *:*                  
LISTEN      0      25                                                     :::514                                                                :::*                  
LISTEN      0      128                                                    :::8080                                                               :::*                  
LISTEN      0      128                                                    :::22                                                                 :::*                  
LISTEN      0      128                                                   ::1:631                                                                :::*                  
LISTEN      0      100                                                   ::1:25                                                                 :::*                  
LISTEN      0      128                                                   ::1:6010                                                               :::*                  
LISTEN      0      128                                                    :::443                                                                :::*           

# 發現此時httpd監聽的端口已經由80改為8080      
  6.exec:
  • Executes external commands. Any command in an `exec` resource **must** be able to run multiple times without causing harm --- that is, it must be *idempotent*.(幂等)
  • **command** (*namevar*):要運作的指令;
  • cwd:The directory from which to run the command.   //定義運作指令前要cd到目錄中去
  • **creates**:檔案路徑,僅此路徑表示的檔案不存在時,command方才執行;
  • user/group:運作指令的使用者身份;
  • path:指令的查找路徑,如果沒有指定路徑,則指令必須使用絕對路徑
  • onlyif:此屬性指定一個指令,此指令正常(退出碼為0)運作時,目前command才會運作;(如果...)
  • unless:此屬性指定一個指令,此指令非正常(退出碼為非0)運作時,目前command才會運作;(如果不...)
  • refresh:重新執行目前command的替代指令;
  • refreshonly:僅接收到訂閱的資源的通知時方才運作;
[root@centos7 mainfests]# cat exec.pp 
exec{'mkdir':
      command   => 'mkdir /tmp/hello.dir',
      path      => '/bin:/sbin:/usr/bin:/usr/sbin',
      creates   => '/tmp/hello.dir',   # 或者 unless    => 'test -d /tmp/hello.dir',也可以
}

[root@centos7 mainfests]# puppet apply --verbose  exec.pp 
Notice: Compiled catalog for centos7 in environment production in 0.13 seconds
Info: Applying configuration version '1484206815'
Notice: /Stage[main]/Main/Exec[mkdir]/returns: executed successfully
Notice: Finished catalog run in 0.07 seconds      
 7.cron:
  • Installs and manages cron jobs.  Every cron resource created by Puppet requires a command and at least one periodic attribute (hour, minute, month, monthday, weekday, or special).
  • command:要執行的任務;
  • hour:
  • minute:
  • monthday:
  • month:
  • weekday:
  • user:添加在哪個使用者之上;
  • name:cron job的名稱;
[root@centos7 mainfests]# cat cron.pp 
cron{'timesync':
      command    => '/usr/sbin/ntpdate cn.pool.ntp.org &> /dev/null',
      ensure     =>present,
      minute     => '*/3',
      user       => 'root',
}

[root@centos7 mainfests]# puppet apply --verbose cron.pp 
Notice: Compiled catalog for centos7 in environment production in 5.38 seconds
Info: Applying configuration version '1484207796'
Notice: /Stage[main]/Main/Cron[timesync]/ensure: created
Notice: Finished catalog run in 0.09 seconds

[root@centos7 mainfests]# crontab -l
# HEADER: This file was autogenerated at 2017-01-12 15:56:42 +0800 by puppet.
# HEADER: While it can still be managed manually, it is definitely not recommended.
# HEADER: Note particularly that the comments starting with 'Puppet Name' should
# HEADER: not be deleted, as doing so could cause duplicate cron jobs.
# Puppet Name: timesync
*/3 * * * * /usr/sbin/ntpdate cn.pool.ntp.org &> /dev/null      
 8.notify:
  • Sends an arbitrary message to the agent run-time log. 發送任意指定資訊到運作日志中
  • message:資訊内容
[root@centos7 mainfests]# cat notify.pp 
notify{'hello':
      message  => 'hello there',
      name     => 'pig head',
}

[root@centos7 mainfests]# puppet apply --verbose notify.pp 
Notice: Compiled catalog for centos7 in environment production in 0.11 seconds
Info: Applying configuration version '1484208626'
Notice: hello there
Notice: /Stage[main]/Main/Notify[hello]/message: defined 'message' as 'hello there'
Notice: Finished catalog run in 0.04 seconds      

繼續閱讀