The Chef sudo
cookbook installs the sudo
package and configures the /etc/sudoers
file.
It also exposes an LWRP for adding and managing sudoers.
The platform has a package named sudo
and the sudoers
file is /etc/sudoers
.
node['authorization']['sudo']['groups']
- groups to enable sudo access (default:[]
)node['authorization']['sudo']['users']
- users to enable sudo access (default:[]
)node['authorization']['sudo']['passwordless']
- use passwordless sudo (default:false
)node['authorization']['sudo']['include_sudoers_d']
- include and manager/etc/sudoers.d
(default:false
)node['authorization']['sudo']['agent_forwarding']
- preserveSSH_AUTH_SOCK
when sudoing (default:false
)
To use attributes for defining sudoers, set the attributes above on the node (or role) itself:
{
"default_attributes": {
"authorization": {
"sudo": {
"groups": ["admin", "wheel", "sysadmin"],
"users": ["jerry", "greg"],
"passwordless": "true"
}
}
}
}
# roles/example.rb
default_attributes(
"authorization" => {
"sudo" => {
"groups" => ["admin", "wheel", "sysadmin"],
"users" => ["jerry", "greg"],
"passwordless" => true
}
}
)
Note that the template for the sudoers file has the group "sysadmin" with ALL:ALL permission, though the group by default does not exist.
Note Sudo version 1.7.2 or newer is required to use the sudo LWRP as it relies on the "#includedir" directive introduced in version 1.7.2. The recipe does not enforce installing the version. To use this LWRP, set node['authorization']['sudo']['include_sudoers_d']
to true
.
There are two ways for rendering a sudoer-fragment using this LWRP:
- Using the built-in template
- Using a custom, cookbook-level template
Both methods will create the /etc/sudoers.d/#{username}
file with the correct permissions.
The LWRP also performs fragment validation. If a sudoer-fragment is not valid, the Chef run will throw an exception and fail. This ensures that your sudoers file is always valid and cannot become corrupt (from this cookbook).
Example using the built-in template:
sudo 'tomcat' do
user "%tomcat" # or a username
runas 'app_user' # or 'app_user:tomcat'
commands ['/etc/init.d/tomcat restart']
end
sudo 'tomcat'
template 'my_tomcat.erb' # local cookbook template
variables :cmds => ['/etc/init.d/tomcat restart']
end
In either case, the following file would be generated in /etc/sudoers.d/tomcat
# This file is managed by Chef for node.example.com
# Do NOT modify this file directly.
%tomcat ALL=(app_user) /etc/init.d/tomcat restart
Attribute | Description | Example | Default |
---|---|---|---|
name | name of the `/etc/sudoers.d` file | restart-tomcat | current resource name |
user | user to provide sudo privileges to | tomcat | |
group | group to provide sudo privileges to, except `%` is prepended to the name in case it is not already | %admin | |
commands | array of commands this sudoer can execute | ['/etc/init.d/tomcat restart'] | ['ALL'] |
nopasswd | supply a password to invoke sudo | true | false |
template | the erb template to render instead of the default | restart-tomcat.erb | |
variables | the variables to pass to the custom template | :commands => ['/etc/init.d/tomcat restart'] |
If you use the template attribute, all other attributes will be ignored except for the variables attribute.
- Author:: Bryan W. Berry [email protected]
- Author:: Adam Jacob [email protected]
- Author:: Seth Chisamore [email protected]
- Author:: Seth Vargo [email protected]
Copyright 2009-2012, Opscode, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.