简体   繁体   English

在CHEF配方中使用数据袋

[英]Using databag in CHEF recipe

I have been struggling for over a week to solve this issue. 我已经为解决这个问题奋斗了一个多星期。 Is there anyone who can help me out solving this. 有谁可以帮助我解决这个问题。

I have a databag mongo, the contents of the databag is: 我有一个数据袋mongo,该数据袋的内容是:

{ "firstuser": {
"id": "firstuser",
"password": "123",
"db": "mydb",
"role": "readWrite"
},
"seconduser": {
"id": "seconduser",
"password": "123",
"db": "mydb",
"role": "readWrite"
},
"thirduser": {
"id": "thirduser",
"password": "123",
"db": "mydb",
"role": "read"
},
"id": "users"
}

I want to loop through the databag, in order to create users in MongoDB. 我想遍历数据包,以便在MongoDB中创建用户。 This is what I have come up with (and a lot more fruitless attempts) 这就是我想出的(还有很多徒劳的尝试)

users_databag_item = data_bag_item('mongodb', 'users')
users_databag_item.each_pair do | user, values |
log "db.createUser( { user: '#{values['id']}', pwd: '#{values['password']}', roles: [ { role: '#{values['role']}', db: '#{values['db']}' } ] } )"
end

The output of the script above is; 上面脚本的输出是;

  1. log[printing the records: db.createUser( { user: 'firstuser', pwd: '123', roles: [ { role: 'read', db: 'mydb' } ] } ) ] action write log [打印记录:db.createUser({用户:'firstuser',pwd:'123',角色:[{角色:'read',db:'mydb'}]}))]操作
  2. log[printing the records: db.createUser( { user: 'seconduser', pwd: '123', roles: [ { role: 'read', db: 'mydb' } ] } ) ] action write 日志[打印记录:db.createUser({用户:'seconduser',pwd:'123',角色:[{角色:'read',db:'mydb'}]}))]操作
  3. log[printing the records: db.createUser( { user: 'thirduser', pwd: '123', roles: [ { role: 'read', db: 'mydb' } ] } ) ] action write 日志[打印记录:db.createUser({用户:'thirduser',密码:'123',角色:[{角色:'read',db:'mydb'}]}))]操作
  4. log[printing the records: db.createUser( { user: '', pwd: '', roles: [ { role: '', db: '' } ] } ) ] action write 日志[打印记录:db.createUser({用户:”,密码:,角色:[{角色:,db:”}]}))]操作

Have a look at row 4, it contains an empty record. 看一下第4行,其中包含一个空记录。 It seems that the loop also uses ("id": "users"). 似乎循环也使用了(“ id”:“ users”)。 Is there a way to filter out this entry? 有没有办法过滤掉该条目?

To prove this I have added "id": "users", "password": "123" which resulted in 2 extra empty records. 为了证明这一点,我添加了“ id”:“ users”,“ password”:“ 123”,这导致2个额外的空记录。

So, I need to filter out: ("id": "users"). 因此,我需要过滤掉:(“ id”:“ users”)。 How can I achieve this? 我该如何实现?

Can put filter for the object type of the values:- 可以为值的对象类型放入过滤器:-

users_databag_item = data_bag_item('mongodb', 'users')
users_databag_item.values.each do | values |
    log "db.createUser( { user: '#{values['id']}', pwd: '#{values['password']}', roles: [ { role: '#{values['role']}', db: '#{values['db']}' } ] } )" if !values.is_a? (String)
end

or 要么

users_databag_item = data_bag_item('mongodb', 'users')
users_databag_item.each_pair do | id, values |
    log "db.createUser( { user: '#{values['id']}', pwd: '#{values['password']}', roles: [ { role: '#{values['role']}', db: '#{values['db']}' } ] } )" if !id.eql? "id"
end

Yes it did work. 是的,它确实有效。 In the end I have managed to fix it in another way. 最后,我设法用另一种方式修复了它。

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

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