【问题标题】:Why Aren't My Chef Normal Attributes Persisted?为什么我的厨师正常属性不保留?
【发布时间】:2015-10-01 17:59:52
【问题描述】:

我需要在节点上运行 chef-client 之间保留一些值。从Chef docs 我认为正常的属性用于此

"在 chef-client 运行开始时,所有默认值、覆盖和 自动属性被重置...普通属性永远不会 重置...在厨师客户端运行结束时,全部默认, 覆盖,自动属性消失,只留下一个 将持续到下一个正常属性的集合 厨师客户端运行。”)

但是,如果我使用以下配方引导节点两次

log 'message' do
  message "Before setting I am #{node['my_key']}"
  level :warn
end

node.normal['my_key'] = 'my value'

log 'message' do
  message "After setting I am #{node['my_key']}"
  level :warn
end

我希望看到

在设定之前我就是我的价值

在第 2 次运行时(因为该值从第 1 次运行中保持不变)。但是它会再次恢复为未设置。

值是否可能持续存在?我需要以不同的方式运行配方吗?还是根本不可能?

编辑:据我所知,厨师运行成功完成。这是命令和输出:

mfreake@my-linux:/export/apps/chef/chef-repo/cookbooks$ knife bootstrap node1.blah.com --ssh-user mfreake --ssh-password 'yaddayadda' --sudo --use-sudo-password  --bootstrap-proxy 'http://proxy.blah.com/' -N node1 --run-list persist_test::read_set_read
Node node1 exists, overwrite it? (Y/N) Y
Client node1 exists, overwrite it? (Y/N) Y
Creating new client for node1
Creating new node for node1
Connecting to node1.marketpipe.com
node1.blah.com [sudo] password for mfreake: -----> Existing Chef installation detected
node1.blah.com Starting first Chef Client run...
node1.blah.com Starting Chef Client, version 12.4.2
node1.blah.com resolving cookbooks for run list: ["persist_test::read_set_read"]
node1.blah.com Synchronizing Cookbooks:
node1.blah.com   - persist_test
node1.blah.com Compiling Cookbooks...
node1.blah.com [2015-09-30T17:45:40+01:00] WARN: Cloning resource attributes for log[message] from prior resource (CHEF-3694)
node1.blah.com [2015-09-30T17:45:40+01:00] WARN: Previous log[message]: /var/chef/cache/cookbooks/persist_test/recipes/read_set_read.rb:2:in `from_file'
node1.blah.com [2015-09-30T17:45:40+01:00] WARN: Current  log[message]: /var/chef/cache/cookbooks/persist_test/recipes/read_set_read.rb:9:in `from_file'
node1.blah.com Converging 2 resources
node1.blah.com Recipe: persist_test::read_set_read
node1.blah.com   * log[message] action write[2015-09-30T17:45:40+01:00] WARN: A message add to the log. 
node1.blah.com 
node1.blah.com   
node1.blah.com   * log[message] action write[2015-09-30T17:45:40+01:00] WARN: A message add to the log. my value
node1.blah.com 
node1.blah.com   
node1.blah.com 
node1.blah.com Running handlers:
node1.blah.com Running handlers complete
node1.blah.com Chef Client finished, 2/2 resources updated in 1.621458795 seconds

【问题讨论】:

  • 1) 你的厨师运行成功了吗?如果不是什么都保存回服务器。 2) 如果您使用-o recipe 启动chef-client,节点对象也不会保存。你真的摆脱这两种情况了吗? (对于你的最后一个问题,是的,这是可能的并且是有意的)
  • 谢谢它似乎成功完成。我在上面添加了命令和输出。输出“节点 node1 存在,覆盖它吗?(是/否)” - 问题?如何不覆盖它?
  • 确实 bootstrap 应该只使用一次,引导(创建节点对象,创建客户端密钥,安装 chef-client 并首先运行)。当你重做它时,它会覆盖节点对象,所以很明显它在运行时是空的。例如通过knife ssh触发运行,一切都会好的。
  • 啊啊啊啊,这解释了很多。多次引导一个节点的想法听起来不正确。我正在使用 Learning Chef 书,也许我只是错过了那一点(尽管我仍然看不到它或在索引中找到它。)。谢谢-如果您想要积分/刻度,请随时将其作为答案
  • 如果 book 以 chef11 为目标是正常的,则在 chef12 中的行为发生了轻微的变化,验证器密钥没有复制到节点以便它自己注册,而是提前创建节点。我会尝试在答案中总结一下

标签: chef-infra knife


【解决方案1】:

好的,有两种情况,节点状态(包括属性)无法保存回主厨服务器:

  1. 当运行错误结束时(这里取决于错误,无论如何都可以保存状态,但这超出了这个问题的范围)
  2. 运行chef-client -o any_recipe_list-o选项在这里临时覆盖运行列表,因此它不会保存回厨师服务器以不覆盖实际运行列表。

node.normalnode.set 是同一个东西,将值写入要存储在服务器上的节点对象中。 Examples in the documentation on the attributes

这里的问题是由于使用了knife bootstrap(根据documentation on the validator less bootstrap,版本> 12.1),它首先在厨师服务器上创建节点及其客户端密钥。调用它两次并允许它覆盖先前的对象重置整个节点对象。

bootstrap 只能使用一次,以后运行的任何 chef-client 都应该由其他方式触发(crontab、knife ssh 等)

【讨论】:

    【解决方案2】:

    您应该尝试node.set['my_key'] = 'my value'(或使用node.set_unless,以便在有人更改您厨师服务器上的属性时安全保存)。

    【讨论】:

    • setnormal 的别名,这并不能解决 OP 问题。
    • @Tensbai 你有那个链接吗?我昨晚实际上搜索了代码库,但找不到 def 。因此我的帖子说“你应该尝试......”而不是“我知道答案是 X”:)
    • 查看上面的属性文档的链接,从代码库中它是别名行here,它是上面正常方法的别名。 node['key'] = value 也可以用于同一目标,并委托给定义为 hereChef::Node::Attribute 类。我个人坚持使用node['key'] 表单来读取属性并使用node.method['key'] 来设置它们(其中方法可以是任何属性优先级)
    • 感谢您的指点。我搜索了def set,但没想到要查找别名! :)
    猜你喜欢
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多