Data bags cannot find method

#1

Hi Experts
I would like to use databags in my provides but getting error data_bag method not found.
provides :cms_mysql_password, platform_family: 'rhel'

set database password

pw = ''
data_bag('secrets').each do |item|
db = data_bag_item('secrets', item)
db['users'].each_pair do |_k, v|
pw = v['password'] if v['id'] == 'root'
end
end
action :create do
script 'set database password' do
interpreter 'bash'
user 'root'
cwd '/tmp'
code <<-EOH
root_temp_pass=$(grep 'A temporary password' /mysql/log/mysqld.log |tail -1 |awk '{split($0,a,": "); print a[2]}')
mysql -uroot -p"$root_temp_pass" --connect-expired-password -Be "ALTER USER 'root'@'localhost' IDENTIFIED BY '{pw}';"
EOH
# not_if { ::File.exist?('/tmp/first_run') }
end
end

Below is my recipe (data bags work fine here)

set database password

pw = ''
data_bag('secrets').each do |item|
db = data_bag_item('secrets', item)
db['users'].each_pair do |_k, v|
pw = v['password'] if v['id'] == 'root'
end
end
cms_mysql_password 'create-password' do
action :create
not_if "mysql -u root -p'#{pw}'"
end

How to call data bag in the provides resources so that it works.Any direction will be helpful.
I have read the chef documentation but not getting the information how to call /load data bags.

0 Likes