自动化运维管理平台-Puppet资源管理

当我们配置完毕puppet相关信息时,我们需要更近一步来部署puppet真正应用,那puppet怎么使用呢?希望帮助到初学的朋友,一起交流和学习,欢迎大家分享!

  
  
  
  
  1. Puppet常用资源:  

  2. 常用的资源主要有以下几个:  

  3. file:主要负责管理文件  

  4. package:软件包的安装管理  

  5. service:系统服务的管理    

  6. cron:配置自动任务计划    

  7. exec:远程执行运行命令  

  8. 更多资源详细资料,可参见:http://puppet.wikidot.com  

  9. 写一个默认配置:vi  /etc/puppet/manifests/site.pp  

一、File资源:

  
  
  
  
  1. file {  

  2. "/tmp/test.txt":  

  3. content => "hello";  

  4.  }  

  5. 意思是在/tmp新建一个test.text文件,文件内容为hello。  

二、Package资源:

  
  
  
  
  1. package {  

  2. ["screen","ntp"]:  

  3. ensure => "installed";  

  4. "pppoe":  

  5. ensure => "absent";  

  6.  }  

  7. 定义的意思是yum install screen 和ntp服务,并且卸载pppoe安装包。  

三、Service资源:

  
  
  
  
  1. service {    

  2. "sshd":    

  3. ensure => running;    

  4. "nfs":    

  5. ensure => stopped;    

  6.  }    

  7. 意思是定义启动sshd服务,停止nfs服务。

四、 Cron资源:

  
  
  
  
  1. cron{  

  2. "ntpdate":  

  3. command => "/usr/sbin/ntpdate pool.ntp.org",  

  4. user => root,  

  5. hour => 0,  

  6. minute => 0,  

  7.  }  

  8. 意思是在客户端写入一个计划任务:0 0  * * *  /usr/sbin/ntpdate pool.ntp.org 自动同步时间!  

五、向客户端推送本地脚本:

  
  
  
  
  1. 首先修改vi /etc/puppet/fileserver.conf 文件,添加如下三行:  

  2. [files]  

  3. path  /etc/puppet/files  

  4. allow *  

  5. 然后cp所需要的脚本到 /etc/puppet/files目录,没有这个目录则新建!  

  6. file {  

  7.   "/tmp/nginx_install.sh":  

  8. source => "puppet://master.puppet.com/files/nginx_install.sh",  

  9. group => root,  

  10. owner => root,  

  11. mode => "755"  

  12. }  

  13. 意思是把 /etc/puppet/files/nginx_install.sh这个脚本推送到客户端的/tmp/下!  

六、Exec远程执行脚本:

如第五步,我们把nginx_install.sh推送过去后,这时候我们就可以执行了如下:

  
  
  
  
  1. exec {  

  2.  "/tmp/nginx_install.sh":  

  3. cwd => "/tmp",  

  4. user => root,  

  5. path => ["/usr/bin","/usr/sbin","/bin","/bin/sh"],  

  6.  }  

这样的配置后,你可以在客户端测试,测试结果已经自动安装完nginx!如下结果

  
  
  
  
  1. [root@master tmp]# puppetd --server=master.puppet.com --test  

  2. info: Caching catalog for master.puppet.com  

  3. info: Applying configuration version '1337450399'  

  4. notice: /Stage[main]//Node[default]/Exec[/tmp/nginx_install.sh]/returns: executed successfully  

  5. notice: Finished catalog run in 49.96 seconds  

  6. 但是这里有个地方需要注意,这每次同步都会执行这个脚本,所以我们要设置一个参数,如果nginx_install.sh有更新才执行:  

  7. exec {  

  8.  "/tmp/nginx_install.sh":  

  9. cwd => "/tmp",  

  10. user => root,  

  11. path => ["/usr/bin","/usr/sbin","/bin","/bin/sh"],  

  12. subscribe => File["/tmp/nginx_install.sh"],  

  13. refreshonly => true;  

  14.  }  

七、执行命令:

  
  
  
  
  1. ###更新sysctl.conf  

  2.       file { "/etc/sysctl.conf":  

  3. source =>     "puppet://master.puppet.com/files/sysctl.conf",  

  4. owner => "root",  

  5. group => "root",  

  6. mode => 644,  

  7.        }  

  8. exec {  

  9.      "sysctl refresh kernel config":  

  10. path => ["/usr/bin", "/usr/sbin", "/bin", "/sbin"],  

  11. command  => "/sbin/sysctl -p",  

  12. subscribe => File["/etc/sysctl.conf"],  

  13. refreshonly => true  

  14.     }  

有更新才在客户端执行!如果没有更新则不执行!

更深入的moudules配置、更多资源等内容后期不断更新!


你可能感兴趣的:(服务器,服务端,puppet,客户机)