Use the chef report nodes subcommand to generate a nodes-oriented report for your chef environment. This report provides details about nodes, applied policies and the cookbooks used during the most recent chef-client run. Upgrade Lab provides further details on how this command can be used in Chef Infra Client upgrade process.
Use the chef shell-init subcommand to set the Ruby included in Chef Workstation as the system Ruby. Chef Workstation is designed to allow the isolation of applications used by Chef Workstation from other Ruby development tools that may be present on the workstation.
This supports Bash, fish, Windows PowerShell posh , and zsh. You can use the chef shell-init command with Windows PowerShell to add the appropriate variables to your environment.
On new Windows machines, PowerShell scripts will not work until an administrator runs the following command:. This will result in an error similar to the following:. The following commands are built into the chef executable and support the use of Policyfile files. Use the chef clean-policy-cookbooks subcommand to delete cookbooks that are not used by Policyfile files.
Cookbooks are considered unused when they are not referenced by any policy revisions on the Chef Infra Server. Cookbooks that are referenced by orphaned policy revisions are not removed. Use chef clean-policy-revisions to remove orphaned policies. Use the chef clean-policy-revisions subcommand to delete orphaned policy revisions to Policyfile files from the Chef Infra Server.
An orphaned policy revision is not associated to any policy group and therefore is not in active use by any node. Use chef show-policy --orphans to view a list of orphaned policy revisions. Use the chef delete-policy subcommand to delete all revisions of the named policy that exist on the Chef Infra Server. The state of the policy revision is backed up locally and may be restored using the chef undelete subcommand. Use the chef delete-policy-group subcommand to delete the named policy group from the Chef Infra Server.
Any policy revision associated with that policy group is not deleted. The state of the policy group is backed up locally and may be restored using the chef undelete subcommand. Use the chef diff subcommand to display an itemized comparison of two revisions of a Policyfile.
Compare the specified git reference against the current revision of a Policyfile. A shortcut for chef diff --git HEAD. When a git-specific flag is not provided, the on-disk Policyfile. Use --pager to enable paged output for a Policyfile. Default value: --pager. Use the chef export subcommand to create a chef-zero-compatible chef-repo that contains the cookbooks described by a Policyfile.
After a chef-zero-compatible chef-repo is copied to a node, the policy can be applied locally on that machine by running chef-client -z local mode. Remove the contents of the destination directory if that directory is not empty.
The following example uses a whitespace array and the Chef InSpec file audit resource to verify if these three tools are present:.
An if statement can be used to specify part of a recipe to be used when certain conditions are met. The if modifier expression must be a single line, and else and elsif statements are not supported.
A case statement can be used to handle a situation where there are a lot of conditions. Use the when statement for each condition, as many as are required. Use the Ruby File class in a recipe. Because Chef has the file resource, use File to use the Ruby File class. In non-Chef Ruby, the syntax is include without the : prefix , but without the : prefix Chef Infra Client will try to find a provider named include. Using the : prefix tells Chef Infra Client to look for the specified class that follows.
The include? For example, using the include? The following example will only execute apt-get update when the system is either Debian or Ubuntu:. The attribute node['platform'] is an automatic attribute from Chef.
Templates are typically used to set up configuration files, allowing for the use of variables and other features intended to make these files more versatile and reusable. They support conditionals, loops and other Ruby features. Below is an example of an ERB template for setting up an Apache virtual host, using a variable to define the document root for this host:.
In order to apply the template, we need to create a template resource. This is how you would apply this template to replace the default Apache virtual host:. Chef makes a few assumptions when dealing with local files, in order to enforce organization and modularity. In this case, Chef would look for a vhost.
This means you will have to explicitly provide any variables you plan to use inside a template, when defining the template resource. Service resources are used to make sure services are initialized and enabled. They are also used to trigger service restarts. In Chef, service resources need to be declared before you try to notify them, otherwise you will get an error.
If you want to make sure Apache is restarted after a virtual host change, you first need to create a service resource for the Apache service. This is how such resource is defined in Chef:. Now, when defining the template resource, you need to include a notify option in order to trigger a restart:. The complete example, including the template file for setting up Apache and an HTML file to be served by the web server, can be found on Github.
The folder also contains a Vagrantfile that lets you test the recipe in a simplified setup, using a virtual machine managed by Vagrant. We could have used a simple local variable here, however in most use case scenarios, recipes need to define global variables that will be used from included recipes or other files. For these situations, it is necessary to create an attribute instead of a local variable, as the later has a limited scope. This service resource enables and starts the service apache2.
Later on, we will need to notify this resource for a service restart. It is important that the service definition comes before any resource that attempts to notify a service, otherwise you will get an error.
This resource will copy our index. Finally, this template resource applies our Apache virtual host template and notifies the service apache2 for a restart. Chef is a powerful configuration management tool that leverages the Ruby language to automate server provisioning and deployment.
It gives you freedom to use the standard language features for maximum flexibility, while also offering custom DSLs for some resources. Configuration management can drastically improve the integrity of servers over time by providing a framework for automating processes and keeping track of changes made to the system environment. This series will introduce you to the concepts behind Configuration Management and give you a practical overview of how to use Ansible, Puppet and Chef to automate server provisioning.
Where would you like to share this to? Twitter Reddit Hacker News Facebook. Share link Tutorial share link. Sign Up. DigitalOcean home. Community Control Panel. Hacktoberfest Contribute to Open Source. Getting Started with Configuration Management.
0コメント