设为首页 收藏本站
查看: 1107|回复: 0

[经验分享] 一步一步学会puppet(一)--工作原理与资源

[复制链接]

尚未签到

发表于 2018-8-2 12:21:19 | 显示全部楼层 |阅读模式
  简介
  puppet是一种Linux、Unix、windows平台的集中配置管理系统;
  使用自有的puppet描述语言,可管理配置文件、用户、cron任务、软件包、系统服务等,puppet把这些系统实体称之为资源;
  puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系
  puppet的基础架构
DSC0000.jpg

  puppet是集中式的配置管理工具,通过自有配置语言对节点进行目标状态定义,并能够基于网络实现目标状态的维护;
  puppet的工作模型
DSC0001.jpg

  puppet通过声明性、基于模块的方法进行IT自动化管理;
  puppet的基本工作流程
DSC0002.jpg

  重要概念
  资源:定义目标状态的核心组件;
  核心资源包括:notify、package、group、user、file、exec、cron、service等;
  模块:以资源为核心,是类的集合,如mod1,mod2
  节点:以被管理主机为为核心,如node1,node2
  puppet利用模块+节点的方式,实现目标状态的定义
  manifest:清单,用于定义并保存资源,是一个资源组织工具;
  facter:获取各被管理节点资源使用情况的方式;
  单机模式下的安装使用
yum -y install ruby # 安装ruby环境  
yum -y localinstall facter-1.7.3-1.el6.x86_64.rpm # puppet 2.7版本依赖facter 2.0以下的版本
  
yum -y localinstall puppet-2.7.25-1.el6.noarch.rpm
  
# 列出资源类型
  
puppet describe -l
  
# 显示资源notify的使用帮助
  
puppet describe notify
  
  实例解析
  核心资源1--notify
  
  
# vi /tmp/test.pp  
notify {'notice':
  message => 'welcome to puppet world', # notify的输出的消息
  
}
  
# puppet apply test.pp
  
# puppet apply test.pp -v # 显示详细信息,包括配置应用版本号
  
# puppet apply test.pp -v -d # 开启debug功能
  核心资源2--package
  
  
# vi nginx.pp  
package {'nginx':
  ensure => present, # 程序包已安装状态
  name => nginx,    # 安装的程序包名
  
}
  
# puppet apply nginx.pp -v
  核心资源3--file
  
# vi file.pp  
file {'abc.txt':
  ensure => present, # 文件存在
  content => 'hello puppet', # 文件内容
  path => "/tmp/abc2.txt", # 生成的文件
  
}
  
file {'fstab.symbolic':
  ensure => present,
  target => "/etc/fstab",    # 链接的源文件
  path => "/tmp/fstab.symbolic", # 生成的文件
  links => follow, # 表示为软链接
  
}
  核心资源4--exec
  
# vi command.pp  
exec {'echo command':
  command => 'mktemp /tmp/tmp.XXXXX', # 执行的命令
  path => '/bin:/sbin:/usr/bin:/usr/sbin', # 命令搜索的路径
  
}
  核心资源5--user/group
  
# vi group_user.pp  
group {'testgrp':
  ensure => present,    # 保证用户组存在
  gid => 1001, # 用户组GID
  
} ->
  
user {'testuser':
  ensure => present,    # 保证用户存在
  gid => 1001,
  uid => 1001,
  home => '/home/test', # 用户家目录
  shell => '/bin/tcsh', # 用户的shell
  password => '$1$7990650b$sAvnSF1/5e4aIWdBMrH7U/', # 用户密码
  managehome => true, # 指定创建用户家目录
  
}
  核心资源6--cron
  
# vi cron.pp  
cron {'ntpdate':
  ensure => present,
  command => '/usr/sbin/ntpdate 172.16.0.1 &> /dev/null', #执行的命令
  minute => '*/5', # 指定cron运行的时间间隔,其它时间段默认都为*
  
}
  核心资源7--资源次序require/before
  
# vi nginx.pp  
package {'nginx':
  ensure => present,
  name => nginx,
  # before => Service['nginx'], # 也可在此指定程序包安装的后续操作
  
}
  
service {'nginx':
  enable => true, # 保证服务开机启动
  ensure => true,
  name => nginx,
  require => Package['nginx'], # 指定服务安装的前提条件
  
}
  核心资源8--资源次序notify/subsribe
  
# vi order.pp  
file {'/tmp/test4.txt':
  ensure => file,
  content => "hello puppet",
  # notify => Exec['monitor'], # 也可在此指定文件创建后,主动通知下一步的命令执行
  
}
  
exec {'monitor':
  command => 'echo "/tmp/test4.txt changed." >> /tmp/monitor.txt',
  refreshonly => true, # 指定只在文件内容发生改变时,才重新执行此处命令
  subscribe => File['/tmp/test4.txt'], # 追踪上一步创建的文件资源改变情况
  path => '/bin:/sbin:/usr/bin:/usr/sbin',
  
}
  核心资源9--资源次序 ->(次序链)/~>(通知链)
  
  
# vi order2.pp  
file {'/tmp/test4.txt':
  ensure => file,
  content => "hello puppet",
  
} ~>     # 在此为通知链,作用类似notify
  
exec {'monitor':
  command => 'echo "/tmp/test4.txt changed." >> /tmp/monitor.txt',
  refreshonly => true,
  path => '/bin:/sbin:/usr/bin:/usr/sbin',
  
}
  变量定义及引用
  
# vi var.pp  
$pkgname='haproxy'
  
