歡迎您光臨本站 註冊首頁

Puppet--sudo和ssh自動化管理

←手機掃碼閱讀     火星人 @ 2014-03-08 , reply:0

Puppet自動化管理—sudossh

一.sudo自動化配置

模塊化管理

管理員將類似的配置組合成模塊,比如webserver裡面就包含了web伺服器的所有相關設置.使用模塊可以將puppet

代碼重用和共享.

模塊的目錄路徑

默認路徑:/etc/puppet/modules 或者使用modulepath變數設置路徑

檢查默認的

module路徑

  1. [root@master ~]# puppet --genconfig|grep modulepath
  2. modulepath = /etc/puppet/modules:/usr/share/puppet/modules

創建sudo模塊對應目錄

  1. [root@master ~]# mkdir -p /etc/puppet/modules/sudo/{files,templates,manifests}
  2. [root@master ~]# touch /etc/puppet/modules/sudo/manifests/init.pp


模塊目錄中的manifests目錄包含有init.pp和其他配置文件,init.pp文件是模塊配置的核心文件,每個模塊都包含init.pp文件.files目錄包含有用於傳輸的文件,比如應用的默認配置文件. Templates目錄包含有模塊可能會用到的配置文件的模板.

編輯init.pp文件,內容如下

  1. [root@master ~]# vim /etc/puppet/modules/sudo/manifests/init.pp
  2. class sudo {
  3. package {sudo:
  4. ensure=>present,
  5. }
  6. if $operatingsystem == "Ubuntu" {
  7. package {"sudo-ldap":
  8. ensure=>present,
  9. require=>Package["sudo"],
  10. }
  11. }
  12. file {"/etc/sudoers":
  13. owner=>"root",
  14. group=>"root",
  15. mode=>0440,
  16. source=>"puppet://$puppetserver/modules/sudo/etc/sudoers",
  17. require=>Package["sudo"],
  18. }
  19. }

files

目錄中創建etc目錄,並複製一份sudoer文件到該目錄下
  1. [root@master ~]# mkdir -p /etc/puppet/modules/sudo/files/etc
  2. [root@master ~]# cp /etc/sudoers /etc/puppet/modules/sudo/files/etc/

編輯nodes.pp文件,將

sudo模塊應用到相應的節點
  1. [root@master ~]# vim /etc/puppet/manifests/nodes.pp
  2. node 'client1.centos' {
  3. include sudo
  4. }

當然在site.pp文件中需要包含node.pp文件,並設置$puppetserver

變數
  1. [root@master ~]# vim /etc/puppet/manifests/site.pp
  2. import 'nodes.pp'
  3. $puppetserver="master.puppet"

應該剛剛只針對了client1.centos應用了sudo模塊,需要到該節點上驗證是否成功

[root@client1 ~]# puppetd --server master.puppet --test
  1. notice: Ignoring --listen on onetime run
  2. info: Caching catalog for client1.centos
  3. info: Applying configuration version '1330047901'
  4. notice: /Stage[main]/Sudo/Package[sudo]/ensure: created
  5. notice: Finished catalog run in 26.30 seconds
  6. You have new mail in /var/spool/mail/root

masterfiles目錄下的

Empire CMS,phome.net

sudoers文件稍作修改後,在client1.centos節點上再次驗證
  1. [root@client1 ~]# puppetd --server master.puppet --test
  2. notice: Ignoring --listen on onetime run
  3. info: Caching catalog for client1.centos
  4. info: Applying configuration version '1330047901'
  5. notice: /Stage[main]/Sudo/File[/etc/sudoers]/ensure: defined content as '{md5}4093e52552d97099d003c645f15f9372'
  6. notice: Finished catalog run in 0.37 seconds

配置客戶端自動運行的時間,客戶端增加配置

runinterval
  1. [agent]
  2. # The file in which puppetd stores a list of the classes
  3. # associated with the retrieved configuratiion. Can be loaded in
  4. # the separate ``puppet`` executable using the ``--loadclasses``
  5. # option.
  6. # The default value is '$confdir/classes.txt'.
  7. classfile = $vardir/classes.txt
  8. # Where puppetd caches the local configuration. An
  9. # extension indicating the cache format is added automatically.
  10. # The default value is '$confdir/localconfig'.
  11. localconfig = $vardir/localconfig
  12. server=master.puppet
  13. report=true
  14. listen=true
  15. runinterval=3600

Node的定義

相同功能的node可以一起定義

  1. node 'web1.example.com', 'web2.example.com', 'web3.example.com' { }

定義

node也支持正則表達式
  1. node /^web\d \.example\.com$/ { }

Base node是基本的node,每個節點都會應用的設置可以放在base裡面

  1. node base {
  2. }

Node的定義支持繼承

  1. node webserver inherits base {
  2. }
  3. node 'web.example.com' inherits webserver {
  4. }

二.SSH自動化管理

創建ssh模塊相應的目錄和文件

  1. [root@master ~]# mkdir -p /etc/puppet/modules/ssh/{manifests,templetes,files}

前面

