*作業メモ的な不完全な記事です。あとでまとめを書くかもしれません。
Digital Oceanにansibleを使ってnginxをインストールしてみました。ansibleの知識もnginxの知識もなかったので疲れた汗 とりあえず、平べったくplaybookを書いたけれど、これをroleに分割したりしていかないと。あとはやりながら覚えていこう。
Digital Oceanの用意
- CentOS 7.0にしてみました。
- ssh接続できるようにしておきました。
Dropletを破棄したり、作成したりしていると同じIPでも中身が異なるため、ssh接続が蹴られることがあります。そういうときは ~/.ssh/known_hosts を開いて、接続先と同じIPを持つ行を削除してください。
ansibleのインストール
$ brew install ansible
inventoryの作成
カレントディレクトリにhostsという名前のinventoryファイルを作成しました。デフォルトでは/usr/local/etc/ansible/hosts
というファイルに記述すれば引数していなく自動で読み込んでくれるようなのですが、ポータブルにしたかったのでカレントディレクトリに作成することにしました。
sshアクセスする際のユーザを指定した。指定の仕方としては、
- 実行時の引数
- インベントリファイル
- playbook.yml
という3つのやり方があるようだけれども、おすすめで気持ち的にもスッキリするインベントリファイルでの指定にしてみた。
参考: AnsibleでのSSH接続ユーザー指定方法 – Qiita
[ocean] xxx.xxx.xxx.xxx [ocean:vars] ansible_ssh_user=root
こんな感じになりました。
とりあえずコマンドで実行してみる。
$ ansible ocean -i hosts -m command -a "uptime"
xxx.xxx.xxx.xxx | success | rc=0 >> 01:50:09 up 27 min, 1 user, load average: 0.00, 0.01, 0.01
できました。
nginxのインストール
とりあえず手探りでインストールしてみた。
command
ansible-playbook ocean.yml -i hosts
ocean.yml
--- - hosts: ocean tasks: - name: update all packages yum: name=* state=latest - name: install nginx rpm from a remote repo yum: name=http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm state=present - name: install nginx yum: name=nginx state=present - name: rename nginx conf.default to conf.default.original command: mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.original removes=default.conf - name: apply-nginx.conf template: src=./my_settings.conf.j2 dest=/etc/nginx/conf.d/my_settings.conf - name: start and enable nginx service: name=nginx state=started enabled=yes
- removesはそのファイルがあった場合実行しない。
my_settings.j2
nginxのdefault.confの単なるコピー。あとで編集できるように。
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
digital-ocean
$ nginx -v nginx version: nginx/1.6.2
- source
- 最新のnginx: nginx: Linux packages
- 参考