package {$pkgname:    # 引用用户自定义变量
  ensure => present,
  
}
  
file {'/tmp/nginx.conf':
  ensure => file,
  content => "worker_processes : $processorcount", # 引用facters变量
  
}
  if语句的使用
  
# vi if.pp  
if $operatingsystem =~ /^(?i-mx:(centos|redhat|fedora))/ { # 正则表达式作为if语句的判断条件
  notice("Welome to $1 linux world.")
  
}
  
if $operatingsystem == 'CentOS' { # 等值比较运算作为if语句的判断条件
  notify {'centos': message => "Welcome to CentOS linux world.",}
  
} elsif $operatingsystem == 'Fedora' {
  notify {'fedora': message => "Welcome to Fedora linux world.",}
  
} else {
  notify {'unkown': message => "Unkown Operating System",}
  
}
  case语句的使用
  
  
# vi case.pp  
case $operatingsystem { # case语句进行多值比较,并执行匹配的响应代码块
  /^(?i-mx:centos|fedora|redhat)/ : {package{"httpd":ensure => present,provider => yum,}}
  /^(?i-mx:ubuntu|debian)/ :{package{"apache2":ensure => present,provider => apt,}}
  default: {notify {"notice":message => "unknown system",}}
  
}
  seletor语句的使用,语法与case类似
  
# vi selector.pp  
$webserver = $operatingsystem ? { # seletor类似三目运算符,直接返回匹配项的value值
  /^(?i-mx:centos|fedora|redhat)/ => 'httpd',
  /^(?i-mx:ubuntu|debian)/ => 'apache2',
  
}
  
$webprovider = $operatingsystem ? {
  /^(?i-mx:centos|fedora|redhat)/ => 'yum',
  /^(?i-mx:ubuntu|debian)/ => 'apt',
  
}
  
package {$webserver:
  ensure => present,
  provider => $webprovider,
  
}
  puppet的类使用
  
  
# vi>
class nginx {  package {'nginx':
  ensure => present,
  }
  
service {'nginx':
  ensure => true,
  require => Package['nginx'],
  }
  
}
  
include nginx # 声明类
  
#>  puppet的带参数的类使用
  
# vi>
$webserver = $operatingsystem ? {
  /^(?i-mx:centos|fedora|redhat)/ => 'httpd',
  /^(?i-mx:ubuntu|debian)/ => 'apache2',
  
}
  
class httpd ($pkgname='apache2') {
  package {"$pkgname":
  ensure => present,
  }
  service {"$pkgname":
  ensure => true,
  require => Package["$pkgname"],
  }
  
}
  
class {'httpd':     # 声明类,并将pkgname变量的值带入
  pkgname => $webserver,
  
}
  puppet模块的使用--实例1
  
# vi>
class nginx {  package {'nginx':
  ensure => present,
  }
  
}
  
class nginx::rproxy inherits nginx { # 定义nginx的子类rproxy,继承nginx类的相关属性
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  source => "/tmp/nginx/nginx.reverse_proxy.conf",
  force => true,
  notify => Service['nginx'],
  } ->
  service {'nginx':
  ensure => true,
  }
  
}
  
class nginx::web inherits nginx {    # 定义nginx的子类web,继承nginx类的相关属性
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  source => "/tmp/nginx/nginx.web.conf",
  notify => Service['nginx'],
  } ->
  service {'nginx':
  ensure => true,
  }
  
}
  
# vi node.pp
  
import "/tmp/class3.pp"
  
include nginx::web    # 声明子类
  
# puppet applay -v node.pp
  puppet模块的使用--构建模块nginx
cd /etc/puppet/modules  
mkdir -pv nginx/{files,lib,manifests,templates} # 创建模块nginx所需的目录结构
  
./nginx/files:    # 编辑类中所需的静态文件
  
total 8
  
-rw-r--r-- 1 root root 1062 May 15 18:15 nginx.reverse_proxy.conf
  
-rw-r--r-- 1 root root 1059 May 15 18:15 nginx.web.conf
  
./nginx/manifests:    # 定义模块所需的清单文件,init.pp文件是必须的
  
total 12
  
-rw-r--r-- 1 root root 131 May 15 18:33 init.pp
  
-rw-r--r-- 1 root root 246 May 15 18:15 rproxy.pp
  
-rw-r--r-- 1 root root 213 May 15 18:34 web.pp
  
# vi init.pp    # 定义nginx主类
  
class nginx {
  package {'nginx':
  ensure => present,
  }
  
}
  
# vi rproxy.pp    # 定义nginx的子类之一
  
class nginx::rproxy inherits nginx {
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  source => 'puppet:///modules/nginx/nginx.reverse_proxy.conf',
  force => true,
  notify => Service['nginx'],
  } ->
  service {'nginx':
  ensure => true,
  }
  
}
  
# vi web.pp    # 定义nginx的子类之一
  
class nginx::web inherits nginx {
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  source => 'puppet:///modules/nginx/nginx.web.conf',
  notify => Service['nginx'],
  }
  service {'nginx':
  ensure => true,
  }
  
}
  
puppet apply -v -e 'include nginx' # 调用模块,执行nginx服务安装配置
  总结
  通过以上实例的理解和学习,可以对puppet中主要的资源类型和用法加深了理解,最后2个实例已经开始尝试已模块化的结构去运行puppet了,下一篇将会介绍puppet更多用法,敬请期待!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-545358-1-1.html 上篇帖子: puppet练习记录一 下篇帖子: 监控puppet日志的python脚本
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表