ruby - Using databag in CHEF recipe -
i have been struggling on week solve issue. there can me out solving this.
i have databag mongo, contents of databag is:
{ "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 loop through databag, in order create users in mongodb. have come (and lot more fruitless attempts)
users_databag_item = data_bag_item('mongodb', 'users') users_databag_item.each_pair | user, values | log "db.createuser( { user: '#{values['id']}', pwd: '#{values['password']}', roles: [ { role: '#{values['role']}', db: '#{values['db']}' } ] } )" end
the output of script above is;
- log[printing records: db.createuser( { user: 'firstuser', pwd: '123', roles: [ { role: 'read', db: 'mydb' } ] } ) ] action write
- log[printing records: db.createuser( { user: 'seconduser', pwd: '123', roles: [ { role: 'read', db: 'mydb' } ] } ) ] action write
- log[printing records: db.createuser( { user: 'thirduser', pwd: '123', roles: [ { role: 'read', db: 'mydb' } ] } ) ] action write
- log[printing records: db.createuser( { user: '', pwd: '', roles: [ { role: '', db: '' } ] } ) ] action write
have @ row 4, contains empty record. seems loop uses ("id": "users"). there way filter out entry?
to prove have added "id": "users", "password": "123" resulted in 2 empty records.
so, need filter out: ("id": "users"). how can achieve this?
can put filter object type of values:-
users_databag_item = data_bag_item('mongodb', 'users') users_databag_item.values.each | 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 | id, values | log "db.createuser( { user: '#{values['id']}', pwd: '#{values['password']}', roles: [ { role: '#{values['role']}', db: '#{values['db']}' } ] } )" if !id.eql? "id" end
Comments
Post a Comment