LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Changing Directories in Chef Recipe (https://www.linuxquestions.org/questions/linux-newbie-8/changing-directories-in-chef-recipe-4175596940/)

johnpedou 01-06-2017 03:38 PM

Changing Directories in Chef Recipe
 
This is my first attempt at writing a chef cookbook. I'm attempting to write a recipe that will automatically install git, make a new directory (git_repo), change to that directory, initialize to be a git repository, and then connect to the remote git repository once I run chef-client on my node. I got it to install git and make the directory but I am unsure how to write within the recipe to change directory to git_repo. the code I have is

Quote:

package 'git' do
action :install
end

directory '/home/git_repo' do
mode 0755
owner 'root'
group 'root'
action :create
end

execute 'change' do
command "sudo cd git_repo"
end
Is there a better resource type to use besides execute for this particular action? If so, could someone elaborate on it?

sag47 01-10-2017 11:17 PM

Why do you need to run the cd command? Many of Chef's commands offer a cwd (i.e. Current working directory) option. Why can't you use that?

Here's an example.

Code:

bash 'extract_module' do
  cwd '/home/git_repo'
  code <<-EOH
    git init
    git remote add origin some_url
    git fetch origin
    git reset --hard origin/master
    EOH
end

See https://docs.chef.io/resource_bash.html

johnpedou 01-10-2017 11:37 PM

Well it was the first time I was writing a cookbook and I did end up using cwd. That's why I marked the post as solved. Regardless, thank you for the help.

sag47 01-11-2017 01:41 PM

Glad you figured it out. Perhaps post your solution when you solve it so others can benefit. After rereading my original reply it came off a bit terse. That was not my intent.


All times are GMT -5. The time now is 06:16 PM.