Run `apt-get update` before installing other packages using Puppet

I am trying to create a puppet module that automates the installation of the CE zend server, this is not important here, but the steps are as follows:

  • update / etc / apt / source.list
  • download repos key via wget
  • do apt-get update
  • do apt-get install zend-server-ce-5.2

I have an init.pp file

 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" } } 

It seems that the puppet runs the commands in a different order, then I need to. Is there any way how to make it work in my desired order?

The conclusion of such a fragment

  [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 

So he says: Could not find package zend-server-ce-php-5.2

Can someone explain to me what is wrong?

+49
apt-get puppet zend-server
Jun 01 '12 at 7:05
source share
8 answers

You need to specify the dependencies. The simplest / cleanest approach is to use the require parameter, which is available for all types of resources.

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

etc..

+50
Jun 01 2018-12-12T00:
source share

Since Puppet 2.6.0 a new feature "relationship syntax" has been introduced.

An example in Puppet 2.6.0 and later would look like this:

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

Each time a package command is executed, the dependency (in our case, "apt-update") is launched with a fist. You can even define longer chains.

+100
Dec 01
source share

I tried the previous option, but it does not work for me on Ubuntu 10.04

Finally, I prepared the following script that starts the update every time the repository is older than one week:

 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 ))'" } 

Hope this helps.

+13
Feb 07 '13 at 14:59
source share

I prefer to install apt-upgrade on a separate stage, running before the main stage, so I do not need to hard-code any dependencies. Check here: http://docs.puppetlabs.com/puppet/2.7/reference/lang_run_stages.html .

A simple example will look below. This means that you have a separate class to do the actual apt-update:

 stage { "init": before => Stage["main"] } class {"apt-update": stage => init, apt_mirror => $apt_mirror } 

Check out my sample LAMPP-box on github to see how the fragments fit together: https://github.com/joerx/vagrant-lampp

Note. Be careful with apt-upgrade as some base fields break in things like kernel updates.

+12
Sep 03 '13 at 3:34
source share

In Puppet 3, this can be done by implementing virtual resources using resource collectors.

 # 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
May 19 '13 at 23:00
source share

Adding this voodoo snippet worked for us:

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

This caused an update. YMMV.

+3
Jul 16 '14 at 16:23
source share

A package that needs to update APT lists must require Class['apt::update']

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

If you are using a custom APT source, just make sure the correct order is:

 Apt::Source['my_source'] -> Class['apt::update'] 
0
Apr 29 '17 at 23:49 on
source share

You really have to use the apt module to create sources and add keys: https://forge.puppet.com/puppetlabs/apt

If you are using 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' 
0
May 03 '19 at 15:51
source share



All Articles