1.

Explain what is “file” resource in Chef and how it differs from “cookbook_file” resource  and “template” resource in Chef?

Answer»

“file” resources in Chef are used to manage files in a Chef node during a chef-client run . A sample file resource is as follows:

```file ‘/tmp/index.html’ do      content 'Hello World'    end```

“file” resource has a name field that defines the resource block. In the above example ‘/tmp/index.html’ defines the name of the file which is managed by Chef with the path. Various actions performed by file resource is as follows:

  • CREATE : creation of a file
  • delete : Delete a file
  • touch : Changes the access TIME and MODIFIED time of the file
  • nothing : Do no action unless notified by other resource
  • create_if_missing : Create a file only if the file is not present in Chef node

“cookbook_file” resource is used to copy files from the “files” folder of the Chef cookbook to a path in Chef node during a chef-client run. Sample “cookbook_file” is specified as below:

```cookbook_file '/var/index.php' do source 'index.php' action :create   end```

Here file “index.php” from “files” folder in the cookbook is copied to “index.php” file in “/var” path in Chef node. If file not present it’s created at the path.If the file is present and if the checksum of the “index.php” file matches the “index.php” file in the cookbook of Chef Server, no file transfer is performed.

“template” resources are used to dynamically generate static files from an embedded Ruby template placed in the “templates” folder of a cookbook. For eg:

```template '/etc/motd' do      source 'motd.erb'  end```

“/etc/motd” is the location at which a new file is created on Chef node during a chef-client run  based on the template file “motd.erb” placed in the “templates” folder of the cookbook.A template file may CONTAIN Ruby statements and EXPRESSIONS unlike in “file” resource or “cookbook_file” resource.



Discussion

No Comment Found