繁体   English   中英

如何有条件地在厨师中运行资源块或食谱?

[英]How can I conditionally run a block of resources or a recipe in chef?

我使用以下配方创建一些用户,将它们添加到组中,并将密码设置为在首次登录时过期。

search(:users, '*:*').each do |user|
  userPassword = "$1$scmdevop$ZDTyqia9RXSrpHGK75FjN/"
  user user['id'] do
    comment user['comment']
    home user['home']
    shell user['shell']
    manage_home true
    password "#{userPassword}"
  end

  if user['sudo'] then
    group "#{node.default["sudogroup"]}" do
      action :modify
      members user['id']
      append true
    end
  end
  if (user['resetPassword'] == nil) || (user['resetPassword']) then
    bash 'setExporation' do
      code 'chage -d 0 ' + user['id']
      user 'root'
    end
  end
end

问题在于,它将以这种方式继续重置密码并在每次运行时设置期望值,因此我试图找到如何有条件地进行设置。 我想使用以下命令来检查用户是否存在

grep -qs #{user["id"]} /etc/passwd

问题是我只能在第一个资源中使用not_if子句,因为在此之后,已经清楚地创建了用户。 有没有办法让三个资源的整个块以shell退出代码为条件?

谢谢,米歇尔。

您可能想要的是来自用户资源的通知,但这可能有点困难,因为这将触发任何更改,而不仅仅是创建。 这里的根本问题是,您所陈述的期望行为是用过程术语而不是收敛状态表达的。 最好的方法可能是构建一个自定义资源来隐藏某些逻辑,但是从本质上讲,您想要的是一个if语句,就像您已经拥有的一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM