歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Centos 6.3下Puppet功能模塊實例筆記

今天來介紹Puppet的具體實用功能,其實我的理解就是怎麼偷懶,呵呵。

1.填充文件內容:

(server):

修改server端配置文件:

# vi /etc/puppet/manifests/site.pp

-----------------

node default{

 file { "/tmp/test":

         content=> "this is a test file";

}

}

-----------------

重啟puppetmaster,更新配置文件信息。

# service puppetmaster restart

(client):

重啟puppet(可不用重啟)

# service puppet restart

同步文件:

# puppetd --server server.example.com  --test

------------------

warning: peer certificate won't be verified in this SSL session

info: Caching certificate for client.example.com

info: Caching certificate_revocation_list for ca

info: Caching catalog for client.example.com

info: Applying configuration version '1369124449'

notice: /Stage[main]//Node[default]/File[/tmp/test]/ensure: defined content as '{md5}100b144907af2a4786003758a0a6a563'

info: Creating state file /var/lib/puppet/state/state.yaml

notice: Finished catalog run in 0.02 seconds

------------------

查看/tmp/test文件及文件內容

# cat /tmp/test

-----------

this is a test file

-----------

2.文件分發:

通過puppet可以向被管理機上推送文件,方法是使用file類型的source屬性

1:修改/etc/puppet/fileserver.conf

2:修改/etc/puppet/manifests/site.pp

實例:要把server服務器上/root目錄下的puppet-2.6.13.tar.gz傳輸至client服務器的/tmp目錄下,文件名不變。

# vi /etc/puppet/fileserver.conf

----------------

[files]

path /root

allow 192.168.7.0/24

----------------

# vi /etc/puppet/manifests/site.pp

添加到node default{}內:

---------------

  file { "/tmp/puppet-2.6.13.tar.gz":

          source => "puppet://$puppetserver/files/puppet-2.6.13.tar.gz",

}

---------------

重啟poppetmaster服務

# service poppetmaster restart

(client):

執行更新命令

# puppetd --test --server server.example.com

此處“$puppetserver”是puppet Server端的名稱,即本機hostname,網上教程都是在hosts裡

指定,生產環境下建議用內部的DNS上作解析

3.修改文件屬性:

實例:把/tmp/puppet-2.6.13.tar.gz文件的權限改為puppet用戶,並設置權限為777。

(server):

# vi /etc/puppet/manifests/site.pp

在source後添加:

---------------

file { "/tmp/puppet-2.6.13.tar.gz":

          source => "puppet://$puppetserver/files/puppet-2.6.13.tar.gz",

          owner => "puppet",

          group => "puppet",

          mode => 777,

  }

--------------

重啟poppetmaster服務

# service poppetmaster restart

(client):

執行更新命令

# puppetd --test --server server.example.com

4.執行SHELL命令或shell腳本:

實例:通過puppet分發執行shell腳本,在客戶端的/tmp目錄下執行test.sh腳本,該腳本實現在本目錄創建一個testfile文件。

(server):

首先創建一個shell腳本test.sh,並保存在/etc/puppet/fileserver.conf配置文件中設置

的/root目錄下

# cd /root

# vi test.sh

---------------

#!/bin/bash

/bin/touch /tmp/testfile

---------------

重啟poppetmaster服務

# service poppetmaster restart

(client):

執行更新命令

# puppetd --test --server server.example.com

設置文件分發和權限分配:

# vi /etc/puppet/manifests/site.pp

添加到node default{}內:

----------------

file { "/tmp/test.sh":

          source => "puppet://$puppetserver/files/test.sh",

          owner => "puppet",

          group => "puppet",

          mode => 755,

  }

exec { "exec-mkdir":

          cwd => "/tmp",

          command => "sh /tmp/test.sh",

          user => "puppet",

          path => "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin",

  }

----------------

重啟poppetmaster服務

# service poppetmaster restart

(client):

執行更新命令

# puppetd --test --server server.example.com

# ll /tmp

----------------------

.....

-rw-r--r--  1 puppet root         0 5月  22 09:28 testfile

-rwxr-xr-x  1 puppet puppet      37 5月  22 09:28 test.sh

----------------------

 

5.服務啟動及關閉:

可以通過puppet對一些服務進行重啟,狀態等操作。puppet是通過service命令操作的。所以,只能針對在/etc/init.d/目錄下的服務

實例:把客戶端的iptables服務關閉,並把nfs服務啟動

# vi /etc/puppet/manifests/site.pp

添加到node default{}內:

---------------------

service {

           "postfix":

            ensure => stopped;

           "nfs":

            ensure => running;

          }

--------------------

重啟poppetmaster服務

# service poppetmaster restart

(client):

執行更新命令

# puppetd --test --server server.example.com

6.cron計劃任務:

接上面的shell程序實例,在10:27執行test.sh

# vi /etc/puppet/manifests/site.pp

添加到node default{}內:

-----------------

cron { "cron-shell":

           command => "sh /tmp/test.sh",

           user => "puppet",

           minute => "27",

           hour => "10"

  }

-----------------

(client):

執行更新命令

# puppetd --test --server server.example.com

本文出自 “一路向北” 博客,請務必保留此出處http://showerlee.blog.51cto.com/2047005/1206540

Copyright © Linux教程網 All Rights Reserved