LoginSignup
20
21

More than 5 years have passed since last update.

Chefでopscode-cookbooksのselinuxレシピを使う

Last updated at Posted at 2013-05-28

opscodeのpublic cookbooksを使ってselinuxを無効化するサンプルです。

このサンプルでは、chef-soloを使ってローカルホストに対してレシピを実行します。
レシピ管理にはBerkshelfを使い、serverspecでテストします。

使用するレシピ

Opscode Cookbook selinux
https://github.com/opscode-cookbooks/selinux

レシピのダウンロード

Berksfile
site :opscode
cookbook 'selinux'
bundle exec berks install --path vendor/cookbooks

レシピを読む

vendor/cookbooks/selinux/recipes/disabled.rb

# setenforce 0で一時的にSELinuxを無効化し、
# /etc/selinux/configの作成を通知する
# selinuxenabledコマンドの終了ステータスが0(selinuxが有効)の場合だけ実行される
execute "disable selinux enforcement" do
  only_if "which selinuxenabled && selinuxenabled"
  command "setenforce 0"
  action :run
  notifies :create, "template[/etc/selinux/config]"
end

# 再起動の際もSELinuxの無効状態を維持するために、
# /etc/selinux/configに、設定を記述する
template "/etc/selinux/config" do
  source "sysconfig/selinux.erb"
  variables(
    :selinux => "disabled",
    :selinuxtype => "targeted"
  )
  action :nothing
end

レシピの実行

nodes/localhost.json
{
    "run_list":[
        "recipe[selinux::disabled]"
    ]
}
sudo bundle exec chef-solo -c solo.rb -j nodes/localhost.json

serverspecでテスト

spec/localhost/selinux_disabled_spec.rb
require 'spec_helper'

describe selinux do
  it { should be_disabled }
end

describe file('/etc/selinux/config') do
  it { should be_file }
  it { should contain "SELINUX=disabled" }
end
bundle exec rake spec
SELinux
  should be disabled

File "/etc/selinux/config"
  should be file
  should contain "SELINUX=disabled"

Finished in 0.19807 seconds
3 examples, 0 failures
20
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
21