如何安装带有puppet的yum软件包组?


Answers:


11

我今天遇到了类似的要求,但是如果事情可以通过其他任何方式解决,我都不喜欢执行人员。因此,我选择了另一条路径,并为“ yumgroup”编写了一个简单的自定义类型。只需将这些文件放在模块路径中的任何模块中即可:

“模块名称/ lib /人偶/提供者/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 /人偶/类型/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

请享用!


我认为这yum_content = yum('grouplist')需要一个,.split("\n")以便.each不会导致错误。
alex.pilon

@ alex.pilon感谢您的提示。尽管在v3中它像这样工作。
Jakov Sosic '17

4

这是“ yumgroup” p资源类型的定义。默认情况下,它将安装默认软件包和强制软件包,并可以安装可选软件包。

尽管很容易实现,但此定义尚不能删除yum组。我并不为自己烦恼,因为在某些情况下它可能导致木偶循环。

该类型需要安装yum-downloadonly rpm,我认为它只能在RHEL / CentOS / SL 6上运行。在我撰写本文时,yum在以前版本中的退出状态是错误的,因此“除非”参数不起作用而不扩展到grep输出。

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软件包,并将其包含在此定义中。不要直接在此定义中声明,否则,如果您多次使用此资源类型,则人偶将给出错误消息。然后,exec资源应要求Package ['yum-downloadonly']。


谢谢你!我创建了一个名为yum_groupinstalls的模块,并使用您的定义和一个用于安装开发工具组的类创建了一个init.pp清单。注意,我必须引用组名: class yum_groupinstalls { yumgroup { '"Development tools"': } } 在定义中,我必须指定yum的完整路径,在CentOS 6.2上对我来说是/ usr / bin / yum。
Banjer 2012年

3

我在Puppet类型参考中找不到Package类型的任何内容,所以我在Freenode的Puppet IRC频道上问(奇怪的是#puppet),却一无所获,所以我认为答案是“还没有”。


3

您可以通过Puppet Exec Type来执行此操作以执行必要的组安装。我肯定会包括一个商品onlyifunless选项,以便它仅在需要时执行,或设置为refreshonly并通过触发它,Notify以便它不会每次都运行。该Exec类型将在您触发的情况下在p客户端上本地执行命令。


1

我喜欢使用自定义资源的解决方案,但是它不是幂等的。我认为存在吗?功能:

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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.