sudo模塊的時候,所有相關的設置都是在init.pp文件中,但再SSH模塊中我們嘗試著將配置分為init.pp,install.pp,config.pp, service.pp,params.pp.

創建配置相應文件

  1. [root@master ~]# touch /etc/puppet/modules/ssh/manifests/{install.pp,config.pp,service.pp}

配置params.pp文件,該文件主要是配置模塊的參數

  1. [root@master ~]# vim /etc/puppet/modules/ssh/manifests/params.pp
  2. class ssh::params {
  3. case $operatingsystem {
  4. Solaris: {
  5. $ssh_package_name ='openssh'
  6. $ssh_service_config='/etc/ssh/sshd_config'
  7. $ssh_service_name='sshd'
  8. }
  9. /(Ubuntu|Debian)/: {
  10. $ssh_package_name='openssh-server'
  11. $ssh_service_config='/etc/ssh/sshd_config'
  12. $ssh_service_name='sshd'
  13. }
  14. /(RedHat|CentOS|Fedora)/: {
  15. $ssh_package_name='openssh-server'
  16. $ssh_service_config='/etc/ssh/sshd_config'
  17. $ssh_service_name='sshd'
  18. }
  19. }
  20. }

編輯ssh模塊的init.pp文件

  1. [root@master ~]# vim /etc/puppet/modules/ssh/manifests/init.pp
  2. class ssh{
  3. Empire CMS,phome.net
    include ssh::params,ssh::install,ssh::config,ssh::service
  4. }

編輯install.pp

  1. [root@master ~]# vim /etc/puppet/modules/ssh/manifests/install.pp
  2. class ssh::install {
  3. package {"$ssh::params::ssh_package_name":
  4. ensure=>installed,
  5. }
  6. }

編輯config.pp

  1. [root@master ~]# vim /etc/puppet/modules/ssh/manifests/config.pp
  2. class ssh::config{
  3. file { $ssh::params::ssh_service_config:
  4. ensure=>present,
  5. owner=>'root',
  6. group=>'root',
  7. mode=>0600,
  8. source=>"puppet://$puppetserver/modules/ssh/sshd_config",
  9. require=>Class["ssh::install"],
  10. notify=>Class["ssh::service"],
  11. }
  12. }

Notify在這裡是發出通知到對應的類,即如果ssh:config改變了,就

notify通知ssh::service類.

編輯service.pp

  1. [root@master ~]# vim /etc/puppet/modules/ssh/manifests/service.pp
  2. class ssh::service{
  3. service{ $ssh::params::ssh_service_name:
  4. ensure=>running,
  5. hasstatus=>true,
  6. hasrestart=>true,
  7. enable=>true,
  8. require=>Class["ssh::config"],
  9. }
  10. }

設置hasstatus告訴puppet該服務支持status命令,即類似service sshd status

設置

hasrestart告訴puppet該服務支持restart命令,即類似service sshd restart

複製默認的sshd_config文件到模塊的files目錄下

  1. [root@master ~]# cp /etc/ssh/sshd_config /etc/puppet/modules/ssh/files/

Ssh模塊設置完成,下面是將該模塊應用到節點上

編輯nodes.pp

  1. [root@master ~]# vim /etc/puppet/manifests/nodes.pp
  2. class base {
  3. include sudo,ssh
  4. }
  5. node 'client1.centos' {
  6. include base
  7. }
  8. node 'client2.centos' {
  9. include base
  10. }

到節點上驗證配置是否正確

  1. [root@client1 ~]# puppetd --server master.puppet --test
  2. notice: Ignoring --listen on onetime run
  3. info: Caching catalog for client1.centos
  4. info: Applying configuration version '1330052716'
  5. --- /etc/ssh/sshd_config 2011-12-08 04:25:10.000000000 0800
  6. /tmp/puppet-file20120224-27947-1eierk0-0 2012-02-24 11:06:15.203891553 0800
  7. @@ -1,3 1,4 @@
  8. # puppet auto configuration
  9. # $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $
  10. # This is the sshd server system-wide configuration file. See
  11. info: FileBucket adding {md5}853a26a0f4b8a7fc8529e45ed57fe67b
  12. info: /Stage[main]/Ssh::Config/File[/etc/ssh/sshd_config]: Filebucketed /etc/ssh/sshd_config to puppet with sum 853a26a0f4b8a7fc8529e45ed57fe67b
  13. notice: /Stage[main]/Ssh::Config/File[/etc/ssh/sshd_config]/content: content changed '{md5}853a26a0f4b8a7fc8529e45ed57fe67b' to '{md5}4a860a0861932b44d8af13e64d953b39'
  14. info: /Stage[main]/Ssh::Config/File[/etc/ssh/sshd_config]: Scheduling refresh of Service[sshd]
  15. notice: /Stage[main]/Ssh::Service/Service[sshd]: Triggered 'refresh' from 1 events
  16. notice: Finished catalog run in 0.81 seconds

本文出自 「Waydee的博客」 博客,請務必保留此出處http://waydee.blog.51cto.com/4677242/847134



[火星人 ] Puppet--sudo和ssh自動化管理已經有577次圍觀

http://coctec.com/docs/linux/show-post-45809.html