Hi Experts:
Iam writing in the provide to create a method which can be used in the recipe ,(which i have shown in below)
But this gives me error pw NoMethodError: undefined method `pw'
provides :cms_mysql_password, platform_family: 'rhel'
action :create do
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
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
end
Below is my recipe which is working fine.
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