Puppet經常使用資源使用詳解

1、關於Puppethtml

puppet是一個IT基礎設施自動化管理工具,它可以幫助系統管理員管理基礎設施的整個生命週期:供應(provisioning)、配置(configuration)、聯動(orchestration)及報告(reporting),基於puppet,能夠實現自動化重複任務,快速部署關鍵性應用以及在本地或雲端完成主動管理變動和快速擴展架構規模等。node


puppet資源nginx

若是把OS的全部配置,如用戶帳號、特定的文件、文件所屬的目錄、運行的服務、程序包以及cron任務等,看做是許多獨立原子單元的集合的話,這些所謂的「單元」就是「資源」,不過,這些資源在其大小、複雜程度以及生命週期的跨度上等多個維度上可能會各不相同。
web

一般來講,類屬於同一種資源的屬性是相近的,如文件都有其屬主和屬組,而用戶帳號則由用戶名、UID、GID等組成。但,即使是同一種資源,其在不一樣OS上的實現方式卻又可能各不相同,例如,在windows上和Linux上啓動和中止服務的方式相去甚遠。
正則表達式


所以,puppet從如下三個維度來對資源完成抽象:
apache

類似的資源被抽象成同一種資源「類型」,如程序包資源、用戶資源及服務資源等;
ubuntu

將資源屬性或狀態的描述與其實現方式剝離開來,如僅說明安裝一個程序包而不用關心其具體是經過yum、pkgadd、ports或是其它方式實現;windows

僅描述資源的目標狀態,也即指望其實現的結果,而不是其具體過程,如「肯定nginx運行起來」而不是具體描述爲「運行nginx命令將其啓動起來」;centos

這三個也被稱做puppet的資源抽象層(RAL)。RAL由type(類型)和provider(提供者,即不一樣OS上的特定實現)組成。
bash


puppet資源解構

在爲puppet定義一個資源時,這種語法被稱做「資源申報(resource declaration)」,它是puppet語言的核心組成部分。上述的定義中,僅描述了資源的目標狀態而沒有提到爲達成目標所須要採起的任何步驟。而資源定義的核心也能夠抽象爲type、title、attribute和value四個部分。


