自动化运维工具puppet学习笔记之基础篇

简介:
 
  1. **************服务端安装软件****************** 
  2. shell#touch /etc/puppet/manifests/site.pp 
  3. shell#cat /etc/hosts 
  4.    192.168.1.254 nat.test.com 
  5. shell#/etc/init.d/puppetmaster start 
  6. shell#puppet cert --sign test.test.com 
  7. 签发证书,需要为每一个域名单独签发 
  8.  
  9. **************客户端配置服务****************** 
  10. shell#cat /etc/hosts 
  11.   192.168.1.254 nat.test.com 
  12.   127.0.0.1 test.test.com 
  13. shell#echo  "server = nat.test.com" >>/etc/puppet/puppet.conf 
  14. shell#puppet agent --no-daemonize --verbose 
  15.  
  16. 服务端文件 
  17. vim /etc/puppet/manifests/site.pp  
  18. node default{ 
  19. file {"/tmp/a.txt": 
  20.      content => "test\n", 
  21.      ensure  => present, 
  22.      backup  => ".bak", 
  23.      mode    => 500, 
  24.      owner   => root, 
  25.      group   => www, 
  26.      } 
  27.  
  28.  
  29. *********************客户端测试**************************** 
  30. puppet agent --verbose  --test 
  31.  
  32. *********************服务端创建模块************************** 
  33. shell#mkdir /etc/puppet/modules/snmpd/{files,manifests,templates} -p 
  34. shell#vim  /etc/puppet/modules/snmpd/manifests 
  35. ###################################################################################################################### 
  36. class snmpd { 
  37.     service { 
  38.         "snmpd": 
  39.         enable    => "true", 
  40.         ensure    => "running", 
  41.         require   => File["snmpd.conf"], 
  42.         subscribe => File["snmpd.conf"], 
  43.         name => $operatingsystem ? { 
  44.             default => "snmpd", 
  45.             }, 
  46.     } 
  47.  
  48.     package { 
  49.         "net-snmp": 
  50.         ensure => present, 
  51.         name => $operatingsystem ? { 
  52.             debian => "snmpd", 
  53.             ubuntu => "snmpd", 
  54.             default => "net-snmp", 
  55.             }, 
  56.     } 
  57.      
  58.     file { 
  59.         "snmpd.conf": 
  60. #         owner  => root, 
  61. #        group  => root, 
  62. #        mode   => 644, 
  63.         require   => Package["net-snmp"], 
  64.         path    => $operatingsystem ?{ 
  65.                default => "/etc/snmp/snmpd.conf", 
  66.                }, 
  67.     } 
  68.  
  69. ##################################################################################################### 
  70.  
  71. vim /etc/puppet/manifests/site.pp 
  72. node default{ 
  73. file {"/tmp/a.txt": 
  74.      content => "This is a test file date\n", 
  75.      ensure  => present, 
  76.      backup  => ".back", 
  77.      mode    => 500, 
  78.      owner   => root, 
  79.      group   => www, 
  80.      } 
  81. node 'test.test.com' { 
  82. file {"/tmp/b.txt": 
  83.      content => "This is a test file\n", 
  84.      ensure  => present, 
  85.      backup  => ".back", 
  86.      mode    => 500, 
  87.      owner   => root, 
  88.      group   => root, 
  89.      } 
  90. include snmpd  #包含创建的类 

 

 
  1.  http://docs.puppetlabs.com/guides/types/service.html 原文地址 
  2. service管理系统运行的服务进程,不幸的是不同的系统管理服务的方式是多样的. 有些系统上面对于服务管理很简单,有些系统提供复杂的强大的服务管理功能.puppet提供最基本的服务管理,你也可以指定provider,使用一些特性. 
  3. 注意,当一个服务从另一个资源收到一个事件,服务会重启,例如配置文件修改,可以要求相应的服务重启.不同的平台重启命令不同,你也可以手工指定重启服务的命令. 
  4. 特性 
  5. controllable provider 提供control 变量 
  6. enableable provider 可以enable和disable服务 
  7. refreshable provider 可以重启服务 
  8. 例子service {             "ssh":             ensure => running;            "nfs":             ensure => stopped;           } 
  9. 参数binary 
  10. 运行服务的命令的路径, 只用于不支持init的操作系统, 如果没有指定启动脚本,就用这个命令来启动服务. 
  11. enable 
  12. 服务在开机的时候是否启动,可以设置的值是true和false,需要provider支持enableable 
  13. ensure 
  14. 是否运行服务, running表示运行服务,stopped 表示停止服务 
  15. hasrestart 
  16. 指出管理脚本是否支持restart参数,如果不支持,就用stop和start实现restart效果. 可以设置的值是true 或 false 
  17. hasstatus 
  18. 指出管理脚本是否支持status参数,puppet用status参数来判断服务是否已经在运行了,如果不支持status参数,puppet利用查找运行进程列表里面是否有服务名来判断服务是否在运行. 可以设置的值是true或false 
  19. name 
  20. 该资源的namevar, 服务的名字,通常就是在/etc/init.d/目录下的名字 
  21. path 
  22. 启动脚本的搜索路径,可以用冒号分割多个路径,或者用数组指定. 
  23. pattern 
  24. 设置搜索进程列表的匹配字符串,用于不支持init脚本的系统.当要停止一个服务的时候,通过查看进程运行列表来判断. 
  25. provider 
  26. puppet提供下面的provider(只列出常见的系统) 
  27. debian debian系统的init模式的管理脚本,支持 enableable, refreshable. 
  28. freebsd init模式,支持enableable, refreshable. 
  29. init 标准的init模式,支持refreshable 
  30. redhat redhat的init模式,支持enableable, refreshable. 
  31. smf solaris新的服务管理框架,支持enableable, refreshable 
  32. restart 
  33. 指定重启脚本,否则就先停止该服务再启动该服务 
  34. start 
  35. 指定启动服务的命令,通常init模式的管理脚本都支持,不需要手工指定 
  36. status 
  37. 指定status命令,如果不指定,就从进程列表查询该服务 
  38. stop 
  39. 指定停止服务的脚本. 

 http://nocap.blog.163.com/blog/static/19052507420121030113615705/

 

 
  1. 官方文档 
  2. http://docs.puppetlabs.com/puppetdocs-latest.tar.gz 
  3. http://www.puppetlabs.com/downloads/docs/puppet_labs_docs_pdfs.zip 
  4.  
  5.  
  6. puppet  cert    sign    mytestagent.example.com     
  7. puppet  cert    sign    --all    
  8. puppet  master  --no-daemonize  --verbose  
  9. puppet  --genconfig 
  10. puppet  agent   --server    myserver.example.com    --waitforcert   60  --test 
  11. puppet  master  --configprint   modulepath 
  12. puppet  config  print   modulepath  --mode  master 
  13.  
  14. auth.conf 
  15. autosign.conf 
  16. ========================================== 
  17. rebuilt.example.com 
  18. *.scratch.example.com 
  19. *.local 
  20. ========================================== 
  21.  
  22. device.conf 
  23. ========================================== 
  24. [device certname] 
  25.         type    <type> 
  26.         url <url> 
  27. [router6.example.com] 
  28.         type    cisco 
  29.         url ssh://admin:password@ef03c87a.local 
  30.  
  31.  
  32. fileserver.conf 
  33. =========================================== 
  34. #   Files   in  the /path/to/files  directory   will    be  served 
  35. #   at  puppet:///mount_point/. 
  36. [mount_point] 
  37. path    /path/to/files 
  38. allow   *.example.com 
  39. deny    *.wireless.example.com 
  40. =========================================== 
  41.  
  42. tagmail.conf 
  43. require 
  44. Set  report=true  on your agent nodes 
  45. Set  reports=tagmail 
  46. Set  the  reportfrom  email address and either  the  smtpserver  or  sendmail  setting on  the puppet master 
  47. Create a  tagmail.conf  file at  the  location specified  in  the   tagmap  setting 
  48. ocated at   /etc/puppet/tagmail.conf  by default 
  49. A comma-separated  list of  tags and  !negated  tags;  valid  tags  include: 
  50. Explicit  tags 
  51. Class names 
  52. Puppet Documentation ? Configuring Puppet 40/411 
  53. “ all ” 
  54. Any valid Puppet  log  level  ( debug ,   info ,   notice ,   warning ,   err ,   alert ,   emerg ,   crit ,  or 
  55. verbose ) 
  56. A colon 
  57. A comma-separated  list of email addresses 
  58. The  list of  tags on a  line builds  the set of  resources whose messages will be  included  in  the mailing; 
  59. each additional  tag adds  to  the set,  and each  !negated  tag subtracts  from  the set. 
  60. So,   for example: 
  61. ============================================================================================== 
  62. all:    log-archive@example.com 
  63. webserver,  !mailserver:    httpadmins@example.com 
  64. emerg,  crit:   james@example.com,  zach@example.com,   ben@example.com 
  65. ============================================================================================== 
  66.  
  67.  
  68. service {   'sshd': 
  69. subscribe   =>  File['sshdconfig'], 
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. define  svn_repo($path) { 
  79. exec    {   "/usr/bin/svnadmin  create  ${path}/${title}": 
  80. unless  =>  "/bin/test  -d  ${path}", 
  81. svn_repo    {   'puppet_repo':  path    =>  '/var/svn_puppet'   } 
  82. svn_repo    {   'other_repo':       path    =>  '/var/svn_other'    } 
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. define  svn_repo($path) { 
  92.      exec   {   "create_repo_${name}": 
  93.      command    =>  "/usr/bin/svnadmin  create  ${path}/${title}", 
  94.      unless     =>  "/bin/test  -d  ${path}", 
  95.      if $require    { 
  96.          Exec["create_repo_${name}"]    {  
  97.          require    +>  $require, 
  98. svn_repo    {   'puppet': 
  99.      path               =>  '/var/svn', 
  100.      Puppet Documentation ? Language Guide 50/411 
  101.      require    =>  Package['subversion'], 
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. 变量赋值 
  111. $value  =   "${one}${two}" 
  112.  
  113. 数组 
  114. $foo    =[ 'one', 'two', 'three' ] 
  115. notice  $foo[1] 
  116. 将返回 two 
  117.  
  118.  
  119. host { 'one.example.com': 
  120.          ensure =>  present, 
  121.          alias  =>  [ 'satu','dua', 'tiga'  ], 
  122.          ip =>  '192.168.100.1', 
  123.  
  124.  
  125. 哈希 
  126. $myhash ={ key1 => 'myval', key2 => $b  } 
  127.  
  128.  访问hash元素 
  129. $myhash ={ key  => { subkey => 'b' }} 
  130. notice($myhash[key][subkey])  
  131.  
  132. 节点赋不同的值 
  133. ========================================================= 
  134. node    a   { 
  135. $setting    =   'this' 
  136. include class_using_setting 
  137. node    b   { 
  138. $setting    =   'that' 
  139. include class_using_setting 
  140. ========================================================= 
  141.  
  142.  
  143. calss的设置 
  144. ========================================================================================= 
  145. $test   =   'top' 
  146. class   myclass { 
  147.         exec    {   "/bin/echo  ${test}":   logoutput   =>  true    } 
  148. class   other   { 
  149.         $test   =   'other' 
  150.         include myclass 
  151. include other 
  152. =========================================================================================== 
  153.  
  154. 访问class里面的值 
  155. ======================================================== 
  156. class   myclass { 
  157. $test   =   'content' 
  158. class   anotherclass    { 
  159. $other  =   $myclass::test 
  160. ======================================================== 
  161.  
  162. 给变量中的数组增加元素 
  163. ========================================================================== 
  164. $ssh_users  =   [   'myself',   'someone'   ] 
  165. class   test    { 
  166.         $ssh_users  +=  ['someone_else'] 
  167. =========================================================================== 
  168.  
  169. 这里的+=是给数组增加值 
  170.  
  171.  
  172.  
  173.  
  174. 变量选择相关 
  175.  
  176.  
  177. file{ '/etc/config': 
  178.         owner     =>    $operatingsystem ? { 
  179.         'sunos'   =>    'adm', 
  180.         'redhat'  =>    'bin', 
  181.         default   =>    undef, 
  182.    }, 
  183. 默认值为undef, 
  184.  
  185.  
  186.  
  187. $owner=$operatingsystem ? { 
  188.       /(redhat|debian)/=>'bin', 
  189.       default          =>undef, 
  190.  
  191.  
  192. $system=$operatingsystem ? { 
  193.       /(redhat|debian)/ => "our system  is $1", 
  194.       default           => "our system  is unknown", 
  195.  
  196. 这里的$1将会返回redhat或者debian 
  197. $0将会返回整行 
  198.  
  199.  
  200.  
  201. case 
  202. ===================================================================================================== 
  203. case    $operatingsystem { 
  204.     'sunos':    {   include solaris }   #   apply   the solaris class 
  205.     'redhat':   {   include redhat  }   #   apply   the redhat  class 
  206.     default:    {   include generic }   #   apply   the generic class 
  207. ==================================================================================================== 
  208. ======================================================================================================= 
  209. case    $hostname { 
  210. 'jack','jill':       {  include hill    }   #   apply   the hill    class 
  211. 'humpty','dumpty':   {  include wall    }   #   apply   the wall    class 
  212. default:         {  include generic }   #   apply   the generic class 
  213. ======================================================================================================== 
  214. 如果v$hostname fact里面含有jack或者jill,那么将会include  hill 
  215. ======================================================================================================== 
  216. case    $hostname   { 
  217.         /^j(ack|ill)$/: {  include  hill   }    #   apply   the hill    class 
  218.         /^[hd]umpty$/:  {  include  wall   }    #   apply   the wall    class 
  219.         default:        {  include generic }    #   apply   the generic class 
  220. =========================================================================================================== 
  221. case    $hostname  { 
  222.         /^j(ack|ill)$/: { notice("Welcome $1!") } 
  223.         default:    { notice("Welcome stranger")    } 
  224. =========================================================================================================== 
  225.  
  226. if $variable { 
  227.      file { '/some/file':   ensure  => present } 
  228. }  else { 
  229.      file { '/some/other/file': ensure  => present } 
  230. ============================================================================================================ 
  231.  
  232. if  $server == 'mongrel'    { 
  233.         include mongrel 
  234. } elsif $server ==  'nginx' { 
  235.         include nginx 
  236. }   else    { 
  237.         include thin 
  238.  
  239.  
  240. if  $ram    >   1024    { 
  241.         $maxclient = 500 
  242.  
  243. ============================================================================================================= 
  244.  
  245. if ( $processor_count > 2 ) and ((  $ram >= 16 * $gigabyte  ) or ( $disksize > 1000 )) { 
  246.     include for_big_irons 
  247. } else { 
  248.    include  for_small_box 
  249. ================================================================================================================ 
  250. unless  $memorysize >   1024    { 
  251. $maxclient  =   500 
  252. ================================================================================================================ 
  253.  
  254. 虚拟资源 
  255. @user   {   'luke': ensure  =>  present } 
  256. User    <|  title   ==  luke    |>  
  257. realize User['luke'] 
  258. #将虚拟资源实例化 
  259.  
  260.  
  261. class   ssh { 
  262. @@sshkey    {   $hostname:  type    =>  dsa,    key =>  $sshdsakey  } 
  263. Sshkey  <<| |>> 
  264.  
  265.  
  266. 正则表达式 
  267.  
  268.  
  269. 安装module 
  270. puppet  module  install puppetlabs-apache   --version   0.0.2 
  271. puppet  module  list 
  272. puppet  module  search  apache 
  273. puppet  module  uninstall   puppetlabs-apache 
  274. puppet  module  upgrade puppetlabs-apache   --version   0.0.3 
  275. https://forge.puppetlabs.com 
  276.  
  277.  
  278. 当一个文件改变的时候如何运行一个命令 
  279. =============================================================================================== 
  280. file    {   "/etc/bind":    source  =>  "/dist/apps/bind"   } 
  281. exec    {   "/usr/bin/ndc   reload": 
  282.     subscribe   =>  File["/etc/bind"], 
  283.     refreshonly =>  true 
  284. ============================================================================================== 
  285.  
  286.  
  287. 如何确保创建一个用户之前另一个组已经存在 
  288. group   {   "fearme": 
  289.         ensure  =>  present, 
  290.         gid =>  1000 
  291. user    {   "tim": 
  292.         ensure  =>  present, 
  293.         gid =>  "fearme", 
  294.         groups  =>  ["adm", "staff",    "root"], 
  295.         membership  =>  minimum, 
  296.         shell   =>  "/bin/bash", 
  297.         require =>  Group["fearme"] 
  298.  
  299.  
  300.  
  301. class   base_class  { 
  302. define  testvar_file($myvar="bob")  { 
  303. file    {   $name: 
  304. content =>  template("john.erb"), 
  305. testvar_file    {   "/tmp/testvar": } 
  306. class   child_class inherits    base_class  { 
  307. Base_class::Testvar_file["/tmp/testvar"]    {   myvar   =>  fred    } 
  308.  
  309.  
  310. gem install rack 
  311. gem install passenger 
  312. passenger-install-apache2-module 

 

本文转自it你好 51CTO博客,原文链接:http://blog.51cto.com/itnihao/1134203,如需转载请自行联系原作者

相关文章
|
运维 Prometheus 监控
别再盲选了!开源运维工具选型这事儿,咱得说人话
别再盲选了!开源运维工具选型这事儿,咱得说人话
766 7
|
10月前
|
数据采集 运维 监控
爬虫与自动化技术深度解析:从数据采集到智能运维的完整实战指南
本文系统解析爬虫与自动化核心技术,涵盖HTTP请求、数据解析、分布式架构及反爬策略,结合Scrapy、Selenium等框架实战,助力构建高效、稳定、合规的数据采集系统。
1393 62
爬虫与自动化技术深度解析:从数据采集到智能运维的完整实战指南
|
人工智能 运维 安全
基于合合信息开源智能终端工具—Chaterm的实战指南【当运维遇上AI,一场效率革命正在发生】
在云计算和多平台运维日益复杂的今天,传统命令行工具正面临前所未有的挑战。工程师不仅要记忆成百上千条操作命令,还需在不同平台之间切换终端、脚本、权限和语法,操作效率与安全性常常难以兼顾。尤其在多云环境、远程办公、跨部门协作频繁的背景下,这些“低效、碎片化、易出错”的传统运维方式,已经严重阻碍了 IT 团队的创新能力和响应速度。 而就在这时,一款由合合信息推出的新型智能终端工具——Chaterm,正在悄然颠覆这一现状。它不仅是一款跨平台终端工具,更是业内率先引入 AI Agent 能力 的“会思考”的云资源管理助手。
|
11月前
|
运维 Linux 网络安全
自动化真能省钱?聊聊运维自动化如何帮企业优化IT成本
自动化真能省钱?聊聊运维自动化如何帮企业优化IT成本
341 4
|
敏捷开发 运维 数据可视化
DevOps看板工具中的协作功能:如何打破开发、测试与运维之间的沟通壁垒
在DevOps实践中,看板工具通过可视化任务管理和自动化流程,提升开发与运维团队的协作效率。它支持敏捷开发、持续交付,助力团队高效应对需求变化,实现跨职能协作与流程优化。
|
运维 监控 安全
从实践到自动化:现代运维管理的转型与挑战
本文探讨了现代运维管理从传统人工模式向自动化转型的必要性与路径,分析了传统运维的痛点,如效率低、响应慢、依赖经验等问题,并介绍了自动化运维在提升效率、降低成本、增强系统稳定性与安全性方面的优势。结合技术工具与实践案例,文章展示了企业如何通过自动化实现运维升级,推动数字化转型,提升业务竞争力。
|
数据采集 运维 安全
Linux设备命令行运维工具WowKey问答
WowKey 是一款用于 Linux 设备运维的工具,可通过命令行手动或自动执行指令剧本,实现批量、标准化操作,如健康检查、数据采集、配置更新等。它简单易用,只需编写 WIS 指令剧本和 APT 帐号密码表文件,学习成本极低。支持不同流派的 Linux 系统,如 RHEL、Debian、SUSE 等,只要使用通用 Shell 命令即可通吃Linux设备。
|
运维 Linux Apache
,自动化运维成为现代IT基础设施的关键部分。Puppet是一款强大的自动化运维工具
【10月更文挑战第7天】随着云计算和容器化技术的发展,自动化运维成为现代IT基础设施的关键部分。Puppet是一款强大的自动化运维工具,通过定义资源状态和关系,确保系统始终处于期望配置状态。本文介绍Puppet的基本概念、安装配置及使用示例,帮助读者快速掌握Puppet,实现高效自动化运维。
536 4
|
机器学习/深度学习 人工智能 运维
机器学习+自动化运维:让服务器自己修Bug,运维变轻松!
机器学习+自动化运维:让服务器自己修Bug,运维变轻松!
651 14

热门文章

最新文章

推荐镜像

更多