# I need to find out the user,group of directory and change owner by using varibales

**URL:** <https://discourse.chef.io/t/i-need-to-find-out-the-user-group-of-directory-and-change-owner-by-using-varibales/14426>\
**Category:** Chef Automate\
**Created:** [January 29, 2019, 9:39pm UTC](https://discourse.chef.io/t/i-need-to-find-out-the-user-group-of-directory-and-change-owner-by-using-varibales/14426 "2019-01-29T21:39:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![raghu1977](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@raghu1977](https://discourse.chef.io/u/raghu1977)\
**Post date:** [January 29, 2019, 9:39pm UTC](https://discourse.chef.io/t/i-need-to-find-out-the-user-group-of-directory-and-change-owner-by-using-varibales/14426/1 "2019-01-29T21:39:48Z")

</div>

I am new writing recipe. I want to find user and group of existing directory/file store it in variable . I need to change the owner and group using that variable. After doing some research, I wrote below code in default.rb. But it is not assigning "#{user\_name}:#{group\_name}" at chown command. Can someone help be with below code? thanks in Advance .

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

ruby\_block "get user" do  
block do  
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)  
user1 = "ls -ltr /opt/AppD | grep -w AppServerAgent | awk '{print $3}'"  
group1 = "ls -ltr /opt/AppD | grep -w AppServerAgent | awk '{print $4}'"  
user\_out = shell\_out(user1)  
group\_out = shell\_out(group1)  
node.run\_state['user\_name'] = user\_out.stdout  
node.run\_state['group\_name'] = group\_out.stdout  
end  
action :create  
end

template "/tmp/script.sh" do  
source "script.sh.erb"  
variables(  
:user\_name =\> lazy { node.run\_state['user\_name'] },  
:group\_name =\> lazy { node.run\_state['group\_name'] }  
)  
end

user\_name=node.run\_state['user\_name']  
group\_name=node.run\_state['group\_name']

execute "unzip\_agent\_file" do  
command \<\<-EOH  
cd #{install\_dir}  
old\_file=( $(ls -ltr | grep ^l | awk '{print $11}') )  
cp -r $old\_file "old\_file"-(date +"%m-%d-%y")  
unlink AppServerAgent  
rm -rf $old\_file  
mkdir -p #{install\_dir}/AppServerAgent#{node['appdynamics']['agent']['version']}  
cd #{install\_dir}/AppServerAgent#{node['appdynamics']['agent']['version']}  
unzip -qq #{install\_dir}/#{local\_zip\_file} -d #{install\_dir}/AppServerAgent#{node['appdynamics']['agent']['version']}  
cd #{install\_dir}  
ln -sf AppServerAgent#{node['appdynamics']['agent']['version']} AppServerAgent  
chown -R #{user\_name}:#{group\_name} /opt/AppD/  
EOH

end

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

---

<div class="post-metadata">

**Author:** ![raghu1977](https://avatars.discourse-cdn.com/v4/letter/r/ea666f/32.png) [@raghu1977](https://discourse.chef.io/u/raghu1977)\
**Post date:** [January 30, 2019, 4:14pm UTC](https://discourse.chef.io/t/i-need-to-find-out-the-user-group-of-directory-and-change-owner-by-using-varibales/14426/2 "2019-01-30T16:14:13Z")

</div>

Can someone help me on this ? "#{user\_name}:#{group\_name}" is throwing as null value. Not sure where I am doing mistake in the above code.
