A resource represents a piece of infrastructure and its desired state, such as a package that should be installed, a service that should be running, or a file that should be generated.
A recipe is a file that holds one or more resources. Each resource declares what state a part of the system should be in, but not how to get there.
A recipe is a collection of resources that describes a particular configuration or policy. A recipe describes everything that is required to configure part of a system. Recipes do things such as:
- install and configure software components.
- manage files.
- deploy applications.
- execute other recipes.
When you deleted the file, you saw the
Think of an action as the process that achieves the desired configuration state. Every resource in Chef has a default action, and it’s often the most common affirmative one – for example, create a file, install a package, and start a service.
When we created the file we didn’t specify the
:create action because
:create is the default. But of course you can specify it if you want.
Create a directory
directory 'C:\temp\messages' do recursive true end file 'C:\temp\messages\settings.ini' do content 'greeting=hello world' end
It uses the
directory resource to ensure that the
C:\temp\messages directory exists.
Example: install IIS server on windows server
The script is as bellow:
powershell_script 'Install IIS' do code 'Add-WindowsFeature Web-Server' guard_interpreter :powershell_script not_if "(Get-WindowsFeature -Name Web-Server).Installed" end
You don’t have to specify an action, because the
:run is the default action.
This time, Chef does not reinstall IIS. That’s because the
not_if attribute skips the resource when the condition is true. In this case, we use the
Get-WindowsFeature PowerShell cmdlet to check whether the
Web-Serverfeature is installed.