首页 文章

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

提问于
浏览
49

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

  • update /etc/apt/source.list

  • 通过wget下载repos密钥

  • 做apt-get update

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

}

似乎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

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

谁能指导我出错了什么?

7 回答

  • 100

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

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

    等等..

  • 0

    自从Puppet 2.6.0以来,一个新功能"relationship syntax"被introduced .

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

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

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

  • 4

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

    希望能帮助到你 .

  • 3

    我更喜欢将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 
    }
    

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

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

  • 13

    在Puppet 3中,这可以通过realizing 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 <| |>
    
  • 49

    添加这个伏都教片段对我们有用:

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

    这迫使了更新 . 因人而异 .

  • 12

    需要更新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']
    

相关问题