puppet有許多內置的資源類型,而經過安裝插件還能夠繼續新增額外的類型。能夠經過puppet官方的類型參考頁面(http://docs.puppetlabs.com/references/latest/type.html)獲取詳細的信息。也可使"puppet describe"命令來獲取puppet當前所支持的類型列表及每種類型的詳細信息,下面給出了一個簡要的使用說明。

puppet describe -l:例如puppet支持的全部資源類型及其描述信息;

puppet describe -s <TYPE>:列出指定資源的簡要說明;

puppet describe <TYPE>:顯示指定資源的詳細說明;


下面說一下,puppet幾個經常使用的資源使用方法:

首先安裝puppet

# 升級facter版本
[root@node1 ~]# rpm -Uvh facter-1.7.3-1.el6.x86_64.rpm
# 添加epel安裝源,安裝puppet
[root@node1 ~]# yum -y localinstall puppet-2.7.23-1.el6.noarch.rpm


package資源使用:

# 安裝nginx
[root@node1 ~]# vi test.pp
package {'nginx':
    ensure => true,             #確保nginx是被安裝的
}
# 執行test.pp
[root@node1 ~]# puppet apply test.pp
notice: /Stage[main]//Package[nginx]/ensure: ensure changed '1.0.15-5.el6' to 'true'
notice: Finished catalog run in 0.77 seconds
[root@node1 ~]# rpm -q nginx
nginx-1.0.15-5.el6.x86_64


service資源使用

[root@node1 ~]# vi test.pp
package {'nginx':
        ensure => true,
}
service {'nginx':
        ensure => true,         #確保服務是被啓動的
        require => Package['nginx'],    #確保是在安裝nginx以後啓動
}
[root@node1 ~]# puppet apply test.pp
notice: /Stage[main]//Package[nginx]/ensure: ensure changed '1.0.15-5.el6' to 'true'
notice: /Stage[main]//Service[nginx]/ensure: ensure changed 'stopped' to 'running'
notice: Finished catalog run in 1.02 seconds
[root@node1 ~]# ps aux | grep nginx
root      8562  0.0  0.0  94572  2024 ?        Ss   11:31   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     8563  0.0  0.0  94916  2580 ?        S    11:31   0:00 nginx: worker process              
root      8568  0.0  0.0   6384   656 pts/0    R+   11:31   0:00 grep nginx
# 發現nginx已經啓動


group資源使用:

[root@node1 ~]# vi test2.pp
group {'hellopuppet':
        ensure => present,     #確保hellopuppet是被建立的
        gid => 1001,           # gid爲1001
}
[root@node1 ~]# puppet apply test2.pp
notice: /Stage[main]//Group[hellopuppet]/ensure: created
notice: Finished catalog run in 0.08 seconds
[root@node1 ~]# cat /etc/group | grep hellopuppet
hellopuppet:x:1001:


user資源使用:

[root@node1 ~]# vi test2.pp
group {'hellopuppet':
        ensure => present,
        gid => 1001,
}
user {'hellopuppet':
        ensure => present,         #確保用戶被建立
        gid => 1001,               #指定基本組ID
        uid => 1001,               #指定uid
        home => '/home/hellopuppet',  #指定家目錄
        managehome => true,           #建立家目錄
        password => '$1$2b2a85db$liwf2K3dQzc2oaRq/RnUg/', #給用戶建立密碼,可使用openssl passwd -1 -salt `openssl rand -hex 4`命令生成隨機串,而後粘貼在這裏
        require => Group['hellopuppet'],  #在建立hellopuppet組以後,建立用戶
}
[root@node1 ~]# puppet apply test2.pp
notice: /Stage[main]//User[hellopuppet]/ensure: created
notice: Finished catalog run in 0.09 seconds
[root@node1 ~]# cat /etc/passwd | grep hellopuppet
hellopuppet:x:1001:1001::/home/hellopuppet:/bin/bash
[root@node1 ~]# cat /etc/shadow | grep hellopuppet
hellopuppet:$1$2b2a85db$liwf2K3dQzc2oaRq/RnUg/:16173:0:99999:7:::
# 發現用戶已被建立,而且生成密碼


cron資源使用:

[root@node1 ~]# vi test3.pp
cron {'ntpdate':
    command => "/usr/sbin/ntpdate 8.8.8.8",   #執行的命令
    user => root,                      #指定執行用戶
    minute => '*/3',                   #指定每3分鐘執行一次
    ensure => present,                 #確保被建立任務           
                                                                                                                                                                                            
}
[root@node1 ~]# puppet apply test3.pp
notice: /Stage[main]//Cron[ntpdate]/ensure: created
notice: Finished catalog run in 0.03 seconds
[root@node1 ~]# crontab -l
# Puppet Name: ntpdate
*/3 * * * * /usr/sbin/ntpdate 8.8.8.8
# 發現cron任務已經被建立

file資源使用:

[root@node1 ~]# cp /etc/nginx/nginx.conf /tmp/nginx.conf
[root@node1 ~]# mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@node1 ~]# vi test4.pp
file {'/etc/nginx/nginx.conf':
        ensure => file,            #確保nginx.conf是一個文件
        mode => 0644,              #定義權限
        source => '/tmp/nginx.conf', #定義文件的源地址,如過沒就從source複製
}
[root@node1 ~]# puppet apply test4.pp
notice: /Stage[main]//File[/etc/nginx/nginx.conf]/ensure: defined content as '{md5}d9dfc198c249bb4ac341198a752b9458'
notice: Finished catalog run in 0.02 seconds
[root@node1 ~]# ls /etc/nginx/nginx.conf
/etc/nginx/nginx.conf




資源間的應用次序

[root@node1 ~]# yum -y remove nginx
[root@node1 ~]# vi test.pp
service {'nginx':
        ensure => running,
        enable => true,
}
package {'nginx':
        ensure => true,
}
Package['nginx'] -> Service['nginx']
[root@node1 ~]# puppet apply test.pp
notice: /Stage[main]//Package[nginx]/ensure: created
notice: /Stage[main]//Service[nginx]/ensure: ensure changed 'stopped' to 'running'
notice: Finished catalog run in 2.27 seconds
[root@node1 ~]# ps aux | grep nginx
root     13589  0.0  0.0  94572  2028 ?        Ss   11:57   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx    13590  0.0  0.0  94916  2576 ?        S    11:57   0:00 nginx: worker process              
root     13596  0.0  0.0   6384   652 pts/0    R+   11:57   0:00 grep nginx
# Package['nginx'] -> Service['nginx'] 表示先安裝nginx,而後啓動nginx


exec資源使用

[root@node1 ~]# vi test.pp
service {'nginx':
        ensure => running,
        enable => true,
}
package {'nginx':
        ensure => true,
}
exec {'chkconfig nginx':
        path => "/sbin",            #執行命令的,程序路徑
        command => "chkconfig --add nginx; chkconfig nginx off",  #執行的命令
        user => root,    #執行用戶
        group => root,
}
Package['nginx'] -> Service['nginx'] -> Exec['chkconfig nginx']
[root@node1 ~]# yum -y remove nginx
[root@node1 ~]# puppet apply test.pp
notice: /Stage[main]//Package[nginx]/ensure: created
notice: /Stage[main]//Service[nginx]/ensure: ensure changed 'stopped' to 'running'
notice: /Stage[main]//Exec[chkconfig nginx]/returns: executed successfully
notice: Finished catalog run in 2.05 seconds
[root@node1 ~]# chkconfig --list nginx
nginx           0:off   1:off   2:off   3:off   4:off   5:off   6:off

發現資源按照順序依次執行


資源間通知的使用:

[root@node1 ~]# vi test4.pp
file {'/etc/nginx/nginx.conf':
        ensure => file,
        mode => 0644,
        source => '/tmp/nginx.conf',
}
service { 'nginx':
      ensure => running,
      enable => true,
}
File['/etc/nginx/nginx.conf'] ~> Service['nginx']
[root@node1 ~]# vi /tmp/nginx.conf
# 修改源文件中啓動線程爲4
worker_processes  4
[root@node1 ~]# puppet apply test4.pp
notice: /Stage[main]//File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'
notice: /Stage[main]//Service[nginx]: Triggered 'refresh' from 1 events
notice: Finished catalog run in 0.50 seconds
[root@node1 ~]# ps aux | grep nginx
root     15452  0.0  0.0  94572  2024 ?        Ss   12:05   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx    15453  0.0  0.0  94916  2612 ?        S    12:05   0:00 nginx: worker process             
nginx    15454  0.0  0.0  94916  2696 ?        S    12:05   0:00 nginx: worker process             
nginx    15456  0.0  0.0  94916  2696 ?        S    12:05   0:00 nginx: worker process             
nginx    15457  0.0  0.0  94916  2676 ?        S    12:05   0:00 nginx: worker process             
root     15463  0.0  0.0   6384   656 pts/0    R+   12:05   0:00 grep nginx
# 啓動進程變爲4個,
# File['/etc/nginx/nginx.conf'] ~> Service['nginx']表示當配置文件有修改時,重啓nginx

正則表達式的使用

[root@node1 ~]# vi test5.pp
$webserver = $operatingsystem ? {
   /(?i-mx:ubuntu|debian)/        => 'apache2',
   /(?i-mx:centos|fedora|redhat)/ => 'httpd',
}
notify {"$webserver":
        message => "install $webserver",
}
[root@node1 ~]# puppet apply test5.pp
notice: install httpd
notice: /Stage[main]//Notify[httpd]/message: defined 'message' as 'install httpd'
notice: Finished catalog run in 0.02 seconds


首先定義一個webserver變量,$operatingsystem是pupppet中的頂級變量,表示當前系統的版本,能夠隨時調用"?"表示判斷語句,而後後面是正則模式匹配,"i"表示忽略字符大小寫,"-m"表示不把.看成換行符,"x"表示忽略模式中的空白字符和註釋,而後經過notify資源輸出,由於當前是centos系統,因此通知要安裝httpd。


puppet資源使用,暫時先介紹這麼多,之後會不斷更新。

相關文章
相關標籤/搜索