Error running package and template resoource in ruby block

My code

ruby_block “install pg” do
block do

if !node[:installed][:postgres]

 package "postgres" do
  version "9.3"
  action :install
  provider Chef::Provider::Package::Apt
 end

template "/etc/postgresql/9.3/main/pg_hba.conf" do
 source "pg_hba.conf.erb"
  mode 0660
 user "postgres"
 group "postgres"

end

execute "createdb ucmsdb" do
  user "postgres"
  not_if "psql --list | grep -v grep | grep ucmsdb", :user => "postgres"
end

node.set[:installed][:postgres] = true
end

end

============================================================================

Error executing action `run` on resource 'ruby_block[install postgres]'

============================================================================

NoMethodError
-------------
undefined method `apt_package' for Chef::Resource::RubyBlock

Cookbook Trace:
---------------
/var/chef/cache/cookbooks/pgdb/recipes/default.rb:48:in `block (2 levels)

in
from_file’

Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/pgdb/recipes/default.rb:44:in

`from_
file’

ruby_block("install postgres") do
  action "run"
  retries 0
  retry_delay 2
  guard_interpreter :default
  block_name "install postgres"
  cookbook_name "pgdb"
  recipe_name "default"
  block

#<Proc:0x000000030f6650@/var/chef/cache/cookbooks/pgdb/recipes/defau
lt.rb:45>
end

Running handlers:
Running handlers complete
Chef Client failed. 0 resources updated in 8.434211777 seconds

Please help

Thanx

Why are you trying to write a recipe within a ruby block ? I can't see a
use case (but tell me if you find one).

You may do the same thing directly within the recipe as they are ruby
code too.

Le 2014-12-18 18:05, meher03@hotmail.com a écrit :

My code

ruby_block "install pg" do
block do

if !node[:installed][:postgres]

package "postgres" do
version "9.3"
action :install
provider Chef::Provider::package::Apt
end

template "/etc/postgresql/9.3/main/pg_hba.conf" do
source "pg_hba.conf.erb"
mode 0660
user "postgres"
group "postgres"
end

execute "createdb ucmsdb" do
user "postgres"
not_if "psql --list | grep -v grep | grep ucmsdb", :user => "postgres"
end

node.set[:installed][:postgres] = true
end

end

============================================================================

Error executing action run on resource 'ruby_block[install postgres]'

============================================================================

NoMethodError

undefined method `apt_package' for Chef::Resource::RubyBlock

Cookbook Trace:

/var/chef/cache/cookbooks/pgdb/recipes/default.rb:48:in `block (2 levels)
in
from_file'

Compiled Resource:

Declared in /var/chef/cache/cookbooks/pgdb/recipes/default.rb:44:in

`from_
file'

ruby_block("install postgres") do
action "run"
retries 0
retry_delay 2
guard_interpreter :default
block_name "install postgres"
cookbook_name "pgdb"
recipe_name "default"
block
#<Proc:0x000000030f6650@/var/chef/cache/cookbooks/pgdb/recipes/defau
lt.rb:45>
end

Running handlers:
Running handlers complete
Chef Client failed. 0 resources updated in 8.434211777 seconds

Please help

Thanx

You could wrap the same logic you’re trying to do in an LWRP:

use_inline_resources

action :install do
package "postgres" do
version "9.3"
action :install
provider Chef::Provider::package::Apt
end

template "/etc/postgresql/9.3/main/pg_hba.conf" do
source "pg_hba.conf.erb"
mode 0660
user "postgres"
group "postgres"
end

execute "createdb ucmsdb" do
user "postgres"
not_if "psql --list | grep -v grep | grep ucmsdb", :user => "postgres"
end
end

Then leverage that in your recipe (you’ll have to write the resource too, which I’m going to leave to you):

custom_postgres “installer” do
action :install
end

All 3 of the resources are already idempotent, so your node attribute isn’t needed. But if your real installer is more complicated you can add conditional checks inside the lightweight provider. They will not be executed until your resource is.

Read https://docs.chef.io/lwrp_custom_provider.html https://docs.chef.io/lwrp_custom_provider.html and http://docs.chef.io/lwrp_custom_resource.html http://docs.chef.io/lwrp_custom_resource.html for more info on making an LWRP.

-T

On Dec 18, 2014, at 9:17 AM, Tensibai tensibai@iabis.net wrote:

Why are you trying to write a recipe within a ruby block ? I can't see a use case (but tell me if you find one).

You may do the same thing directly within the recipe as they are ruby code too.

Le 2014-12-18 18:05, meher03@hotmail.com a écrit :

My code

ruby_block "install pg" do
block do

if !node[:installed][:postgres]

 package "postgres" do
  version "9.3"
  action :install
  provider Chef::Provider::Package::Apt
 end

template "/etc/postgresql/9.3/main/pg_hba.conf" do
 source "pg_hba.conf.erb"
  mode 0660
 user "postgres"
 group "postgres"

end

execute "createdb ucmsdb" do
  user "postgres"
  not_if "psql --list | grep -v grep | grep ucmsdb", :user => "postgres"
end

node.set[:installed][:postgres] = true
end

end

============================================================================

Error executing action `run` on resource 'ruby_block[install postgres]'

============================================================================

NoMethodError
-------------
undefined method `apt_package' for Chef::Resource::RubyBlock

Cookbook Trace:
---------------
/var/chef/cache/cookbooks/pgdb/recipes/default.rb:48:in `block (2 levels)

in
from_file'

Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/pgdb/recipes/default.rb:44:in

`from_
file'

ruby_block("install postgres") do
  action "run"
  retries 0
  retry_delay 2
  guard_interpreter :default
  block_name "install postgres"
  cookbook_name "pgdb"
  recipe_name "default"
  block

#<Proc:0x000000030f6650@/var/chef/cache/cookbooks/pgdb/recipes/defau
lt.rb:45>
end

Running handlers:
Running handlers complete
Chef Client failed. 0 resources updated in 8.434211777 seconds

Please help

Thanx