|
一、简介1、puppet是一个IT基础设施自动化管理工具,puppet是一种Linux、Unix、windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件、用户、cron任务、软件包、系统服务等。puppet把这些系统实体称之为资源,puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系。基于puppet,实现自动化重复工作,快速部署关键性应用及本地或云端管理更改。基于ruby开发对于管理员是抽象的,依赖于ruby和facter。管理内容丰富,包括file、user、group、host、package、service、cron、exec、yumrepo等。使用模型:单机环境创建.pp文件申报资源归为类目录:模块分布式使用,master/agent2、puppet基本执行流程puppet modules:puppet 模块,功能类似于变量,可以被puppet多次调用,这个也是puppet的核心设置,模块中保存的是“类”,而“类”中定义的是资源。目前可以通过网站下载相关的功能模块,减少了开发难度。(https://forge.puppet.com),puppet模块位于master端(服务器端),master端同样还定义了node,就是被管理的客户端(agent)节点,node里面定义了适用的class,而不是模块,这里要注意。而agent(客户端)则通过自身的facter程序反馈给master自己的信息(客户端的ip、系统等相关信息),master收到agent的facter信息后会将适用于它 的node配置转成catalog信息,并推送到agent端。由于puppet的master和agent是基于“https”协议进行通信的,所以在大企业中也往往在master前端布置一个nginx或者haproxy进行反向代理,实现负载均衡功能。版本控制工具cvs—》svn———》gitpuppet的文件1、资源清单文件,.pp后缀,用于定义资源所在的位置的文件。二、安装2.6属于废弃的版本,应该使用2.7或者3.3。我这里是centos6.7,使用的是2.7
1、安装更新puppet的仓库
[root@localhost yum.repos.d]# rpm -Uvh --force https://yum.puppetlabs.com/puppetlabs-release-pc1-el-6.noarch.rpm
Retrieving https://yum.puppetlabs.com/puppetlabs-release-pc1-el-6.noarch.rpm
warning: /var/tmp/rpm-tmp.laVV75: Header V4 RSA/SHA1 Signature, key> Preparing... ########################################### [100%]
1:puppetlabs-release-pc1 ########################################### [100%]
2、安装puppetserver
[root@localhost yum.repos.d]# yum install puppetserver
三、配置文件1、帮助信息 [root@localhost ~]# puppet help
Usage: puppet <subcommand> [options] <action> [options]
2、获取特定选项的帮助信息 [root@localhost ~]# puppet help apply
USAGE
-----
puppet apply [-h|--help] [-V|--version] [-d|--debug] [-v|--verbose]
[-e|--execute] [--detailed-exitcodes] [-L|--loadclasses]
[-l|--logdest syslog|eventlog|<FILE>|console] [--noop]
[--catalog <catalog>] [--write-catalog-summary] <file>
四、基本用法显示资源类型:常见的group 、user、pickage、notify、mount、cron、exec、file、yumrepo、service、user。
[root@localhost ~]# puppet describe --list
These are the types known to puppet:
augeas - Apply a change or an array of changes to the ...
computer - Computer object management using DirectorySer ...
cron - Installs and manages cron jobs
exec - Executes external commands
file - Manages files, including their content, owner ...
filebucket - A repository for storing and retrieving file ...
group - Manage groups
puppet基本的数据类型:字符型:直接字符串,需要引号数组:[‘elemenet1’,’element2’]布尔型:不能加引号,true、false。undef:hash:(应用不多)正则表达式:(?<标志位>:<正则表达式>)或者(?-<标志位>:<正则表达式>),常常应用于case和selector场景中。标志位:!:不区分大小写m:把”.”当做换行符使用x:忽略模式中的空白字符和注释,如果不使用其中的某个标志位可以使用“-”去除。样例理解:通过facter来收集系统信息,并判断是什么系统,最终输出信息。
[root@localhost ~]# vim test.pp
$package = $operatingsystem ? {
/(?i-mx:^(Centos|fedar|redhat))/ => "httpd",
/(?i-mx:^(debian|ubuntu))/ => "apache2",
}
notify {"notify":
message => "install $package",
}
~
[root@localhost ~]# vim test.pp
[root@localhost ~]# puppet apply test.pp
Notice: Compiled catalog for localhost.localdomain in environment production in 0.10 seconds
Notice: install httpd
Notice: /Stage[main]/Main/Notify[notify]/message: defined 'message' as 'install httpd'
Notice: Applied catalog in 0.04 seconds
常见操作符:https://www.iyunv.com/source/plugin/onexin_bigdata/
if语句判断依据是条件表达式结果的布尔类型,如果为true就执行,如果为false就执行另一个命令。
实例:
[root@localhost ~]# vim notify.pp
$old = 21
if $old > 30 {
notice ("old man")
} else {
notice ("young man")
}
$eld = 34
if $eld > 30 {
notify {"old man":}
} else {
notify {"young man":}
}
[root@localhost ~]# vim notify.pp
[root@localhost ~]# puppet apply notify.pp
Notice: Scope(Class[main]): young man
Notice: Compiled catalog for localhost.localdomain in environment production in 0.11 seconds
Notice: old man
Notice: /Stage[main]/Main/Notify[old man]/message: defined 'message' as 'old man'
Notice: Applied catalog in 0.04 seconds
实例2:通过正则表达式判断系统,然后安装web服务程序。 [root@node7.dtedu.com ~]# vim yumwebapp.pp
if $operatingsystem =~ /(?!-mx:^(centos|fedra|redhat))/ {
$webserver = "httpd"
} elsif $operatingsystem =~ /(?!-mx:^(debian|ubuntu))/ {
$webserver = "apache2"
} else {
$webserver = undef
notice ("unknow os")
}
package {"$webserver":
ensure => installed,
}
[root@node7.dtedu.com ~]# puppet apply yumwebapp.pp
Notice: Compiled catalog for node7.dtedu.com in environment production in 0.78 seconds
Notice: /Stage[main]/Main/Package[httpd]/ensure: created
Notice: Applied catalog in 3.04 seconds
case判断语句基本使用方法:
[root@node7.dtedu.com ~]# vim case.pp
case $operatingsystem {
"centos": {notice ("welcome to centos system")}
'redhat','fedora': {notice ("welcome to redhat famly")}
/^(centos|ubuntu)$/: {notice ("welcom to debian")}
default: {notice ("i do not konw you!")}
}
selector只用用于出现直接值的地方,就是一个条件判断并赋值的功能。表达式格式:https://www.iyunv.com/source/plugin/onexin_bigdata/使用要点: |
|
|