puppet常用资源类型与使用方法

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

通用

1、puppet describe -l 查看puppet支持的资源类型
puppet describe typename 查看某个资源类型的Parameters,Providers--详细
puppet describe -s typename -m 查看某个资源的参数等--简单
2、puppet apply [-d|--debug] [-v|--verbose] [-e|--execute] [--noop] <file>
puppet help apply 查看帮助
puppet apply -v 查看详情
-d 调试模式
-l 保存在指定文件中
--noop 干跑,不执行

一、group:

1、Manage groups
属性:
name:组名;
gid:GID;
system:是否为系统组,true OR false;
ensure:目标状态,present/absent;
members:成员用户;
2、例子:创建组
(1) vim group.pp
group{'mygrp':
ensure => present
}
(2)执行
puppet apply -v --noop group.pp 先看看是否有错
puppet apply -v group.pp 真正执行

二、user

1、Manage users.
属性:
name:用户名;
uid: UID;
gid:基本组ID;
groups:附加组,不能包含基本组;
comment:注释; 
expiry:过期时间 ;
home:家目录;
shell:默认shell类型;
system:是否为系统用户 ;
ensure:present/absent;
password:加密后的密码串;
2、例子:创建用户
(1)用户名为user2,不写name。默认是跟title一直,都是user2
vim user.pp
user{'user2':
ensure => present,
uid => 1021,
groups => 'redhat', 
}
(2)执行
puppet apply -v --noop user.pp
puppet apply -v user.pp

三、package

1、Manage packages.
属性:
ensure:installed, present, latest, absent, any version string (implies present)
name:包名;
source:程序包来源,仅对不会自动下载相关程序包的provider有用,例如rpm或dpkg;
provider:指明安装方式;
2、例子:安装包
安装memcached包
package{'memcached':
ensure => installed,
}

四、service

1、Manage running services.
属性:
ensure:Whether a service should be running. Valid values are stopped (also called false), running (also called true)
enable:Whether a service should be enabled to start at boot. 开机启动Valid values are truefalsemanual.
name:
path:The search path for finding init scripts. Multiple values should be separated by colons or provided as an array. 脚本的搜索路径,默认为/etc/init.d/;
hasrestart:true |false false :init 脚本中的restart命令将不能用,使用stop ,start,默认false
hasstatus:true|false stasus 命令是否使用
start:手动定义启动命令;
stop:
status:
restart:Specify a restart command manually. If left unspecified, the service will be stopped and then started. 通常用于定义reload操作;

2、例子:安装包并启动服务
package{'memcached':
ensure => installed,
}
service{'memcached':
ensure => running,
enable => false,
}

五、file

1、Manages files, including their content, ownership, and permissions. 管理文件,包括它们的内容、所有权和权限。
ensure:Whether the file should exist, and if so what kind of file it should be. Possible values are presentabsentfiledirectory, and link.
file:类型为普通文件,其内容由content属性生成或复制由source属性指向的文件路径来创建;
link:类型为符号链接文件,必须由target属性指明其链接的目标文件;
directory:类型为目录,可通过source指向的路径复制生成,recurse属性指明是否递归复制;
path:文件路径;
source:源文件;
content:文件内容;
target:符号链接的目标文件; 
owner:属主
group:属组
mode:权限;
atime/ctime/mtime:时间戳;
2、例子
(1)例子1:将'/root/manifects/files/redis.conf 的文件复制到/tmp/redis.conf
file{'/tmp/redis.conf':
ensure => file,
source => '/root/manifects/files/redis.conf',
}
(2)例子2:基于内容复制
将hello 
hi 内容给/tmp/test.txt
file{'/tmp/test.txt':
ensure => file,
content => "hello\nhi\n",
}

六、exec

1、Executes external commands. Any command in an exec resource must be able to run multiple times without causing harm --- that is, it must be idempotent.

command (namevar):要运行的命令;
cwd:The directory from which to run the command.
creates:文件路径,仅此路径表示的文件不存在时,command方才执行;
user/group:运行命令的用户身份;
path:The search path used for command execution. Commands must be fully qualified if no path is specified.
onlyif:此属性指定一个命令,此命令正常(退出码为0)运行时,当前command才会运行;
unless:此属性指定一个命令,此命令非正常(退出码为非0)运行时,当前command才会运行;
refresh:重新执行当前command的替代命令;
refreshonly:仅接收到订阅的资源的通知时方才运行;
2、例子
puppet常用资源类型与使用方法
(1)指定包为redis
(2)指定复制的文件,并修改复制后文件的所属人owner,所属组root,权限的0640
(3)除非pidof redis-server 不成功才执行上面的命令,重启服务(以守护进程方式启动)
(4)-> 代表依赖关系,~> 代表触发

七、cron

1、Installs and manages cron jobs. Every cron resource created by Puppet requires a command and at least one periodic attribute (hour, minute, month, monthday, weekday, or special).
command:要执行的任务;
ensure:present/absent;
hour:
minute:
monthday:
month:
weekday:
user:以哪个用户的身份运行命令
target:添加为哪个用户的任务
name:cron job的名称;
2、例子:每3分钟同步一次服务器时间
cron{'timesync':
command => '/usr/sbin/ntpdate 172.16.0.1 &> /dev/null',
ensure => present,
minute => '*/3',
user => 'root',
}

八、notify

1、Sends an arbitrary message to the agent run-time log. 向代理运行时日志发送任意消息。
属性:
message:信息内容
name:信息名称;
核心类型:
group: 组
user:用户
packge:程序包 
service:服务
file:文件
exec:执行自定义命令,要求幂等
cron:周期性任务计划
notify:通知
官方地址:
https://docs.puppet.com/puppet/5.2/cheatsheet_core_types.html#notify/
2、例子:
当redis的配置文件改变了,会重启服务
puppet常用资源类型与使用方法


本文转自 hawapple 51CTO博客,原文链接:http://blog.51cto.com/guanm/2051343


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
安全 Linux 网络协议
puppet yum模块、配置仓储、mount模块
转载:http://blog.51cto.com/ywzhou/1577335 作用:自动为客户端配置YUM源,为使用yum安装软件包提供便捷。 1、服务端配置yum模块 (1)模块清单 [root@puppet ~]# tree /etc/puppe...
1072 0
|
网络协议 安全 网络安全
|
负载均衡 应用服务中间件 网络安全
|
Perl 存储 数据挖掘