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

Jar*_*aro 50 puppet apt-get zend-server

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

  1. 更新/etc/apt/source.list
  2. 通过wget下载repos密钥
  3. 做apt-get update
  4. do apt-get install 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"
}

}
Run Code Online (Sandbox Code Playgroud)

似乎puppet以不同的顺序运行命令然后我需要.有什么方法告诉他按照我想要的顺序跑吗?

这样的片段的输出是

  [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
Run Code Online (Sandbox Code Playgroud)

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

谁能指导我出错了什么?

DrD*_*Dol 101

从Puppet 2.6.0开始,引入了一个新功能"关系语法" .

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

exec { "apt-update":
    command => "/usr/bin/apt-get update"
}

Exec["apt-update"] -> Package <| |>
Run Code Online (Sandbox Code Playgroud)

每次执行包命令时,将依次触发依赖项(在我们的例子中为"apt-update").您甚至可以定义更长的链.

  • 谢谢,这非常有帮助!对于像我这样的其他新手想知道最后一行:"<|" 是收集符号,请参阅http://docs.puppetlabs.com/puppet/3/reference/lang_collectors.html (12认同)
  • 无论是否需要安装包,这似乎都会在每个木偶更新上运行.有没有办法收紧这个,以便apt-get更新只在系统中缺少包时运行? (2认同)

cze*_*vik 50

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

package { "zend-server-ce-php-5.2":
  ensure  => latest,
  require  => Exec['apt-get update'],
}
Run Code Online (Sandbox Code Playgroud)

等等..

  • 我阅读了http://docs.puppetlabs.com/guides/language_guide.html#run-stages,puppet提供了阶段,因此您可以明确指定执行顺序.在Puppet 2.6.0版中添加了运行阶段,您现在可以指定任意数量的阶段,这些阶段提供另一种方法来控制puppet中资源管理的顺序. (5认同)
  • @BrandonCook然后你可以给你的exec一个新名字.`exec {update:command =>"apt-get update"}`然后将其引用为`Exec [update]`. (3认同)

小智 13

我尝试过以前的变种,但它在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 ))'"
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.


joe*_*erx 12

我更喜欢将apt-upgrade放到在主阶段之前运行的单独阶段,因此我不必硬连接任何依赖项.请点击此处:http://docs.puppetlabs.com/puppet/2.7/reference/lang_run_stages.html.

一个简单的例子如下所示.它意味着你有一个单独的类来进行实际的apt-update:

stage { "init": before  => Stage["main"] }

class {"apt-update": 
  stage => init, 
  apt_mirror => $apt_mirror 
}
Run Code Online (Sandbox Code Playgroud)

检查我在github上的样本LAMPP-box,看看它们是如何组合在一起的:https://github.com/joerx/vagrant-lampp

注意:小心使用apt-upgrade,因为一些基本框会破坏内核升级之类的东西.