天天看點

puppet常用資源類型與使用方法

1、puppet describe -l 檢視puppet支援的資源類型

puppet describe typename 檢視某個資源類型的Parameters,Providers--詳細

puppet describe -s typename -m 檢視某個資源的參數等--簡單

2、puppet apply [-d|--debug] [-v|--verbose] [-e|--execute] [--noop] <file>

puppet help apply 檢視幫助

puppet apply -v 檢視詳情

-d 調試模式

-l 儲存在指定檔案中

--noop 幹跑,不執行

1、Manage groups

屬性:

name:組名;

gid:GID;

system:是否為系統組,true OR false;

ensure:目标狀态,present/absent;

members:成員使用者;

2、例子:建立組

(1) vim group.pp

group{'mygrp':

ensure => present

}

(2)執行

puppet apply -v --noop group.pp 先看看是否有錯

puppet apply -v group.pp 真正執行

1、Manage users.

name:使用者名;

uid: UID;

gid:基本組ID;

groups:附加組,不能包含基本組;

comment:注釋; 

expiry:過期時間 ;

home:家目錄;

shell:預設shell類型;

system:是否為系統使用者 ;

ensure:present/absent;

password:加密後的密碼串;

2、例子:建立使用者

(1)使用者名為user2,不寫name。預設是跟title一直,都是user2

vim user.pp

user{'user2':

ensure => present,

uid => 1021,

groups => 'redhat', 

puppet apply -v --noop user.pp

puppet apply -v user.pp

1、Manage packages.

ensure:installed, present, latest, absent, any version string (implies present)

name:包名;

source:程式包來源,僅對不會自動下載下傳相關程式包的provider有用,例如rpm或dpkg;

provider:指明安裝方式;

2、例子:安裝包

安裝memcached包

package{'memcached':

ensure => installed,

1、Manage running services.

ensure:Whether a service should be running. Valid values are <code>stopped</code> (also called <code>false</code>), <code>running</code> (also called <code>true</code>)

enable:Whether a service should be enabled to start at boot. 開機啟動Valid values are <code>true</code>, <code>false</code>, <code>manual</code>.

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:true |false false :init 腳本中的restart指令将不能用,使用stop ,start,預設false

hasstatus:true|false stasus 指令是否使用

start:手動定義啟動指令;

stop:

status:

restart:Specify a restart command manually. If left unspecified, the service will be stopped and then started. 通常用于定義reload操作;

2、例子:安裝包并啟動服務

service{'memcached':

ensure =&gt; running,

enable =&gt; false,

1、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 <code>present</code>, <code>absent</code>, <code>file</code>, <code>directory</code>, and <code>link</code>.

file:類型為普通檔案,其内容由content屬性生成或複制由source屬性指向的檔案路徑來建立;

link:類型為符号連結檔案,必須由target屬性指明其連結的目标檔案;

directory:類型為目錄,可通過source指向的路徑複制生成,recurse屬性指明是否遞歸複制;

path:檔案路徑;

source:源檔案;

content:檔案内容;

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

owner:屬主

group:屬組

mode:權限;

atime/ctime/mtime:時間戳;

2、例子

(1)例子1:将'/root/manifects/files/redis.conf 的檔案複制到/tmp/redis.conf

file{'/tmp/redis.conf':

ensure =&gt; file,

source =&gt; '/root/manifects/files/redis.conf',

(2)例子2:基于内容複制

将hello 

hi 内容給/tmp/test.txt

file{'/tmp/test.txt':

content =&gt; "hello\nhi\n",

1、Executes external commands. Any command in an <code>exec</code> 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.

creates:檔案路徑,僅此路徑表示的檔案不存在時,command方才執行;

user/group:運作指令的使用者身份;

path:The search path used for command execution. Commands must be fully qualified if no path is specified.

onlyif:此屬性指定一個指令,此指令正常(退出碼為0)運作時,目前command才會運作;

unless:此屬性指定一個指令,此指令非正常(退出碼為非0)運作時,目前command才會運作;

refresh:重新執行目前command的替代指令;

refreshonly:僅接收到訂閱的資源的通知時方才運作;

puppet常用資源類型與使用方法

(1)指定包為redis

(2)指定複制的檔案,并修改複制後檔案的所屬人owner,所屬組root,權限的0640

(3)除非pidof redis-server 不成功才執行上面的指令,重新開機服務(以守護程序方式啟動)

(4)-&gt; 代表依賴關系,~&gt; 代表觸發

1、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:以哪個使用者的身份運作指令

target:添加為哪個使用者的任務

name:cron job的名稱;

2、例子:每3分鐘同步一次伺服器時間

cron{'timesync':

command =&gt; '/usr/sbin/ntpdate 172.16.0.1 &amp;&gt; /dev/null',

minute =&gt; '*/3',

user =&gt; 'root',

1、Sends an arbitrary message to the agent run-time log. 向代理運作時日志發送任意消息。

message:資訊内容

name:資訊名稱;

核心類型:

group: 組

user:使用者

packge:程式包 

service:服務

file:檔案

exec:執行自定義指令,要求幂等

cron:周期性任務計劃

notify:通知

官方位址:

<a href="https://docs.puppet.com/puppet/5.2/cheatsheet_core_types.html#notify/">https://docs.puppet.com/puppet/5.2/cheatsheet_core_types.html#notify/</a>

2、例子:

當redis的配置檔案改變了,會重新開機服務

puppet常用資源類型與使用方法

本文轉自 hawapple 51CTO部落格,原文連結:http://blog.51cto.com/guanm/2051343