SSブログ

itablesを設定して、Port 80/tcpをあける [Chef]

Webサーバを動かすためには、Firewallの設定が有効になっているため、Port 80/tcpをあける必要があります。
/etc/sysconfig/iptables ファイルを変更し、iptablesサービスを再起動することで、設定を変更するCookbookを自前で作成します。
とはいえ、templateファイルを置きかえて、サービスを再起動するだけの内容です。

まず、空のCookbookをsite-cookbookに作成します。
C:\Users\hoge\chef-repo> knife cookbook create iptables -o site-cookbooks ** Creating cookbook iptables ** Creating README for cookbook: iptables ** Creating CHANGELOG for cookbook: iptables ** Creating metadata for cookbook: iptables
/etc/sysconfig/iptablesのテンプレートファイルを以下のように作成します。

【chef-repo\site-cookbooks\iptables\templates\default\iptables.erb】
# Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
※このとき、iptables.erbの改行コードが、CRLFではなく、LFで保存されるよう注意すること。iptablesファイルは、shellスクリプトであるため、改行コードがDOS互換であってはならない。

レシピファイルは、以下のように記述します。

【chef-repo\site-cookbooks\iptables\recipes\iptables.rb】
# vim iptables.rb template "/etc/sysconfig/iptables" do source "iptables.erb" group "root" owner "root" mode "0600" notifies :reload, 'service[iptables]' end service "iptables" do supports :status => true, :restart => true action [ :enable, :restart ] end

RoleのJOSNファイルを以下のように記述します。

【chef-repo\roles\webserver.json】
// webserver.json { "name": "webserver", "default_attributes": {}, "override_attributes": {}, "json_class": "Chef::Role", "descripution": "", "chef_type": "role", "run_list": [ "recipe[iptables::iptables]", ] }
以上で、Cookします。
C:\Users\hoge\chef-repo> knife solo cook vagrant@hoge202

Chef-soloコマンドが実行され、Cookbookが正常に動作したことを確認します。

iptabesサービスが、正常に変更されていることをGuestOS上で確認します。
C:\Users\hoge\chef-repo>vagrant ssh web : [vagrant@localhost ~]$ sudo -s [root@localhost vagrant]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED ACCEPT icmp -- anywhere anywhere ACCEPT all -- anywhere anywhere ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:https REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT) target prot opt source destination REJECT all -- anywhere anywhere reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT) target prot opt source destination



共通テーマ:blog
Webサーバを作るapache2 のCookbookを作る ブログトップ

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。