厨师守卫 only_if 与 '&&'不遵守这两项声明

发布于 2025-01-09 07:32:20 字数 2017 浏览 2 评论 0原文

我在厨师食谱中有以下哈希,它创建了一个目录,

node['fnb_base_directory']['directory_name'].map do |directory_name, dir|
  next if directory_name.empty?
  directory directory_name do
    owner     dir['owner']
    group     dir['group']
    mode      dir['mode']
    recursive dir['recursive']
    action    dir['action']
    only_if "getent passwd #{dir['owner']}" && "getent group #{dir['group']}"
  end
end

我只希望厨师尝试基于此守卫创建目录:

only_if "getent passwd #{dir['owner']}" && ; "getent group #{dir['group']}"

因此,这基本上意味着在尝试创建目录之前,用户 都必须存在。

问题似乎是,当厨师解释这一点时,我发现它仅遵循其中的一个语句,即仅检查group是否存在,然后继续尝试创建目录,但会失败,因为用户尚不存在。 请参阅下面的解释:

directory("/opt/test_dir_creation") do
  action [:create]
  default_guard_interpreter :default
  declared_type :directory
  cookbook_name "fnb_base_wrapper"
  recipe_name "fnb_base_directory"
  recursive false
  owner "nonexistent_user"
  group "opc"
  mode "0755"
  only_if "getent group opc"
end

失败:

directory[/opt/test_dir_creation] action create
* cannot determine user id for 'nonexistent_user', does the user exist on this system?
================================================================================
Error executing action `create` on resource 'directory[/opt/test_dir_creation]'
================================================================================

Chef::Exceptions::UserIDNotFound
--------------------------------
cannot determine user id for 'nonexistent_user', does the user exist on this system?

用户尚不存在的原因是因为该用户是在另一个食谱中创建的,其优先级不如我们的基本厨师(创建目录)高,因此为什么目录创建是在用户之前完成的被创建。

然后,该目录将在第二次聚合时创建,用户将存在于该目录中,然后仅继续创建该目录。

供参考。 我使用 getent 是因为我们在服务器上使用 AD,因此它可能并不总是静态用户/组,而是驻留在 AD 中的用户/组。

我也检查过这个问题: 在 Chef only_if 防护中使用多个条件

这对我没有帮助。

我们将非常感谢您的帮助、指导和建议。

I have the below hash in a chef recipe, that creates a directory/s

node['fnb_base_directory']['directory_name'].map do |directory_name, dir|
  next if directory_name.empty?
  directory directory_name do
    owner     dir['owner']
    group     dir['group']
    mode      dir['mode']
    recursive dir['recursive']
    action    dir['action']
    only_if "getent passwd #{dir['owner']}" && "getent group #{dir['group']}"
  end
end

I ONLY want chef to try create the directory based on this guard:

only_if "getent passwd #{dir['owner']}" && "getent group #{dir['group']}"

So that basically means that BOTH the user and the group must exist before trying to create the directory.

The problem appears to be that when chef interprets this, I see it is only adhering to ONE of the statements i.e. ONLY checks that group exists and then proceeds to attempt to create the directory, but will fail because the user does not exist yet.
See below interpretation:

directory("/opt/test_dir_creation") do
  action [:create]
  default_guard_interpreter :default
  declared_type :directory
  cookbook_name "fnb_base_wrapper"
  recipe_name "fnb_base_directory"
  recursive false
  owner "nonexistent_user"
  group "opc"
  mode "0755"
  only_if "getent group opc"
end

Failure:

directory[/opt/test_dir_creation] action create
* cannot determine user id for 'nonexistent_user', does the user exist on this system?
================================================================================
Error executing action `create` on resource 'directory[/opt/test_dir_creation]'
================================================================================

Chef::Exceptions::UserIDNotFound
--------------------------------
cannot determine user id for 'nonexistent_user', does the user exist on this system?

The reason the user does not exist yet, is because that user is created in another cookbook whose priority is not as high as our base cook (which creates directories), hence why the directory creation is done before the user is created.

The directory will then be created on the 2nd converge, where the user will then exist, and proceed to create the directory ONLY then.

FYI.
I am using getent because we use AD on our servers, so it may not always be a static user/group, but one that resides in AD.

I have also checked this question:
Using multiple conditions in Chef only_if guard

It does not help me.

Your help, guidance and advice will be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2025-01-16 07:32:20

尝试删除中间的引号,以便整个表达式包括 &&条件在 shell 中运行。

only_if "getent passwd #{dir['owner']} && getent group #{dir['group']}"

Chef 接受字符串 shell 命令或 ruby​​ 块

only_if “管道中可能存在的某些 shell 命令返回 0”

only_if {
  return true if <condition1> && <condition2>
  return true if <condition3> || <condition4>
  return false
}

Try removing the quotes in the middle so the whole expression including the && condition runs in the shell.

only_if "getent passwd #{dir['owner']} && getent group #{dir['group']}"

Chef accepts a string shell command or a ruby block

only_if "some shell commands which are possibly in a pipeline return 0"

only_if {
  return true if <condition1> && <condition2>
  return true if <condition3> || <condition4>
  return false
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文