Reputation: 5824
I am writing Chef cookbook for PHP configuration, getting password error while knife ssh 'sudo chef-client'
Webserver.rb :
#
# Cookbook Name:: awesome_customers
# Recipe:: webserver
#
# Copyright (c) 2015 The Authors, All Rights Reserved.
# Install Apache and start the service.
httpd_service 'customers' do
mpm 'prefork'
action [:create, :start]
end
# Write the home page.
template "#{node['awesome_customers']['document_root']}/index.php" do
# content '<html>This is a placeholder</html>'
source 'index.php.erb'
mode '0644'
owner node['awesome_customers']['user']
group node['awesome_customers']['group']
variables({
:database_password => user_password_data_bag_item['password']
})
end
# Install the mod_php5 Apache module.
httpd_module 'php5' do
instance 'customers'
end
# Install php5-mysql.
package 'php5-mysql' do
action :install
notifies :restart, 'httpd_service[customers]'
end
# Load the secrets file and the encrypted data bag item that holds the database password.
password_secret = Chef::EncryptedDataBagItem.load_secret(node['awesome_customers']['passwords']['secret_path'])
user_password_data_bag_item = Chef::EncryptedDataBagItem.load('passwords', 'db_admin_password', password_secret)
Error Output :
54.153.93.47 Recipe Compile Error in /var/chef/cache/cookbooks/awesome_customers/recipes/default.rb
54.153.93.47 NoMethodError
54.153.93.47 undefined method `user_password_data_bag_item' for Chef::Resource::Template
54.153.93.47 32>> :database_password => user_password_data_bag_item['password']
54.153.93.47 [2015-09-29T10:41:56+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
I have loaded the secrets file and the encrypted data bag item that holds the database password. Still Getting password error !
Upvotes: 1
Views: 120
Reputation: 5824
Got the solution !
I copied the following line which holds database password @ end of "Webserver.rb". As order is important in recipe it should be before the template resource for the home page.
# Load the secrets file and the encrypted data bag item that holds the database password.
password_secret = Chef::EncryptedDataBagItem.load_secret(node['awesome_customers']['passwords']['secret_path'])
user_password_data_bag_item = Chef::EncryptedDataBagItem.load('passwords', 'db_admin_password', password_secret)
So Webserver.rb looks like :
#
# Cookbook Name:: awesome_customers
# Recipe:: webserver
#
# Copyright (c) 2015 The Authors, All Rights Reserved.
# Install Apache and start the service.
httpd_service 'customers' do
mpm 'prefork'
action [:create, :start]
end
# Load the secrets file and the encrypted data bag item that holds the database password.
password_secret = Chef::EncryptedDataBagItem.load_secret(node['awesome_customers']['passwords']['secret_path'])
user_password_data_bag_item = Chef::EncryptedDataBagItem.load('passwords', 'db_admin_password', password_secret)
# Write the home page.
template "#{node['awesome_customers']['document_root']}/index.php" do
# content '<html>This is a placeholder</html>'
source 'index.php.erb'
mode '0644'
owner node['awesome_customers']['user']
group node['awesome_customers']['group']
variables({
:database_password => user_password_data_bag_item['password']
})
end
# Install the mod_php5 Apache module.
httpd_module 'php5' do
instance 'customers'
end
# Install php5-mysql.
package 'php5-mysql' do
action :install
notifies :restart, 'httpd_service[customers]'
end
This worked !
Upvotes: 0