本文收錄在Linux運維企業架構實戰系列html
puppet是什麼,我們先不用專業的名詞解釋它,我們先描述一些工做場景,看明白這些工做場景,天然會知道puppet是什麼。node
(1)場景一:python
管理員想要在100臺服務器上同時建立一個名叫"along"的用戶,怎麼辦,固然,咱們能夠"手動解決",登陸每一臺服務器,而後一臺一臺的建立用戶,若是你真的這樣幹,那麼我只能說,算你狠!!!可是,既然咱們這篇文章是介紹puppet的,咱們確定會推薦你使用puppet解決這個問題。linux
(2)場景二:nginx
公司新買了一堆雲服務器,這些服務器最終可能要提供相同的服務,如今須要管理員在這一堆服務器上安裝一些相同的應用,並且安裝完成後,還須要這些服務器上的應用自動啓動,怎麼辦,固然,手動解決算你狠,兄弟我服了,你說你寫腳本解決,嗯,這是個辦法,在我沒有認識puppet以前我可能也會寫個腳本,可是咱們是介紹puppet的,咱們會推薦你使用puppet解決這個問題。正則表達式
(3)場景三:redis
在一些服務器中,執行大批量的重複操做,咱們可使用puppet。shell
看完上述三個場景,你應該已經大概猜出puppet是幹嘛的了,沒錯,說的糙一點,你能夠把它理解成批量處理工具,可是你又不能徹底把它理解成批量處理工具,由於除了批量處理,他還有一些別的功能和特性,咱們暫且先這麼認爲,以便咱們入門。vim
(1)我本身對puppet的瞭解數組
像puppet這種工具備幾個專業的名詞能夠用來稱呼它們:"配置管理工具"、"自動化運維管理工具"。
Linux中,常見的配置管理工具備puppet、saltstack、ansible(博主以前講解過)、chef等,它們的功能相似,puppet算是這個領域裏面的老大哥,時間久(我說的是創始時間久,不要想歪了····),成熟,被普遍的應用,google、twitter、redhat,cisco等大公司都用到了它,功能強大,puppet是使用ruby研發的,saltstack和ansible都屬於python系的, ansible和saltstack與puppet相比,屬於後起之秀,咱們之後也可能會對它們進行總結,可是如今,咱們只聊puppet。
(2)專業術語介紹
本篇文章主要講解資源;類、模塊、站點清單都在下篇進行詳解。
group | 組 |
user | 用戶 |
packge | 程序包 |
service | 服務 |
file | 文件 |
exec | 執行自定義命令 |
cron | 週期性任務計劃 |
notify | 通知 |
yumrepo | 源 |
host | /etc/hosts中主機解析 |
(1)查詢資源類型和幫助信息
puppet describe [-h|--help] [-s|--short] [-p|--providers] [-l|--list] [-m|--meta] [type]
(2)執行資源清單命令
puppet apply [-h|--help] [-V|--version] [-d|--debug] [-v|--verbose] [-e|--execute] [--detailed-exitcodes] [-l|--logdest <file>] [--noop] [--catalog <catalog>] [--write-catalog-summary] <file>
這類垂直管理系統的使用及活躍,極大減輕了運維人員在重複性、批量化操做方面的負擔,可以很是有效地在各自領域完成既定的運維子目標。但其缺陷在於只能針對某一垂直領域的特定問題進行高效處理,對於它們之間的關聯性很難應對。由於運維的本質是保證服務的可用性,而自動化運維則是在徹底保證這一前提下,儘量將須要人干涉的部分處理掉,因此判斷其優劣的標準則是——與人工處理比,對服務的保證有沒有提升。若是僅是解決報警、部署這些單一動做,後續仍然須要人去處理、去關注、去判斷的話,就離這個目標還有距離,談不上真正的自動化,只能算是工具化。
puppet是一個開源的軟件自動化配置和部署工具,它使用簡單且功能強大,正獲得了愈來愈多地關注,如今不少大型IT公司均在使用puppet對集羣中的軟件進行管理和部署,如google利用puppet管理超過6000臺地mac桌面電腦(2007年數據)。
puppet設計架構是基於c/s架構的。服務器端保存着全部對客戶端服務器的配置代碼,在puppet裏面叫作manifest. 客戶端下載manifest以後,能夠根據manifest對服務器進行配置,例如軟件包管理,用戶管理和文件管理等等。
puppet的使用模型分爲 單機使用模型 和 master/agent模型;本文主要講解單機使用模型,便於入門。
從上述專業的解釋中,咱們能夠得知puppet是C/S架構的,也就是說,它有服務端,也有客戶端,管理員能夠經過puppet服務端(master),管理每一臺被管理的服務器,可是須要puppet客戶端做爲中介,也就是說,puppet客戶端做爲代理(agent),接收來自puppet服務端的配置信息,按照服務端(master)發送過來的配置信息,對被管理服務器進行配置,真正執行配置操做的是puppet客戶端,puppet服務端只負責將配置信息準備好,發送給puppet客戶端,以便客戶端執行具體操做,puppet客戶端還有另外一個做用,就是向puppet服務端發送報告,當客戶端按照配置信息執行完成相關配置之後,會將執行信息發送到服務端,好比執行成功與否,執行結果等,默認狀況下,每30分鐘,puppet客戶端會向puppet服務端發起一次請求,請求受管理服務器的配置信息, puppet服務端將配置信息發送給客戶端,客戶端根據反回的信息進行判斷,判斷被管理服務器是否符合管理員定義的配置,puppet的這種工做模式就是C/S架構的,也能夠理解爲master/agent的工做模式,用文字描述顯得太無力,咱們畫個圖來看看。
那麼,咱們把上圖中的兩臺服務器拿出來,詳細說說它們之間的具體工做流程,可是此處咱們須要說明,在master/agent模型下工做的puppet的工做流程比咱們總結的要複雜一點,可是爲了入門方便,咱們只取出其中的一部分核心的流程進行總結,在後面的實際應用中,咱們會不斷的豐富這些流程,此處先給出一個簡化過的流程圖,以下圖所示。
過程分析:
① 客戶端puppet agent請求catalog,咱們已經說過,catalog其實就是被管理服務器對應的配置文件(通過處理過的配置文件),服務端master收到agent的請求,而後找到對應被管理服務器的"站點清單",或者被稱爲"主機清單",由於一臺"被管理服務器"可能同時擔任多個"角色",每一個角色可能都會對應一個"manifest"(也就是清單),因此,咱們能夠爲每一臺被管理服務器配置一個"站點清單",站點清單也能夠理解成爲一種"清單",只是它是針對一臺服務器而存在的一種清單。
② 服務端master找到被管理服務器的站點清單後,根據站點清單,找到對應服務器須要哪些"清單",由於一臺服務器可能會須要多張"清單",上圖中爲了示例,只畫出了一個清單,可是這不表明一定只有一個。
③ master將找到的全部"清單"進行處理,處理爲catalog。
④ master將處理過的catalog發送到agent端。
⑤ agent收到master發送過來的catalog,而後,agent會查詢"被管理服務器的當前狀態",看看服務器的當前狀態是否符合catalog中定義的目標狀態。
⑥ 若是"被管理服務器的當前狀態"與"catalog中定義的目標狀態"一致,那麼資源對應的操做將不會執行,若是"被管理服務器的當前狀態"與"catalog中定義的目標狀態"不一致,那麼資源對應的操做將會執行,以便讓"被管理服務器"達到管理員指定的"目標狀態"。
⑦ 通過上一步的"狀態判斷",執行對應操做,無論執行是否成功,都會生成對應的報告信息。
⑧ agent將生成的報告信息發送給master端。
上述過程,就是puppet在master/agent模式下的工做流程,咱們說過,默認狀況下,客戶端每隔30分鐘向服務端請求一次配置信息,而後根據服務端返回的配置信息,判斷當前服務器是否處於管理員定義的目標狀態,若是被管理的服務器不處於管理員定義的目標狀態,則須要執行對應的操做,使得被管理主機最終處於管理員定義的目標狀態,咱們也能夠沒必要每次都等待30分鐘,咱們能夠從master端推送catalog到agent端,主動告訴agent端,配置已經發生了改變,請執行對應的操做。這是後話,咱們之後再聊。
固然,puppet也能夠不在master/agent模式下工做,咱們能夠在受管理服務器上只安裝puppet客戶端,使用客戶端手動執行對應配置文件,至關於配置文件中的信息並非經過puppet服務端發送過來,而是經過本地的配置文件獲取,也是能夠的,咱們暫且稱這種不須要puppet服務端的工做模式爲單機模式,咱們在學習puppet時,可使用單機的模式進行練習,可是在生產環境中,通常都使用master/agent的方式使用puppet。
實現定義多個manifests --> complier --> catalog --> apply
首先,咱們仍是來安裝一下puppet,puppet的安裝可使用源碼安裝,也可使用rpm(官方提供)、epel源、官方提供的yum倉庫來安裝(經過下載官方提供的rpm包能夠指定官方的yum倉庫)。
在這裏,咱們就是用 yum 安裝的方式。
[root@along ~]# yum -y install puppet
(1)資源抽象
puppet 從如下三個維度來對資源完成抽象:
這三個也被稱做puppet 的資源抽象層(RAL)
RAL 由type( 類型) 和provider( 提供者,即不一樣OS 上的特定實現)組成。
(2)資源定義
① 資源定義經過向資源類型的屬性賦值來實現,可稱爲資源類型實例化;
② 定義了資源實例的文件即清單,manifest;
③ 定義資源的語法以下:
type {'title': attribute1 => value1, atrribute2 => value2, …… }
注意:type必須使用小寫字符;title是一個字符串,在同一類型中必須唯一;每個屬性之間須要用「,」隔開,最後一個「,」可省略。
例如,能夠同時有名爲nginx 的「service」資源和「package」資源,但在「package」 類型的資源中只能有一個名爲「nginx」的資源。
(3)資源屬性中的三個特殊屬性:
(1)屬性 Parameters:
(2)puppet describe group -s -m 使用以前,能夠查詢這個類型的選項
① 編輯manifest( 清單)
[root@along manifest]# vim group.pp group{'mygrp': name => 'mygrp', ensure => present, gid => 2000, }
② 執行並驗證 manifest
---先預執行一次,檢查是否有語法錯誤 [root@along manifest]# puppet apply -v --noop group.pp Notice: Compiled catalog for along in environment production in 0.18 seconds Info: Applying configuration version '1550042227' Notice: /Stage[main]/Main/Group[mygrp]/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 Info: Creating state file /var/lib/puppet/state/state.yaml Notice: Finished catalog run in 0.03 seconds ---真正執行,執行成功 [root@along manifest]# puppet apply -v group.pp Notice: Compiled catalog for along in environment production in 0.06 seconds Info: Applying configuration version '1550042298' Notice: /Stage[main]/Main/Group[mygrp]/ensure: created Notice: Finished catalog run in 0.09 seconds ---查詢驗證 [root@along manifest]# cat /etc/group |grep mygrp mygrp:x:2000:
(1)屬性 Parameters:
(2)puppet describe user -s -m 使用以前,能夠查詢這個類型的選項
① 編輯manifest( 清單)
[root@along manifest]# vim user1.pp user{'along1': ensure => present, system => false, comment => 'Test user', shell => '/usr/sbin/nologin', home => '/app/along', managehome => true, groups => 'mygrp', uid => 3000, }
② 執行manifest
[root@along manifest]# puppet apply -v --noop user1.pp [root@along manifest]# puppet apply -v user1.pp Notice: Compiled catalog for along in environment production in 0.08 seconds Info: Applying configuration version '1550042932' Notice: /Stage[main]/Main/User[along1]/ensure: created Notice: Finished catalog run in 0.12 seconds [root@along manifest]# cat /etc/passwd |grep along1 along1:x:3000:3000:Test user:/mnt/along:/usr/sbin/nologin
① 屬性:
② puppet describe package -s -m 能夠查詢這個類型的選項
① 建立manifest
[root@along manifest]# vim pkg.pp package{'redis': ensure => installed, provider => yum }
② 執行manifest
[root@along manifest]# puppet apply --noop -v pkg.pp [root@along manifest]# puppet apply -v pkg.pp Notice: Compiled catalog for along in environment production in 0.26 seconds Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') Info: Applying configuration version '1550044027' Notice: /Stage[main]/Main/Package[redis]/ensure: created Notice: Finished catalog run in 6.09 seconds [root@along manifest]# rpm -ql redis
① 屬性:
② puppet describe service -s -m 能夠查詢這個類型的選項
① 編寫manifest 資源清單
[root@along manifest]# vim srv1.pp service{'redis': ensure => running, enable => true, }
② 執行
[root@along manifest]# puppet apply -v --noop srv1.pp [root@along manifest]# puppet apply -v srv1.pp Notice: Compiled catalog for along in environment production in 0.08 seconds Info: Applying configuration version '1550044320' Notice: /Stage[main]/Main/Service[redis]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[redis]: Unscheduling refresh on Service[redis] Notice: Finished catalog run in 0.11 seconds [root@along manifest]# ss -nutl |grep 6379 tcp LISTEN 0 128 127.0.0.1:6379 *:*
① 編寫manifest 資源清單
[root@along manifest]# vim srv2.pp package {'memcached': ensure => installed, } service{'memcached': ensure => running, enable => false, require => Package['memcached'] }
② 執行
[root@along manifest]# puppet apply -v --noop srv2.pp [root@along manifest]# puppet apply -v srv2.pp Notice: Compiled catalog for along in environment production in 0.35 seconds Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') Info: Applying configuration version '1550044434' Notice: /Stage[main]/Main/Package[memcached]/ensure: created Notice: /Stage[main]/Main/Service[memcached]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[memcached]: Unscheduling refresh on Service[memcached] Notice: Finished catalog run in 3.28 seconds [root@along manifest]# ss -nutlp |grep memcached udp UNCONN 0 0 *:11211 *:* users:(("memcached",pid=8573,fd=28))
(1)類型的屬性、選項
① 屬性
② puppet describe file -s -m |tail -20
建立test.txt,且直接寫內容
① 編寫manifest 清單
[root@along manifest]# vim file1.pp file{'/mnt/test.txt': ensure => file, content => "How are you?\nHow old are you?\n", owner => 'along1', group => 'mygrp', mode => '0400' }
② 執行
[root@along manifest]# puppet apply -v --noop file1.pp [root@along manifest]# puppet apply -v file1.pp Notice: Compiled catalog for along in environment production in 0.07 seconds Info: Applying configuration version '1550044597' Notice: /Stage[main]/Main/File[/mnt/test.txt]/ensure: defined content as '{md5}93c1208b855bf27a44bb2eb1b022b8cd' Notice: Finished catalog run in 0.04 seconds [root@along manifest]# ll /mnt/test.txt -r-------- 1 along1 mygrp 30 Feb 13 15:56 /mnt/test.txt [root@along manifest]# cat /mnt/test.txt How are you? How old are you?
① 編寫manifest 清單
[root@along manifest]# vim file2.pp file{'redis.conf': path => '/mnt/redis.conf', ensure => file, source => '/root/manifest/files/redis.conf' } file{'symlink.conf': ensure => link, path => '/mnt/symlink.conf', target => '/mnt/redis.conf', require => File['redis.conf'] }
提示:若要指定source ,最好先把源文件複製到此目錄中,由於在master-agent 模式中,不能確保agent 機器上的路徑下,必定有要複製的文件;本次操做須要在/root/manifests/files/準備好redis.con文件。
[root@along manifest]# mkdir files [root@along manifest]# cp /etc/redis.conf /root/manifest/files/
② 執行
[root@along manifest]# puppet apply -v --noop file2.pp [root@along manifest]# puppet apply -v file2.pp Notice: Compiled catalog for along in environment production in 0.07 seconds Info: Applying configuration version '1550045858' Notice: /Stage[main]/Main/File[redis.conf]/ensure: defined content as '{md5}d98629fded012cd2a25b9db0599a9251' Notice: /Stage[main]/Main/File[symlink.conf]/ensure: created Notice: Finished catalog run in 0.05 seconds [root@along manifest]# ll /mnt/redis.conf /mnt/symlink.conf -rw-r----- 1 777 root 46729 Feb 13 16:17 /mnt/redis.conf lrwxrwxrwx 1 root root 15 Feb 13 16:17 /mnt/symlink.conf -> /mnt/redis.conf
建立目錄,且遞歸複製另外一個目錄
① 編寫manifest 清單
[root@along manifest]# vim file3.pp file{'test.dir': ensure => directory, path => '/app/test.dir', source => '/etc/httpd', recurse => true #遞歸複製 }
注意:
目錄複製目錄,是將目錄下的全部文件遞歸複製;
不要用path 爲目錄,source爲文件,不會將此文件複製到目錄中;只能是目錄複製目錄
② 執行
[root@along manifest]# puppet apply -v file3.pp Notice: Compiled catalog for along in environment production in 0.07 seconds Info: Applying configuration version '1550046089' Notice: /Stage[main]/Main/File[test.dir]/ensure: created Notice: /Stage[main]/Main/File[/mnt/test.dir/along]/ensure: created Notice: /Stage[main]/Main/File[/mnt/test.dir/along/.bash_logout]/ensure: defined content as '{md5}6a5bc1cc5f80a48b540bc09d082b5855' Notice: /Stage[main]/Main/File[/mnt/test.dir/symlink.conf]/ensure: created Notice: /Stage[main]/Main/File[/mnt/test.dir/along/.bash_profile]/ensure: defined content as '{md5}f939eb71a81a9da364410b799e817202' Notice: /Stage[main]/Main/File[/mnt/test.dir/redis.conf]/ensure: defined content as '{md5}d98629fded012cd2a25b9db0599a9251' Notice: /Stage[main]/Main/File[/mnt/test.dir/along/.bashrc]/ensure: defined content as '{md5}2f8222b4f275c4f18e69c34f66d2631b' Notice: /Stage[main]/Main/File[/mnt/test.dir/test.txt]/ensure: defined content as '{md5}93c1208b855bf27a44bb2eb1b022b8cd' Notice: Finished catalog run in 0.08 seconds [root@along manifest]# ll /mnt/test.dir/ total 52 drwx------ 2 along1 along1 62 Feb 13 16:21 along -rw-r----- 1 777 root 46729 Feb 13 16:21 redis.conf lrwxrwxrwx 1 root root 15 Feb 13 16:21 symlink.conf -> /mnt/redis.conf -r-------- 1 along1 mygrp 30 Feb 13 16:21 test.txt
下載redis 包,複製配置文件,啓動redis 服務
① 編寫manifest 清單
[root@along manifest]# vim srv3.pp package{'redis': ensure => installed, } file{'/etc/redis.conf': ensure => file, source => '/root/manifest/files/redis.conf', owner => 'redis', group => 'root', mode => '0640' } service{'redis': ensure => running, enable => false } Package['redis'] -> File['/etc/redis.conf'] -> Service['redis']
注意:我事先將/root/manifest/files/redis.conf配置文件的端口修改成了6300
② 執行
[root@along manifest]# puppet apply -v --noop srv3.pp [root@along manifest]# puppet apply -v srv3.pp Notice: Compiled catalog for along in environment production in 0.46 seconds Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') Info: Applying configuration version '1550046351' Notice: /Stage[main]/Main/Package[redis]/ensure: created Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d98629fded012cd2a25b9db0599a9251 Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}d98629fded012cd2a25b9db0599a9251' to '{md5}d3fc0c22e1a90f88a895242b2a251dad' Notice: /Stage[main]/Main/Service[redis]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[redis]: Unscheduling refresh on Service[redis] Notice: Finished catalog run in 3.65 seconds [root@along manifest]# ss -nutlp |grep 6300 tcp LISTEN 0 128 127.0.0.1:6300 *:* users:(("redis-server",pid=11628,fd=4))
注意:exec資源中的任何命令都必須可以屢次運行而不會形成損害——也就是說,它必須具備冪等性。
建立一個目錄
① 編寫manifest 清單
[root@along manifest]# vim cmd1.pp exec{'cmd': command => '[ -e /mnt/testdir ] || mkdir /mnt/testdir', path => ['/bin','sbin','/usr/bin','/usr/sbin'], # creates => '/mnt/testdir' 判斷目錄是否存在 }
分析:先判斷目錄是否存在,不存在再建立目錄,使命令具備冪等性;就算是目錄已存在,也不會有報錯
② 執行
[root@along manifest]# puppet apply -v cmd1.pp Notice: Compiled catalog for along in environment production in 0.02 seconds Info: Applying configuration version '1550047003' Notice: /Stage[main]/Main/Exec[cmd]/returns: executed successfully Notice: Finished catalog run in 0.03 seconds [root@along manifest]# ll -d /mnt/testdir/ drwxr-xr-x 2 root root 6 Feb 13 16:36 /mnt/testdir/ [root@along manifest]# puppet apply -v cmd1.pp Notice: Compiled catalog for along in environment production in 0.02 seconds Info: Applying configuration version '1550047046' Notice: /Stage[main]/Main/Exec[cmd]/returns: executed successfully Notice: Finished catalog run in 0.03 seconds
---編寫manifest 清單 [root@along manifest]# vim cmd2.pp exec{'user': command => 'useradd along', path => '/bin:sbin:/usr/bin:/usr/sbin', unless => 'id along' } ---執行 [root@along manifest]# puppet apply -v cmd2.pp Notice: Compiled catalog for along in environment production in 0.02 seconds Info: Applying configuration version '1550047187' Notice: Finished catalog run in 0.03 seconds [root@along manifest]# id along uid=1000(along) gid=1000(along) groups=1000(along)
① 編寫manifest 清單
[root@along manifest]# vim cmd3.pp package{'redis': ensure => installed, } file{'/etc/redis.conf': ensure => file, source => '/root/manifest/files/redis.conf', owner => 'redis', group => 'root', mode => '0640' } exec{'redis': command => '/usr/bin/systemctl start redis', refresh => "/usr/bin/systemctl restart redis", user => 'redis', group => 'redis', } Package['redis'] -> File['/etc/redis.conf'] ~> Exec['redis']
注意:當配置文件修改,觸發動做,會執行refresh 命令替代command
② 執行
[root@along manifest]# vim files/redis.conf 把redis 端口修改成7777,再執行 port 7777 [root@along manifest]# puppet apply -v cmd3.pp Notice: Compiled catalog for along in environment production in 0.38 seconds Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') Info: Applying configuration version '1550109397' Notice: /Stage[main]/Main/Exec[redis]/returns: executed successfully Notice: Finished catalog run in 0.12 seconds [root@along manifest]# ss -nutlp |grep redis tcp LISTEN 0 128 127.0.0.1:7777 *:* users:(("redis-server",pid=4988,fd=4))
由Puppet建立的每一個cron資源都須要一個命令和至少一個週期屬性(小時、分鐘、月、月、工做日、或特殊)
① 編寫manifest 清單
下載ntpdate包,並制定計劃任務,每隔5分鐘同步一次時間
[root@along manifest]# vim cron.pp package{'ntpdate': ensure => installed, } cron{'mysync': command => '/usr/sbin/ntpdate 192.168.10.1 &> /dec/null', ensure => present, minute => "*/5", target => 'root' }
② 執行
[root@along manifest]# puppet apply -v cron.pp Notice: Compiled catalog for along in environment production in 0.29 seconds Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') Info: Applying configuration version '1550109974' Notice: /Stage[main]/Main/Cron[mysync]/ensure: created Notice: /Stage[main]/Main/Package[ntpdate]/ensure: created Notice: Finished catalog run in 3.20 seconds [root@along manifest]# crontab -l # HEADER: This file was autogenerated at 2019-02-14 10:06:14 +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: mysync */5 * * * * /usr/sbin/ntpdate 192.168.10.1 &> /dec/null
---編寫manifest 清單 [root@along manifest]# vim notify.pp notify{'syhi': message => "hi there." } ---執行 [root@along manifest]# puppet apply -v notify.pp Notice: Compiled catalog for along in environment production in 0.01 seconds Info: Applying configuration version '1550110275' Notice: hi there. Notice: /Stage[main]/Main/Notify[syhi]/message: defined 'message' as 'hi there.' Notice: Finished catalog run in 0.04 seconds
(1)yumrepo:yum源
(2)host:定義/etc/hosts 中名字解析
(1)格式:3中實現方法
① A before B: B依賴於A,定義在A資源中;
{ ... before => Type['B'], ... }
② B require A: B依賴於A,定義在B資源中;
{ ... require => Type['A'], ... }
③ A -> B,B依賴於A
(2)注意:
① before、 require 和 -> 三者用其一便可
② 資源引用:Type['title']
類型的首字母必須大寫
(3)演示:建立一個用戶,須要依賴一個組
① 建立manifest
[root@along manifest]# vim user2.pp [root@along manifest]# cat user2.pp group{'redhat': ensure => present, # before => User['ilinux'], #方案1 } user{'ilinux': ensure => present, comment => "ilinux.io", groups => 'redhat', require => Group['redhat'], #方案2 } #Group['redhat'] -> User['ilinux'] #方案3
② 執行,先建立了組redhat,再建立了用戶ilinux
[root@along manifest]# puppet apply -v user2.pp Notice: Compiled catalog for along in environment production in 0.14 seconds Info: Applying configuration version '1550043297' Notice: /Stage[main]/Main/Group[redhat]/ensure: created Notice: /Stage[main]/Main/User[ilinux]/ensure: created Notice: Finished catalog run in 0.12 seconds
(1)格式:notify/subscribe
① notify:A notify B:B依賴於A,且A發生改變後會通知B;
{ ... notify => Type['B'], ... }
② subscribe:B subscribe A:B依賴於A,且B監控A資源的變化產生的事件;
{ ... subscribe => Type['A'], ... }
③ A ~ B ,B依賴於A縮寫版
(2)實例:下載redis 包,複製配置文件,啓動redis 服務
① 修改srv3.pp manifest 清單
[root@along manifest]# vim srv3.pp package{'redis': ensure => installed, } file{'/etc/redis.conf': ensure => file, source => '/root/manifest/files/redis.conf', owner => 'redis', group => 'root', mode => '0640', # notify => Service['redis'] } service{'redis': ensure => running, enable => false, hasrestart => true, # subscribe => File['/etc/redis.conf'] } Package['redis'] -> File['/etc/redis.conf'] ~> Service['redis']
② 執行
[root@along manifest]# puppet apply -v --noop srv3.pp [root@along manifest]# puppet apply -v srv3.pp Notice: Compiled catalog for along in environment production in 0.42 seconds Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') Info: Applying configuration version '1550046675' Notice: /Stage[main]/Main/Package[redis]/ensure: created Info: FileBucket got a duplicate file {md5}d98629fded012cd2a25b9db0599a9251 Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d98629fded012cd2a25b9db0599a9251 Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}d98629fded012cd2a25b9db0599a9251' to '{md5}d3fc0c22e1a90f88a895242b2a251dad' Info: /Stage[main]/Main/File[/etc/redis.conf]: Scheduling refresh of Service[redis] Notice: /Stage[main]/Main/Service[redis]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[redis]: Unscheduling refresh on Service[redis] Notice: Finished catalog run in 5.25 seconds [root@along manifest]# ss -nutlp |grep redis tcp LISTEN 0 128 127.0.0.1:6300 *:* users:(("redis-server",pid=12492,fd=4))
③ 修改 /root/manifest/files/redis.conf 把端口改成6000
[root@along manifest]# vim files/redis.conf port 6000 [root@along manifest]# puppet apply -v srv3.pp 再執行,由於文件發生了修改,觸發了重啓服務,端口也確實改成了6000 Notice: Compiled catalog for along in environment production in 0.44 seconds Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') Info: Applying configuration version '1550046726' Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d3fc0c22e1a90f88a895242b2a251dad Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}d3fc0c22e1a90f88a895242b2a251dad' to '{md5}46e070a908eb1aca5c908e62dd4296b9' Info: /Stage[main]/Main/File[/etc/redis.conf]: Scheduling refresh of Service[redis] Notice: /Stage[main]/Main/Service[redis]: Triggered 'refresh' from 1 events Notice: Finished catalog run in 0.23 seconds [root@along manifest]# ss -nutlp |grep redis tcp LISTEN 0 128 127.0.0.1:6000 *:* users:(("redis-server",pid=12785,fd=4))
如同 anssible 同樣,puppet 也能夠定義「標籤」——tag,打了標籤之後,咱們在運行資源的時候就能夠只運行某個打過標籤的部分,而非所有。這樣就更方便於咱們的操做。
一個資源中,能夠有一個tag也能夠有多個。
(1)格式:
資源定義:
type{'title': ... tag => 'TAG1', } type{'title': ... tag => ['TAG1','TAG2',...], }
手動調用:
puppet apply --tags TAG1,TAG2,... FILE.PP
(2)實例:
① 建立manifest
[root@along manifest]# vim srv3.pp package{'redis': ensure => installed, } file{'/etc/redis.conf': ensure => file, source => '/root/manifests/files/redis.conf', owner => 'redis', group => 'root', mode => '0640', tag => 'conf' } service{'redis': ensure => running, enable => false, hasrestart => true } Package['redis'] -> File['/etc/redis.conf'] ~> Service['redis']
② 執行
---先修改files配置文件端口爲8888 [root@along manifest]# vim files/redis.conf port 8888 ---再執行 [root@along manifest]# puppet apply -v --tags conf srv3.pp Notice: Compiled catalog for along in environment production in 0.44 seconds Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false. (at /usr/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default') Info: Applying configuration version '1550113147' Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum fe60391ea96c958fe489f86002679ec3 Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}fe60391ea96c958fe489f86002679ec3' to '{md5}cb214aa5ead0d70454d957ca9125ad01' Info: /Stage[main]/Main/File[/etc/redis.conf]: Scheduling refresh of Service[redis] Notice: /Stage[main]/Main/Service[redis]: Triggered 'refresh' from 1 events Notice: Finished catalog run in 0.17 seconds [root@along manifest]# grep "port 8888" /etc/redis.conf port 8888
puppet 變量以「$」開頭,賦值操做符爲「=」,語法爲$variable_name=value。
(1)數據類型:
(2)正則表達式:
(?<ENABLED OPTION>:<PATTERN>) (?-<DISABLED OPTION>:<PATTERN>) OPTIONS: i:忽略字符大小寫; m:把.當換行符; x:忽略<PATTERN>中的空白字符;
注意:不能賦值給變量,僅能用在接受=~或!~操做符的位置;
(3)puppet的變量種類
puppet 種類有三種,爲facts,內建變量和用戶自定義變量。
(4)變量的做用域
其優先級排序爲:top scope < node scope < class scope