# Error running package and template resoource in ruby block

**URL:** <https://discourse.chef.io/t/error-running-package-and-template-resoource-in-ruby-block/6083>\
**Category:** Chef Infra (archive)\
**Created:** [December 18, 2014, 5:05pm UTC](https://discourse.chef.io/t/error-running-package-and-template-resoource-in-ruby-block/6083 "2014-12-18T17:05:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![M\_G](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@M\_G](https://discourse.chef.io/u/M_G)\
**Post date:** [December 18, 2014, 5:05pm UTC](https://discourse.chef.io/t/error-running-package-and-template-resoource-in-ruby-block/6083/1 "2014-12-18T17:05:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tensibai](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tensibai/32/29_2.png) [@Tensibai](https://discourse.chef.io/u/Tensibai)\
**Post date:** [December 18, 2014, 5:17pm UTC](https://discourse.chef.io/t/error-running-package-and-template-resoource-in-ruby-block/6083/2 "2014-12-18T17:17:13Z")

</div>

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](mailto: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:📦: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

---

<div class="post-metadata">

**Author:** ![Tyler\_Ball](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tyler_ball/32/14_2.png) [@Tyler\_Ball](https://discourse.chef.io/u/Tyler_Ball)\
**Post date:** [December 18, 2014, 5:53pm UTC](https://discourse.chef.io/t/error-running-package-and-template-resoource-in-ruby-block/6083/3 "2014-12-18T17:53:13Z")

</div>

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:📦: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) [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) [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](mailto: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](mailto: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
