OpenSource For You

Cookbook_101: Example code

-

In Figure 3.1, a file is a resource, which will create a ‘regular_ file’in /home/raju directory, the owner of which is Raju, and write some content into the file. The code given in this figure is part of cookbook_101 to help create a file with certain properties. The block of code between do and end typically executes as a single command.

The example code given in Figure 3.2 depicts how to create a directory, ‘dir_file’, with the directory resource, which has the properties of the owner, group and mode of the directory. The action is to create a directory if it has not already been created. The action may be deleted or ‘nothing’. The ‘nothing’attribute in an ‘action’ specifies ‘don’t do anything unless some other resources invoke some action.’ If action is not included in the block of code, then Chef will execute the default action based on the resource type, which may create a file or directory, install a package or start the service.

The example code block given in Figure 3.3 will create a symbolic link file. Link is the resource type; by default, it will create a symbolic link. If you want to create a hard link file, you need to explicitly specify link_type as hard. The source file is list.txt and we want to create a symlink file as symlnk_ list.txt. If the link_type is symbolic, then including link_type is optional. Typically, if properties like user, mode, action, etc, are not mentioned in the resource block, they will be assigned their default value.

Newspapers in English

Newspapers from India