除了 exec 之外,puppet 还有其他方法可以安装 yum 软件包组(例如“开发工具”)吗?
答案1
我今天也遇到了类似的请求,但如果可以通过其他方式解决问题,我不喜欢使用 exec。所以我选择了一条不同的路径,并为“yumgroup”编写了一个简单的自定义类型。只需将这些文件放入模块路径中的任何模块中即可:
“模块名称/lib/puppet/provider/yumgroup/default.rb”
Puppet::Type.type(:yumgroup).provide(:default) do
desc 'Support for managing the yum groups'
commands :yum => '/usr/bin/yum'
# TODO
# find out how yum parses groups and reimplement that in ruby
def self.instances
groups = []
# get list of all groups
yum_content = yum('grouplist').split("\n")
# turn of collecting to avoid lines like 'Loaded plugins'
collect_groups = false
# loop through lines of yum output
yum_content.each do |line|
# if we get to 'Available Groups:' string, break the loop
break if line.chomp =~ /Available Groups:/
# collect groups
if collect_groups and line.chomp !~ /(Installed|Available)/
current_name = line.chomp.sub(/^\s+/,'\1').sub(/ \[.*\]/,'')
groups << new(
:name => current_name,
:ensure => :present
)
end
# turn on collecting when the 'Installed Groups:' is reached
collect_groups = true if line.chomp =~ /Installed Groups:/
end
groups
end
def self.prefetch(resources)
instances.each do |prov|
if resource = resources[prov.name]
resource.provider = prov
end
end
end
def create
yum('-y', 'groupinstall', @resource[:name])
@property_hash[:ensure] == :present
end
def destroy
yum('-y', 'groupremove', @resource[:name])
@property_hash[:ensure] == :absent
end
def exists?
@property_hash[:ensure] == :absent
end
end
“模块名称/lib/puppet/类型/yumgroup.rb”
Puppet::Type.newtype(:yumgroup) do
@doc = "Manage Yum groups
A typical rule will look like this:
yumgroup { 'Development tools':
ensure => present,
}
"
ensurable
newparam(:name) do
isnamevar
desc 'The name of the group'
end
end
然后,运行启用 pluginsync 的 puppet agent,您就可以使用如下自定义类型:
yumgroup {'Base': ensure => present, }
或者:
yumgroup {'Development tools': ensure => absent, }
您可以通过运行以下命令查看安装了哪些组:
puppet resource yumgroup
享受!
答案2
这里是 'yumgroup' puppet 资源类型的定义。它默认安装默认和强制包,并可以安装可选包。
虽然这个定义很容易实现,但目前还不能删除 yum 组。我自己没有这么做,因为在某些情况下,它会导致 puppet 出现循环。
这种类型需要安装 yum-downloadonly rpm,我认为它只适用于 RHEL/CentOS/SL 6。在我写这篇文章的时候,以前版本的 yum 的退出状态是错误的,因此如果不扩展到 grep 进行输出,“unless”参数将无法工作。
define yumgroup($ensure = "present", $optional = false) {
case $ensure {
present,installed: {
$pkg_types_arg = $optional ? {
true => "--setopt=group_package_types=optional,default,mandatory",
default => ""
}
exec { "Installing $name yum group":
command => "yum -y groupinstall $pkg_types_arg $name",
unless => "yum -y groupinstall $pkg_types_arg $name --downloadonly",
timeout => 600,
}
}
}
}
我故意省略了将 yum-downloadonly 设为依赖项,因为它可能与其他人的清单冲突。如果您想这样做,请在单独的清单中声明 yum-downloadonly 包并将其包含在此定义中。不要直接在此定义中声明,否则如果您多次使用此资源类型,puppet 将给出错误。然后,exec 资源应该需要 Package['yum-downloadonly']。
答案3
我在傀儡类型参考对于包类型,因此我在 Freenode 上的 Puppet IRC 频道(奇怪的是#puppet)上询问,但没有得到任何答案,所以我认为答案是“还没有”。
答案4
我喜欢使用自定义资源的解决方案,但它不是幂等的。我对exists?函数的看法:
Puppet::Type.type(:yumgroup).provide(:default) do
desc 'Support for managing the yum groups'
commands :yum => '/usr/bin/yum'
# TODO
# find out how yum parses groups and reimplement that in ruby
def self.instances
groups = []
# get list of all groups
yum_content = yum('grouplist')
# turn of collecting to avoid lines like 'Loaded plugins'
collect_groups = false
# loop through lines of yum output
yum_content.each do |line|
# if we get to 'Available Groups:' string, break the loop
break if line.chomp =~ /Available Groups:/
# collect groups
if collect_groups and line.chomp !~ /(Installed|Available)/
current_name = line.chomp.sub(/^\s+/,'\1').sub(/ \[.*\]/,'')
groups << new(
:name => current_name,
:ensure => :present
)
end
# turn on collecting when the 'Installed Groups:' is reached
collect_groups = true if line.chomp =~ /Installed Groups:/
end
groups
end
def self.prefetch(resources)
instances.each do |prov|
if resource = resources[prov.name]
resource.provider = prov
end
end
end
def create
yum('-y', 'groupinstall', @resource[:name])
@property_hash[:ensure] == :present
end
def destroy
yum('-y', 'groupremove', @resource[:name])
@property_hash[:ensure] == :absent
end
def exists?
cmd = "/usr/bin/yum grouplist hidden \"" + @resource[:name] + "\" | /bin/grep \"^Installed\" > /dev/null"
system(cmd)
$?.success?
end
end