puppet 运维工具基础<一>
2017-04-27 21:29
483 查看
运维工具
Puppet,IT基础设施自动化管理工具。使用自有的puppet描述语言,可管理配置文件、用户、cron任务、软件包、系统服务等。puppet采用C/S星状的结构,所有的客户端和一个或几个服务器交互。每个客户端周期的(默认半个小时)向服务器发送请求,获得其最新的配置信息,保证和该配置信息同步。整个生命周期:
provisioning(供应),configuration(配置),orchestration(编排),reporting(报告)
puppet/agent:
master:puppet server
agent:puppet agent(真正执行相应管理操作的核心部件,周期性的向master请求与自己相关的配置)
puppet的三层模型:
配置语言
事务层
资源抽象层
资源类型:例如用户,组,文件,服务,cron任务等
基于ruby语言的研发.
1.定义:使用puppet配置语言去定义基础配置信息。
2.模拟:根据资源关系图,puppet可以模拟部署。
3.强制:比对客户端主机状态和定义的语言是否一致。
4.报告:通过puppet api将执行结果发送给接受者。
Puppet的工作机制
1.客户端puppet向master发起认证请求,或使用带签名的证书。
2.Master告诉客户端,你是合法的。
3.客户端puppet调用facter,facter探测主机的一些变量,例如主机名、内存、IP等等,peppet将这些信息通过SSL
连接发送给服务器端
4.服务器端检测客户端的主机名,然后找到manifest对应的node配置,并且对该部分的内容进行解析。解析成一个
中间的伪代码(catalog),然后将伪代码发给客户端
5.客户端接收到伪代码并且执行。
6.客户端执行时判断有没有File文件,如果有,则向fileserver发起请求。
7.客户端如果有Report,则把执行结果发送给服务器。
8.服务器把客户端的执行结果写入日志,并发送给报告系统。
Puppet安装配置
按照网上的方式,我配了好久,发现我老是因为ruby(selinux)什么玩意儿的而不能yum安装通过,centos6.5和centos7.2都试了,都不行,按照网上的方式完全装不上,终于找到了解决方法。就是先装这个包rubygems-1.3.7-1.el6.noarch.rpm最后安装puppet和facter。用yum 的epel源
系统环境 redhat 6.5 selinux和iptables都关闭。
Usage: puppet <subcommand> [options] <action> [options]
获取所支持的所有资源类型:
# puppet describe -l
# puppet describe RESOURCE_TYPE
资源定义语法1.定义资源时,只能使用小写。
2.资源名称必须唯一。
资源
常用的资源类型:user,group,file,package,exec,notify......
简单的演示一下自动化的执行过程。
[root@localhost ~]# mkdir manifests
[root@localhost ~]# cd manifests/
[root@localhost manifests]# vim test.pp
group{'distro': gid => 2000, ensure => present, before => User['redhat'], } user{'redhat': uid => '2000', gid => '2000', shell => '/bin/bash', home => '/bin/redhat', ensure => present, require => Group['ditro'], }
执行
[root@localhost manifests]# puppet apply -v test.pp Notice: Compiled catalog for localhost.lan in environment production in 0.22 seconds Info: Applying configuration version '1493142235' Notice: /Stage[main]/Main/Group[distro]/ensure: created Notice: /Stage[main]/Main/User[redhat]/ensure: created Info: Creating state file /var/lib/puppet/state/state.yaml Notice: Finished catalog run in 0.10 seconds
用户redhat,和组distro被创建
[root@localhost manifests]# tail /etc/passwd|grep redhat redhat:x:2000:2000::/bin/redhat:/bin/bash
常用资源。
<1>group:
name:组名
gid:GID
system:true,flase (系统组)
members:组内成员
ensure:present,absent
<2>user:
commet:注释信息
ensure:present,absent
groups:附加组
home:家目录
shell:默认shell
name:用户名(namevar)
system:是否是系统用户
password:密码
<3>file:
(管理文件、目录、符号链接,权限)
content:直接给出文件内容,支持\n,\t:
source:从指定位置下载文件,支持的url只有file和puppet(master服务器),与source和target互斥
ensure:file,directory,link,present(文件不存在则创建),absent(存在则删除)
force:当前的唯一作用是用在把一个目录变成一个链接,可用值,yes,no
group:属组
owner:属主
target:当ensure为link时,指定被链接的文件
mode:权限 rwx 1777 ...
path:目标路径
recurse:递归操作
backup:通过backup备份文件
更详细的资料可以参考:http://puppet.wikidot.com/file
演示file资源
file{'/tmp/mydir': ensure => directory, } file{'/tmp/puppet.file': content => 'puppet testing', ensure => file, mode => 0600, owner => root, group => root, } file{'/tmp/fstab.puppet': source => '/etc/fstab', ensure => file, } file{'/tmp/puppet.link': ensure => link, target => '/tmp/puppet.file' }
执行结果
[root@localhost manifests]# puppet apply -v file.pp Notice: Compiled catalog for localhost.lan in environment production in 0.08 seconds Info: Applying configuration version '1493144452' Notice: /Stage[main]/Main/File[/tmp/fstab.puppet]/ensure: defined content as '{md5}03e599f455aed424ce17656764437a96' Notice: /Stage[main]/Main/File[/tmp/mydir]/ensure: created Notice: /Stage[main]/Main/File[/tmp/puppet.file]/ensure: defined content as '{md5}1049fff3799b9ec7da193d9eb36ae32e' Notice: /Stage[main]/Main/File[/tmp/puppet.link]/ensure: created Notice: Finished catalog run in 0.05 seconds
<4>exec:
(执行命令相关)
cammond:运行的命令
path:命令的路径
user:以指定用户身份去运行
onlyif:给定测试命令,仅在此命令执行成功时才运行cammond命令。
cwd:进入该目录后,执行命令。
timeout:超时时长,默认单位为秒
tries:尝试执行的次数
refresh:定义如何更新命令。当exec收到一个来自其他资源的事件时,默认只会重新执行一次命令。不过这个参数允许你定义更新时执行不同的命令。
refreshonly:在一个依赖的对象被改变时,命令才会被执行
cwd:进入指定的目录。
creates:后跟文件名,文件不存在时才会执行。
演示exec资源。
exec{'/etc/init.d/httpd start': user => root, group => root, path => '/usr/bin', refresh => 'lsof -i :80 && /etc/init.d/httpd start', timeout => 10, tries => 2, }
<5>cron:
(定时任务相关)
ensure:present,absent
command:要运行的jobs
hour:小时
minute:分钟
monthday:
weekday:
user:运行的用户
cron{'cron time': command => '/usr/sbin/ntpdate ntp.sjtu.edu.cn', user => 'root', hour => ['2-5'], minute => '*/10', }
<6>notify:
(通知相关)
message:提示信息(namevar)
name:
withpath:
notify{"hello,world": }
<7>package:(安装软件包相关)
configfiles:升级软件的话,原有的配置文件如何处理(replace|keep)
ensure:lastes(安装最新版本),installed(安装),pureged(清除)
name:装的程序包的名字(namevar)
source:rpm包的路径
provider:指明yum或者rpm去安装
演示(第一个用yum去安装,第二个去用rpm安装一个rpm软件包)
package{'nmap': ensure => latest, provider => yum, } package{'zsh': ensure => installed, source => '/usr/local/src/zsh-4.3.10-7.el6.x86_64.rpm', provider => rpm, }
执行结果。
[root@localhost manifests]# puppet apply -v packages.pp Notice: Compiled catalog for localhost.lan in environment production in 0.24 seconds Info: Applying configuration version '1493154486' Notice: /Stage[main]/Main/Package[zsh]/ensure: created Notice: /Stage[main]/Main/Package[nmap]/ensure: created Notice: Finished catalog run in 9.46 seconds
<8>service
(服务管理相关)
enable:是否开机自启,(true|false)
ensure:启动(running),停止(stopped)
hasrestart:是否支持restart
hassrtatus:是否支持status
name:服务名称(namevar)
path:脚本查找路径
pattern:当脚本不支持restart|status时,用于确定服务是否处于运行状态(用进程列表去判断)
restart:用于重启服务:
start、start、status:自定义相关的指令(后跟命令)
演示
package{"nginx": ensure => latest, } service{"nginx": ensure => running, enable => true, hasrestart => true, hasstatus => true, restart => '/etc/init.d/nginx reload', }
结果
[root@localhost manifests]# puppet apply service.pp -v Notice: Compiled catalog for localhost.lan in environment production in 0.36 seconds Info: Applying configuration version '1493155762' Notice: /Stage[main]/Main/Package[nginx]/ensure: created Notice: /Stage[main]/Main/Service[nginx]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[nginx]: Unscheduling refresh on Service[nginx] Notice: Finished catalog run in 52.73 seconds
特殊属性 metaparameters
‘before,require’,‘notify,subscribe’四个元参数来定义资源之间的相关性。
依赖关系:
被依赖的资源使用 before
依赖其他资源使用 require
-> 链式依赖
通知关系:
通知其他资源用 notify
监听其他资源用 subscribe
~> 链式通知
举个例子来说明
before和require 是依赖关系的定义。
group{'distro': gid => 2000, ensure => present, before => User['redhat'], ##依赖方式1 :该资源必须先于 redhat 存在 } -> ##依赖方式2:链式执行 user{'redhat': uid => '2000', gid => '2000', shell => '/bin/bash', home => '/bin/redhat', ensure => present, require => Group['ditro'], ##依赖方式3: 该资源需要ditro资源先运行 }
notify和subscribe是其他文件改变去执行某个动作。用在改变了配置文件就重启最好。
举个例子
file{'/etc/nginx/conf.d/default.conf': ensure => file, mode => 644, source => '/etc/nginx/conf.d/default.conf', notify => Service['nginx'], } service{'nginx': ensure => running, enable => true, hasstatus => true, hasrestart => true, }
变量
puppet的变量都是以$开头puppet复制符号 =
Top scope(全局有效的变量),Node scope(仅在节点有效),Class scope(在类中有效)
puppet的变量名称必须有两个名字,简短名称和长格式名称
puppet语言支持多种数据类型以用于变量和属性的值,以及函数的参数。
字符型:
1.非结构化的字符串,可以使用引号也可以不使用
2.单引号中的变量不会替换,而双引号中的能够进行变量替换
3.字符型值也支持使用转义
数组:
1.数组值为中括号[]中以逗号分隔的项目列表,最后一个项目后面可以有逗号。
2.数组中的元素可以为任意可用数据类型,包括hash或其他数组
3.数组引索以0开始,也可以使用负数
布尔型:
1.true和false
undef:
1.从未声明的变量的值为undef。
2.也可手动为某变量赋予undef,
hash:
1.键值对定义在{}中,彼此使用逗号分隔。{'1' => 'red','2' => 'blue','3' => 'yellow'}
正则表达式:
不能复制给变量,仅能用于有限的几个接收正则的地方。
(?<ENABLED OPTION>:<SUBPATTERN>)和(?-<ENABLED OPTION>:<SUBPATTERN>)
OPTION:
i:忽略字符的大小写
m:把.当换行符
x:忽略模式中的空白和注释
表达式:
and or !,+,-,*,/,%,>>,<<
puppet中的变量
自定义变量
facter变量,可直接引用:(主机的环境)
facter -p去查看
客户端内置
$clientcert
$clientversion
服务器端内置
$servername
$serverip
$serverversion
条件判断
if,case,selector
if例子:(因为selinux是facter中的变量)
if $selinux == 'true'{ notice("SeLinux on") } else{ notice("SeLinux off") }
根据Linux 的发行版本去改变欢迎语
if $operatingsystem =~ /^(?i-mx:(centos|redhat))/ { notice("Welcome ro $1 Linux server") }
case例子
case $operatingsystem { 'Solaris': {notice("Welcome to Solaris")} 'CentOS','redhat': {notice("Welcome to RedHat OSFamly")} /^(Debian|Ubuntu)$/: {notice("Welcome to $1 Linux")} default: {notice("Not Welcome Windows")} }
case后面可以跟字符串,变量,有返回值的函数等等。
selector直接返回一个值,和case类似。但不是去执行一个代码块,常用作变量赋值
$webserver = $operatingsystem ? { /(?i-mx:ubuntu|debian)/ => 'apache2', /(?i-mx:centos|redhat)/ => 'httpd', }
类:Class
类是用于一组公共资源,创建后,可以在puppet全局调用,类可以被继承。语法格式:
class class_name {
...puppet code ...
}
例子:
class apache {
package { httpd:
ensure => latest,
}
file { 'httpd.conf':
path => '/etc/httpd/conf/httpd.conf',
ensure => file,
require => Package['httpd'],
}
service { httpd:
ensure => runnig,
require => Package['httpd'],
subscribe => File['httpd.conf'],
}
}
引用类。
include base::linux,apache
require apache
class nginx { $webserver=nginx package{$webserver: ensure => latest, } file{'/etc/nginx/conf.d/default.conf': ensure => file, mode => 644, source => '/etc/nginx/conf.d/default.conf', require => Package['nginx'], notify => Service['nginx'], } service{'nginx': ensure => running, enable => true, hasstatus => true, hasrestart => true, } } include nginx
定义能够接受参数的类:
class class_name($arg1='vlaue1',$args2='vlaue2') {
...puppet code ...
}
class nginx($webserver=nginx) { package{$webserver: ensure => latest, } ....... } include nginx
类继承:
class base_class::class_name
作用:继承一个已有的类,并实现覆盖资源属性,或向资源属性追加额外值。
=>,+>
类继承时:
(1).声明一个子类时,其基类会被自动首先声明。
(2).基类成为了子类的父作用域,基类中的变量和属性默认会被子类复制一份。
class nginx { package{'nginx': ensure => latest, } -> service{'nginx': enable => true, ensure => running, hasrestart => true, hasstatus => true, restart => 'service nginx reload', } } class nginx::webserver inherits nginx{ file{'/etc/nginx/nginx.conf': source => '/root/modules/nginx/files/nginx_web.conf', ensure => file, notify => Service['nginx'], } } class nginx::proxy inherits nginx { file{'/etc/nginx/nginx.conf': source => '/root/modules/nginx/files/nginx_proxy.conf', ensure => file, notify => Service['nginx'], } } include nginx::webserver
子类的重写
class nginx { package{'nginx': ensure => latest, } -> service{'nginx': enable => true, ensure => running, hasrestart => true, restart => 'service nginx reload', } } class nginx::webserver inherits nginx{ Package['nginx'] { name => httpd, } file{'/etc/nginx/nginx.conf': source => /root/modules/nginx/files/nginx_web.conf, ensure => file, notify => Service['nginx'], } } include webserver
模板:
基于ERB模板语言,在静态文件中使用变量等变成元素生成适用于多种不同环境的文本文件。用于实现在文本文件中嵌入ruby代码,原来的文本信息不会被改变,但ruby会被执行,执行结果将直接替换原来的代码。
<%= Ruby Expression %> 替换为表达式的值
<%= Ruby Expression %> 仅执行代码, 而不替换
<%# comment %> 文本注释
<% if CONDITION -%>
some text
<% end %>
..... file{'/etc/nginx/nginx.conf': content => template('/root/modules/nginx/files/nginx_web.conf'), ensure => file, notify => Service['nginx'], } ......
模块:
[root@localhost puppet]# lsauth.conf modules puppet.conf
module_name/
manifests/
init.pp 至少应该包含一个与当前模块名称同名类
files:静态文件 puppet:///modules/module_name/file_name;
templates:模板文件目录 template('module_name/template_file_name')
lib:插件目录
tests:当前模块的使用帮助文件及示例文件
可以使用puppet module search nginx
去互联网上去搜寻关于ngnix相关的模块。
当然,也可以自己创建模块。
[root@localhost puppet]# mkdir -pv /etc/puppet/modules/nginx/{manifests,files,templates,tests,lib,spec} mkdir: created directory `/etc/puppet/modules/nginx' mkdir: created directory `/etc/puppet/modules/nginx/manifests' mkdir: created directory `/etc/puppet/modules/nginx/files' mkdir: created directory `/etc/puppet/modules/nginx/templates' mkdir: created directory `/etc/puppet/modules/nginx/tests' mkdir: created directory `/etc/puppet/modules/nginx/lib' mkdir: created directory `/etc/puppet/modules/nginx/spec' [root@localhost puppet]# puppet module list /etc/puppet/modules └── nginx (???) /usr/share/puppet/modules (no modules installed)
相关文章推荐
- puppet 运维工具基础<三>
- puppet 运维工具基础<二>
- 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <一>
- 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <一>
- java-基础<一>
- saltstack配置管理工具攻略<一>
- 黑马程序员_Java语法及面向对象基础<一>
- Android Activity学习 <一> Activity基础知识和生命周期
- 【CSS基础】琐碎记录<一>
- Bootstrap<基础一> CSS 概览
- 黑马程序员_java基础<一>_Java开发前奏
- MongoDB<一>基础入门
- Java正则表达式基础<一>
- Linux基础入门<一>
- 黑马程序员_java基础部分<一>
- Android开发工具——ADB(Android Debug Bridge) <一>概览
- 【Linux基础笔记】Linux开机自动启动网卡eth0<一>
- 程序员_Java基础<一>-面向对象上
- 【LaTeX排版】LaTeX使用--入门基础<一>
- 【node.js】基础<一>