厨师仅在第一次运行时运行其他 bash 块之后运行 bash 块



我正在使用厨师来做到这一点:

我试图仅在前一个块运行后运行 bash 块,很简单,我使用 notify。我还希望在初始运行和第二次运行时进行锁定文件检查(有没有更好的方法来确保它仅在以前的 bash 块运行时运行?

这是我目前的厨师代码:

if not File.exist? tsm_login_lock
bash 'login_tsm' do
user tableau_user
cwd tableau_user_home
code <<-EOH
source /etc/profile.d/tableau_server.sh
tsm login -u #{tableau_user} -p #{password}
tsm settings import -f  /home/analytics/setting_file.json
tsm pending-changes apply
tsm licenses activate -k #{key}
tsm register --file #{registration}
tsm pending-changes apply
EOH
notifies :run, "bash[tsm_init]", :immediately
end
file tsm_login_lock do
mode '0644'
content 'tableau server stareted'
end
end
if not File.exist? tsm_init_lock
bash 'tsm_init' do
user tableau_user
cwd tableau_user_home
code <<-EOH
tsm initialize --start-server --request-timeout 1800
EOH
notifies :run, "bash[tsm_2]", :immediately
end
file tsm_init_lock do
mode '0644'
content 'tableau server initialized'
end
end

您需要在此处组合几种方法:

  • 你希望确保收到的资源不会自行运行。因此,他们的操作应设置为:nothing.这样,它们就不会自行运行,然后被通知有条件地再次运行。您定义为订阅一部分的操作是它在收到通知时将执行的操作。
  • 您还希望确保仅当锁定文件锁定的资源实际运行时才会创建锁定文件。因此,它们也应设置为无,并收到:create操作通知。
  • 使用厨师卫士检查锁定文件是否存在。这样,您仍然会看到资源被跳过的特定输出(由于保护(,而不是一起忽略。

使用代码的示例:

使用not_if防护,以便在 tsm_login_lock 变量定义的文件存在时资源不会运行。另外通知要创建的锁定文件。

bash 'login_tsm' do
user tableau_user
cwd tableau_user_home
code <<-EOH
source /etc/profile.d/tableau_server.sh
tsm login -u #{tableau_user} -p #{password}
tsm settings import -f  /home/analytics/setting_file.json
tsm pending-changes apply
tsm licenses activate -k #{key}
tsm register --file #{registration}
tsm pending-changes apply
EOH
notifies :run, "bash[tsm_init]", :immediately
notifies :create, "file[#{tsm_login_lock}]", :immediately
not_if { ::File.exist?(tsm_login_lock) }
end

让此资源不自行执行任何操作,除非它锁定的资源通知

file tsm_login_lock do
mode '0644'
content 'tableau server stareted'
action :nothing
end

同样,此资源应具有 init 锁定文件的not_if保护。此外,它应该具有默认操作 nothing,因为它从登录资源接收通知。最后,通知要创建的锁定文件。

bash 'tsm_init' do
user tableau_user
cwd tableau_user_home
code <<-EOH
tsm initialize --start-server --request-timeout 1800
EOH
action :nothing
not_if { ::File.exist?(tsm_init_lock) }
notifies :run, "bash[tsm_2]", :immediately
notifies :create, "file[#{tsm_init_lock}]", :immediately
end

让这个 init 锁定文件资源自己不做任何事情,只应该由它锁定的资源通知

file tsm_init_lock do
mode '0644'
content 'tableau server initialized'
action :nothing
end

最后,我强烈建议您了解您认为 Tableau 和 init 的成功登录方式。问问自己,如果您登录到服务器,您将如何检查这些内容。将这些验证用于防护而不是锁定文件。通常,您希望在需要确保资源幂等的地方使用防护。查看上面有关防护装置的链接,了解有关防护装置工作原理的完整详细信息。

最新更新