2012-08-27 90 views
23

我使用opscode nginx cookbook來配置我的節點上的nginx服務器。 nginx cookbook有一些默認屬性,我想在我的角色(「web_server」)中重寫。廚師:我如何覆蓋角色中的默認屬性?

這些都是我想重寫屬性:

default['nginx']['version'] = "1.2.2" # in cookbooks/nginx/attributes/default.rb 
default['nginx']['source']['prefix'] = "/opt/nginx-#{node['nginx']['version']}" # in cookbooks/nginx/attributes/source.rb 

在我的角色/ web_server.rb文件我有這樣的事情:

name "web_server" 
description "Setup a web server" 
run_list "role[base]", "recipe[nginx]" 
override_attributes 'nginx' => { 
    'install_method' => "source", 
    'version' => "1.2.3", 
    'source' => { "prefix" => "/opt/nginx", "checksum" => nil } 
} 

然而,在運行時chef-客戶端的nginx配方會忽略我的覆蓋並使用默認配置。

我在這裏做錯了什麼?

謝謝!

+0

您是否忘記將角色上傳到廚師服務器(如果您正在使用它)? –

+6

您是否運行帶有或不帶自定義運行列表(-o)的chef-client?今天,我學習了(在調試2-3小時後),在覆蓋運行列表時,不會使用角色/節點中設置的屬性。 –

+2

我遇到了同樣的問題:/我非常確定我在角色中設置了override_attributes,並且我只在我的屬性文件中使用了默認值。我敢打賭,這是一種廚師的錯誤或什麼。 –

回答

1

我可以猜到的唯一問題是這些屬性必須被 force_overridden屬性所覆蓋。 此外,請確保您的運動列表可以覆蓋您的屬性(因爲我對您安排角色文件的方式持懷疑態度)

0

您是否嘗試過使用圓括號?我用圓括號嘗試了你的例子,並得到了默認的屬性重寫。

# your roles/web_server.rb file 

override_attributes(
    'nginx' => { 
    'install_method' => "source", 
    'version' => "1.2.3", 
    'source' => { "prefix" => "/opt/nginx", "checksum" => nil } 
    } 
) 
4

屬性優先圖[1]顯示,這四個選項排名您的角色上面:

12. An override attribute located in an environment 
13. A force_override attribute located in a cookbook attribute file 
14. A force_override attribute located in a recipe 
15. An automatic attribute identified by Ohai at the start of the chef-client run 

如果這些似乎並沒有是原因,那麼也許更改格式可能的幫助。我會寫它想:

override_attributes(
    nginx: { 
    install_method: 'source', 
    version: '1.2.3', 
    source: { 
     prefix: '/opt/nginx', 
     checksum: [ ], 
    }, 
    } 
) 

[1] https://docs.chef.io/attributes.html#attribute-precedence

1

您還可以使用替代屬性的角色編輯器(在網絡或刀的作用編輯)

{ 
    "name": "web_server", 
    "description": "nginx version", 
    "json_class": "Chef::Role", 
    "default_attributes": { 

    }, 
    "override_attributes": { 
    "nginx": { 
     "version": "1.2.2" 
    } 
    }, 
    "chef_type": "role", 
    "run_list": [ 
    "recipe[]", 
    "recipe[]" 
    ], 
    "env_run_lists": { 

    } 
} 
3

按照Chef Attribute Preference文件,這應該工作:

name "web_server" 
description "Setup a web server" 
run_list "role[base]", "recipe[nginx]" 
default_attributes 'nginx' => { 
    'install_method' => "source", 
    'version' => "1.2.3", 
    'source' => { "prefix" => "/opt/nginx", "checksum" => nil } 
} 

你sh不得在角色中使用override_attributes。一旦你開始使用覆蓋而不是默認值,你很快就會發現你已經使用了最強大的覆蓋,並且沒有更多的方法來覆蓋它。改爲使用default_overrides

優先級規則周圍的屬性,只使用default水平實際上是相當一致的:

  1. 如果有一個,從角色的屬性時,例如,require_two_factor_authrole[single_sign_on]被迫真正與default_overrides,即使在QA
  2. 如果有一個,從環境屬性被使用,例如,require_two_factor_auth被迫真正在production
  3. 如果有一個從屬性配方中使用,例如,require_two_factor_auth設置爲true在auth::two_factor
  4. 最後,從默認的屬性文件理智默認屬性是使用,例如,require_two_factor_auth = false

然而,它是個極不尋常不過,在所有這四個地方都要設置相同的屬性。如果屬性的正確值確實取決於配方,角色和環境,那麼通常結果值將結合所有三個屬性,並在每個級別設置不同的屬性並將其組合在配方中。


如果這是行不通的,兩種可能性是:沒有上傳到服務器

  • 重載運行列表與chef-client -o "recipe[nginx]",而不是chef-client -o role[web_server]或純chef-client
    • 編輯角色如果情況並非如此,請提供更多細節。我一直都在使用它,它總是有效的,並且我會擔心是否存在這種情況不如記錄的情況。