简体   繁体   English

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

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

I've the following recipe used to create some users, add them to a group and set the password to expire at the first login. 我使用以下配方创建一些用户,将它们添加到组中,并将密码设置为在首次登录时过期。

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

The problem is that in this way it will continue to reset the password and set the espiration at every run so I was trying to find how to make it conditionally. 问题在于,它将以这种方式继续重置密码并在每次运行时设置期望值,因此我试图找到如何有条件地进行设置。 I would like to use the following command to check if the user exist 我想使用以下命令来检查用户是否存在

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

The problem is that I can use the not_if clause only in the first resource because after that the user has been clearly created. 问题是我只能在第一个资源中使用not_if子句,因为在此之后,已经清楚地创建了用户。 Is there a way to get the entire block of three resources being conditional to a shell exit code? 有没有办法让三个资源的整个块以shell退出代码为条件?

Thanks, Michele. 谢谢,米歇尔。

What you probably want is a notification from the user resource, but this might be a little hard because that would trigger on any change, not just creation. 您可能想要的是来自用户资源的通知,但这可能有点困难,因为这将触发任何更改,而不仅仅是创建。 The underlying problem here is that the desired behavior you stated is expressed in procedural terms, not in terms of convergent state. 这里的根本问题是,您所陈述的期望行为是用过程术语而不是收敛状态表达的。 Best approach is probably to build a custom resource to hide some of this logic, but at heart what you want is an if statement like you already have. 最好的方法可能是构建一个自定义资源来隐藏某些逻辑,但是从本质上讲,您想要的是一个if语句,就像您已经拥有的一样。

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

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