在Ubuntu 11.04上安装Nginx 1.0.5时如何使用Puppet依赖项

时间:2011-08-26 19:13:52

标签: ubuntu nginx ubuntu-11.04 puppet

我是Puppet的新手,对使用依赖项有疑问。

我正在使用Puppet在Ubuntu 11.04上安装Nginx 1.0.5。它需要添加一个新的apt存储库,因为natty通常带有Nginx 0.8。在命令行,安装如下:

# apt-get install python-software-properties
# add-apt-repository ppa:nginx/stable
# apt-get update
# apt-get install nginx

所以我写了这个Puppet脚本:

class nginx::install {
  package { "nginx":
    ensure => present,
    require => Exec["nginx_repository"],
  }

  exec { "add-apt-repository ppa:nginx/stable && apt-get update":
    alias => "nginx_repository",
    require => Package["python-software-properties"],
  }

  package { "python-software-properties":
    ensure => installed,
  }
}

该脚本有效,但exec {}指令每次都运行,而不是仅在实际安装nginx时运行。理想情况下,我希望只在实际安装nginx之前运行“apt”命令,而不是在简单地检查nginx安装时。

我对通知/订阅模型有基本的了解,但我不确定如何让nginx指令仅在实际安装nginx时发送“通知”信号。

2 个答案:

答案 0 :(得分:14)

以下是解决此问题的两种方法:

1)

exec { "add-apt-repository ppa:nginx/stable && apt-get update":
    alias => "nginx_repository",
    require => Package["python-software-properties"],
    creates => "/etc/apt/sources.list.d/nginx-stable-natty.list",
}

这将告诉exec仅在该文件不存在时才运行。如果还有其他方法可以检查exec是否已成功运行,则可以使用onlyif =>unless =>来指定要检查的命令。

2)

  exec { "add-apt-repository ppa:nginx/stable && apt-get update":
    alias => "nginx_repository",
    require => Package["python-software-properties"],
    refreshonly => true,
    subscribe => Package["python-software-properties"],
  }

这将告诉exec仅在通知的情况下运行,并告诉该程序包通知exec它应该运行。 (您可以在python-software-properties包节中指定notify => Exec["nginx_repository"];关系一端的通知效果与关系另一端的订阅相同。)

第二种方法的缺点是,如果出现任何问题,木偶将永远不会弄明白,如果包装是通过其他方式安装的,而不是通过傀儡规则(例如作为其他地方的依赖),它永远不会运行exec(并且nginx包安装将继续失败)。

换句话说,让exec有一些检查它是否已经运行的方法是非常可取的。

答案 1 :(得分:1)

您可以使用Facter变量lsbdistcodename来确保版本独立性,如以下对freiheit代码中creates属性的修改:

exec { "add-apt-repository ppa:nginx/stable && apt-get update":
  alias => "nginx_repository",
  require => Package["python-software-properties"],
  creates => "/etc/apt/sources.list.d/nginx-stable-${lsbdistcodename}.list",
}

对于Ubuntu 12.04 Lucid,这扩展为:

creates => "/etc/apt/sources.list.d/nginx-stable-lucid.list",