Puppet自动化管理—sudossh

一.sudo自动化配置

模块化管理


管理员将类似的配置组合成模块,比如webserver里面就包含了web服务器的所有相关设置。使用模块可以将puppet代码重用和共享。

模块的目录路径

默认路径:/etc/puppet/modules  或者使用modulepath变量设置路径

检查默认的module路径

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

     创建sudo模块对应目录

 
  
  1. [root@master ~]# mkdir -p /etc/puppet/modules/sudo/{files,templates,manifests} 
  2.  
  3. [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.  
  3.  class sudo { 
  4.  
  5.        package {sudo: 
  6.  
  7.            ensure=>present, 
  8.  
  9.        } 
  10.  
  11.    
  12.  
  13.        if $operatingsystem == "Ubuntu" { 
  14.  
  15.            package {"sudo-ldap"
  16.  
  17.                ensure=>present, 
  18.  
  19.                require=>Package["sudo"], 
  20.  
  21.           } 
  22.  
  23.       }   
  24.  
  25.   
  26.  
  27.       file {"/etc/sudoers"
  28.  
  29.           owner=>"root"
  30.  
  31.           group=>"root"
  32.  
  33.           mode=>0440
  34.  
  35.           source=>"puppet://$puppetserver/modules/sudo/etc/sudoers"
  36.  
  37.           require=>Package["sudo"], 
  38.  
  39.       } 
  40.  
  41.   }   

       files目录中创建etc目录,并复制一份sudoer文件到该目录下

 
  
  1. [root@master ~]# mkdir -p /etc/puppet/modules/sudo/files/etc 
  2.  
  3. [root@master ~]# cp /etc/sudoers  /etc/puppet/modules/sudo/files/etc/ 

       编辑nodes.pp文件,将sudo模块应用到相应的节点

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

       当然在site.pp文件中需要包含node.pp文件,并设置$puppetserver变量

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

       应该刚刚只针对了client1.centos应用了sudo模块,所以需要到该节点上验证是否成功

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

       masterfiles目录下的sudoers文件稍作修改后,在client1.centos节点上再次验证

 
  
  1. [root@client1 ~]# puppetd   --server  master.puppet --test 
  2.  
  3. notice: Ignoring --listen on onetime run 
  4.  
  5. info: Caching catalog for client1.centos 
  6.  
  7. info: Applying configuration version '1330047901' 
  8.  
  9. notice: /Stage[main]/Sudo/File[/etc/sudoers]/ensure: defined content as '{md5}4093e52552d97099d003c645f15f9372' 
  10.  
  11. notice: Finished catalog run in 0.37 seconds 

配置客户端自动运行的时间,客户端增加配置runinterval

 
  
  1. [agent] 
  2.  
  3.     # The file in which puppetd stores a list of the classes 
  4.  
  5.     # associated with the retrieved configuratiion.  Can be loaded in 
  6.  
  7.     # the separate ``puppet`` executable using the ``--loadclasses`` 
  8.  
  9.     # option. 
  10.  
  11.     # The default value is '$confdir/classes.txt'. 
  12.  
  13.     classfile = $vardir/classes.txt 
  14.  
  15.   
  16.  
  17.     # Where puppetd caches the local configuration.  An 
  18.  
  19.     # extension indicating the cache format is added automatically. 
  20.  
  21.     # The default value is '$confdir/localconfig'. 
  22.  
  23.     localconfig = $vardir/localconfig 
  24.  
  25.   
  26.  
  27.     server=master.puppet 
  28.  
  29.     report=true 
  30.  
  31.     listen=true 
  32.  
  33. 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.  
  3. … 
  4.  

       Node的定义支持继承

 
  
  1. node webserver inherits base { 
  2.  
  3. … 
  4.  
  5.  
  6. node 'web.example.com' inherits webserver { 
  7.  
  8. … 
  9.  

二.SSH自动化管理

创建ssh模块相应的目录和文件

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

       前面sudo模块的时候,所有相关的设置都是在init.pp文件中,但再SSH模块中我们尝试着将配置分为init.ppinstall.ppconfig.ppservice.ppparams.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.  
  3.  class ssh::params { 
  4.  
  5.        case $operatingsystem { 
  6.  
  7.            Solaris: { 
  8.  
  9.                $ssh_package_name ='openssh' 
  10.  
  11.                $ssh_service_config='/etc/ssh/sshd_config' 
  12.  
  13.                $ssh_service_name='sshd' 
  14.  
  15.            } 
  16.  
  17.            
  18.  
  19.            /(Ubuntu|Debian)/: { 
  20.  
  21.               $ssh_package_name='openssh-server' 
  22.  
  23.               $ssh_service_config='/etc/ssh/sshd_config' 
  24.  
  25.               $ssh_service_name='sshd' 
  26.  
  27.           } 
  28.  
  29.           
  30.  
  31.           /(RedHat|CentOS|Fedora)/: { 
  32.  
  33.               $ssh_package_name='openssh-server' 
  34.  
  35.               $ssh_service_config='/etc/ssh/sshd_config' 
  36.  
  37.               $ssh_service_name='sshd' 
  38.  
  39.           } 
  40.  
  41.       } 
  42.  
  43.   } 

       编辑ssh模块的init.pp文件

 
  
  1. [root@master ~]# vim /etc/puppet/modules/ssh/manifests/init.pp 
  2.  
  3.   
  4.  
  5.    class ssh{ 
  6.  
  7.        include ssh::params,ssh::install,ssh::config,ssh::service 
  8.  
  9.    } 

       编辑install.pp

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

       编辑config.pp

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

       Notify在这里是发出通知到对应的类,即如果ssh:config改变了,就notify通知ssh::service类。

       编辑service.pp

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

       设置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.  
  3.   
  4.  
  5.    class base { 
  6.  
  7.        include sudo,ssh 
  8.  
  9.    } 
  10.  
  11.    
  12.  
  13.    node 'client1.centos' { 
  14.  
  15.        include base 
  16.  
  17.    } 
  18.  
  19.    
  20.  
  21.    node 'client2.centos' { 
  22.  
  23.       include  base 
  24.  
  25.  } 

       到节点上验证配置是否正确

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