machine_image¶
Use the machine_image resource to define a machine image. This image may then be used by the machine resource when building machines.
Warning
This functionality is available with Chef provisioning and is packaged in the Chef development kit. Chef provisioning is a framework that allows clusters to be managed by the chef-client and the Chef server in the same way nodes are managed: with recipes. Use Chef provisioning to describe, version, deploy, and manage clusters of any size and complexity using a common set of tools.
Syntax¶
The syntax for using the machine_image resource in a recipe is as follows:
machine_image 'name' do
attribute 'value' # see properties section below
...
action :action # see actions section below
end
where
machine_imagetells the chef-client to use theChef::Provider::MachineImageprovider during the chef-client runnameis the name of the resource block and also the name of the machine imageattributeis zero (or more) of the properties that are available for this resourceactionidentifies which steps the chef-client will take to bring the node into the desired state
Actions¶
This resource has the following actions:
:archive- Use to archive a machine image.
:create- Default. Use to create a machine image.
:destroy- Use to destroy a machine image.
:nothing- Define this resource block to do nothing until notified by another resource to take action. When this resource is notified, this resource block is either run immediately or it is queued up to be run at the end of the Chef Client run.
Properties¶
This resource has the following properties:
attributes- Use to specify a hash of attributes to be applied to the machine image.
chef_environment- The name of the environment.
complete- Use to specify if all of the attributes specified in
attributesrepresent a complete specification for the machine image. When true, any attributes not specified inattributeswill be reset to their default values. ignore_failureRuby Type: true, false | Default Value:
falseContinue running a recipe if a resource fails for any reason.
image_optionsRuby Type: Hash
Use to specify options that are used with this machine image.
name- The name of the machine image.
notifiesRuby Type: Symbol, ‘Chef::Resource[String]’
A resource may notify another resource to take action when its state changes. Specify a
'resource[name]', the:actionthat resource should take, and then the:timerfor that action. A resource may notify more than one resource; use anotifiesstatement for each resource to be notified.A timer specifies the point during the Chef Client run at which a notification is run. The following timers are available:
:before- Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
:delayed- Default. Specifies that a notification should be queued up, and then executed at the end of the Chef Client run.
:immediate,:immediately- Specifies that a notification should be run immediately, per resource notified.
The syntax for
notifiesis:notifies :action, 'resource[name]', :timer
raw_jsonThe machine image as JSON data. For example:
{ }
recipe- Use to add a recipe to the run-list for the machine image. Each
recipeadds a single recipe to the run-list. The order in whichrecipedefines the run-list is the order in which Chef will execute the run-list on the machine image. remove_recipe- Use to remove a recipe from the run-list for the machine image.
remove_role- Use to remove a role from the run-list for the machine image.
retriesRuby Type: Integer | Default Value:
0The number of times to catch exceptions and retry the resource.
retry_delayRuby Type: Integer | Default Value:
2The retry delay (in seconds).
role- Use to add a role to the run-list for the machine image.
run_listUse to specify the run-list to be applied to the machine image.
A run-list defines all of the information necessary for Chef to configure a node into the desired state. A run-list is:
- An ordered list of roles and/or recipes that are run in the exact order defined in the run-list; if a recipe appears more than once in the run-list, the chef-client will not run it twice
- Always specific to the node on which it runs; nodes may have a run-list that is identical to the run-list used by other nodes
- Stored as part of the node object on the Chef server
- Maintained using knife and then uploaded from the workstation to the Chef server, or maintained using Chef Automate
A run-list must be in one of the following formats: fully qualified, cookbook, or default. Both roles and recipes must be in quotes, for example:
'role[NAME]'or
'recipe[COOKBOOK::RECIPE]'Use a comma to separate roles and recipes when adding more than one item the run-list:
'recipe[COOKBOOK::RECIPE],COOKBOOK::RECIPE,role[NAME]'subscribesRuby Type: Symbol, ‘Chef::Resource[String]’
A resource may listen to another resource, and then take action if the state of the resource being listened to changes. Specify a
'resource[name]', the:actionto be taken, and then the:timerfor that action.Note that
subscribesdoes not apply the specified action to the resource that it listens to - for example:file '/etc/nginx/ssl/example.crt' do mode '0600' owner 'root' end service 'nginx' do subscribes :reload, 'file[/etc/nginx/ssl/example.crt]', :immediately end
In this case the
subscribesproperty reloads thenginxservice whenever its certificate file, located under/etc/nginx/ssl/example.crt, is updated.subscribesdoes not make any changes to the certificate file itself, it merely listens for a change to the file, and executes the:reloadaction for its resource (in this examplenginx) when a change is detected.A timer specifies the point during the Chef Client run at which a notification is run. The following timers are available:
:before- Specifies that the action on a notified resource should be run before processing the resource block in which the notification is located.
:delayed- Default. Specifies that a notification should be queued up, and then executed at the end of the Chef Client run.
:immediate,:immediately- Specifies that a notification should be run immediately, per resource notified.
The syntax for
subscribesis:subscribes :action, 'resource[name]', :timer
tags- Use to specify the list of tags to be applied to the machine image. Any tag not specified in this list will be removed.
Examples¶
The following examples demonstrate various approaches for using resources in recipes. If you want to see examples of how Chef uses resources in recipes, take a closer look at the cookbooks that Chef authors and maintains: https://github.com/chef-cookbooks.
Build a machine from a machine image
machine_image 'web_image' do
recipe 'apache2'
end
machine 'web_machine' do
from_image 'web_image'
end