在使用 Puppet 安装其他软件包之前运行 `apt-get update`

问题描述 投票:0回答:8

我正在尝试创建自动安装zend server CE的puppet模块,这在这里并不重要,但步骤如下

  1. 更新/etc/apt/source.list
  2. 通过 wget 下载存储库密钥
  3. 执行 apt-get 更新
  4. 执行 apt-get 安装 zend-server-ce-5.2

我有

init.pp
文件

class zendserverce {

# https://github.com/puppetlabs/puppetlabs-stdlib
file_line { 'debian_package':
    path => '/etc/apt/sources.list',
    line => 'deb http://repos.zend.com/zend-server/deb server non-free'
}

exec { "wget http://repos.zend.com/zend.key -O- |apt-key add -":
    path => ["/usr/bin", "/usr/sbin"]
}

exec { "apt-get update":
    command => "/usr/bin/apt-get update",
    onlyif  => "/bin/sh -c '[ ! -f /var/cache/apt/pkgcache.bin ] || /usr/bin/find /etc/apt/* -cnewer /var/cache/apt/pkgcache.bin | /bin/grep . > /dev/null'",
}

package { "zend-server-ce-php-5.2":
    ensure => "latest"
}

}

木偶似乎以与我需要的不同顺序运行命令。有什么办法告诉他按照我想要的顺序运行吗?

该代码片段的输出是

  [0;36mnotice: /Stage[main]/Mc/Package[mc]/ensure: ensure changed 'purged' to 'latest'[0m
  [1;35merr: /Stage[main]/Zendserverce/Package[zend-server-ce-php-5.2]/ensure: change from purged to latest failed: Could not update: Execution of '/usr/bin/apt-get -q -y -o DPkg::Options::=--force-confold install zend-server-ce-php-5.2' returned 100: Reading package lists...
  Building dependency tree...
  Reading state information...
  E: Couldn't find package zend-server-ce-php-5.2 at /tmp/vagrant-puppet/modules 0/zendserverce/manifests/init.pp:28[0m
  [0;36mnotice: /Stage[main]/Zendserverce/Exec[wget http://repos.zend.com/zend.key -O- |apt-key add -]/returns: executed successfully[0m
  [0;36mnotice: /Stage[main]/Zendserverce/File_line[debian_package]/ensure: created[0m
  [0;36mnotice: Finished catalog run in 6.75 seconds[0m

所以它说:找不到包 zend-server-ce-php-5.2

任何人都可以指导我出了什么问题吗?

puppet apt-get zend-server
8个回答
99
投票

自 Puppet 2.6.0 起,引入了新功能“关系语法”。

Puppet 2.6.0 及更高版本中的示例如下所示:

exec { "apt-update": command => "/usr/bin/apt-get update" } Exec["apt-update"] -> Package <| |>

每次执行打包命令时,都会首先触发依赖项(在我们的例子中为“apt-update”)。 您甚至可以定义更长的链。


52
投票
需要指定依赖关系。最简单/最干净的方法是使用可用于所有资源类型的 require 参数。

package { "zend-server-ce-php-5.2": ensure => latest, require => Exec['apt-get update'], }

等等..


14
投票
我尝试了以前的变体,但它在 Ubuntu 10.04 上对我不起作用

最后我准备了以下脚本,每次存储库超过一周时都会运行更新:

exec { 'apt-get update': command => "/usr/bin/apt-get update", onlyif => "/bin/bash -c 'exit $(( $(( $(date +%s) - $(stat -c %Y /var/lib/apt/lists/$( ls /var/lib/apt/lists/ -tr1|tail -1 )) )) <= 604800 ))'" }

希望有帮助。


13
投票
我更喜欢将 apt-upgrade 放入在主阶段之前运行的单独阶段,因此我不必硬连接任何依赖项。检查这里:

https://www.puppet.com/docs/puppet/8/lang_run_stages.html

一个简单的例子如下所示。这意味着您有一个单独的类来执行实际的 apt-update:

stage { "init": before => Stage["main"] } class {"apt-update": stage => init, apt_mirror => $apt_mirror }
在 github 上检查我的示例 LAMPP-box,看看各个部分如何组合在一起:

https://github.com/joerx/vagrant-lampp

注意:小心 apt-upgrade,因为某些基础盒子会因内核升级等问题而损坏。


4
投票
在 Puppet 3 中,这可以通过

使用资源收集器实现虚拟资源来完成

# so you don't have to fully qualify paths to binaries Exec { path => ['/usr/bin'] } # virtual resource @exec { 'sudo apt-get update': tag => foo_update } # realize resource. filter by arbitrary "foo_update" # tag and relate it to all Package resources Exec <| tag == foo_update |> -> Package <| |>
    

4
投票
添加这段巫术片段对我们有用:

Apt::Pin <| |> -> Package <| |> Apt::Source <| |> -> Package <| |>

这强制更新。 YMMV.


2
投票
需要更新 APT 列表的软件包应该需要

Class['apt::update']



package { "zend-server-ce-php-5.2": ensure => "latest", require => Class['apt::update'] }

如果您使用自定义 APT 源,只需确保顺序正确即可:

Apt::Source['my_source'] -> Class['apt::update']
    

0
投票
您确实应该使用 apt 模块来创建源并添加密钥:

https://forge.puppet.com/puppetlabs/apt

如果您使用 hiera:

apt::sources: 'artifactory-pro-debs': location: 'http://repos.zend.com/zend-server/deb' release: 'server repos: 'non-free' key: source: 'http://repos.zend.com/zend.key'
    
© www.soinside.com 2019 - 2024. All rights reserved.