インストール †
- パッケージ (apt、yum、homebrew) からインストールできるけど、Ansible への追随が遅いので python の pip がよさげ
- homebrew 版の python への Ansible のインストールは失敗するので、インストールされている場合はアンインストールする
$ brew uninstall python
- xcode (含apple版python) をインストール
$ xcode-select --install
- pip をインストール
$ easy_install --user pip
Mac OSX では、"--user" を入れて、ログインユーザのローカル環境にインストールするようにしないとインストールに失敗する
- ansible をインストール
$ pip install --user ansible --upgrade
$ ansible --version
ansible 2.1.0.0
config file =
configured module search path = Default w/o overrides
- ルートの ansible.cfg を配置
$ sudo mkdir /etc/ansible
$ sudo curl -L https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg -o /etc/ansible/ansible.cfg
ほぼ全部コメントなんで無くてもいいけど、リファレンスとして入れておく
- ファイル・ディスクリプタの上限を大きくする (ansible では、デフォルトの 256 では足りない)
- /Library/LaunchDaemons?/limit.maxfiles.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>524288</string>
<string>524288</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
- 確認
$ launchctl limit maxfiles
maxfiles 524288 524288
$ ulimit -n
524288
CentOS7 を Vagrant で起動する †
$ cd sourcetree/AnsibleExam/nginx
$ vagrant init centos/7
$ vagrant up
- ここまでで Vagrant により、次のファイルが生成される
$ tree -a
.
├── .vagrant
│ └── machines
│ └── default
│ └── virtualbox
│ ├── action_provision
│ ├── action_set_name
│ ├── creator_uid
│ ├── id
│ ├── index_uuid
│ ├── private_key
│ └── synced_folders
└── Vagrantfile
4 directories, 8 files
- 接続情報の確認
$ vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile "/Users/atsushi/sourcetree/AnsibleExam/gitlab/.vagrant/machines/default/virtualbox/private_key"
IdentitiesOnly yes
LogLevel FATAL
このプロジェクト向けの ansible の設定 †
- ansible.cfg
[defaults]
hostfile = hosts
remote_user = vagrant
private_key_file = .vagrant/machines/default/virtualbox/private_key
host_key_checking = False
- ansible.cfg の優先順位 �� ./ansible.cfg �� ~/ansible.cfg �� /etc/ansible.cfg
- まぁ、プロジェクトのカレントディレクトリ (��) を参照するようにするのがいいでしょう
- hosts
testserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
- いずれ、ansible.cfg、hosts に複数サーバを追加すると、複数サーバに対して一度にコマンド発行できるようになる
- ここまでで、プロジェクトのディレクトリはこうなっているはず
.
├── /.vagrant
├── Vagrantfile
├── ansible.cfg
└── hosts
ansible コマンドの実行 †
$ ansible testserver -m ping
testserver | SUCCESS => {
"changed": false,
"ping": "pong"
}
$ ansible testserver -m command -a uptime
testserver | SUCCESS | rc=0 >>
10:26:47 up 1:04, 1 user, load average: 0.00, 0.01, 0.05
うまく動いているようだ
- ansible コマンドのオプション
| -m {モジュール名} -a {パラメータ} | モジュール実行 |
| -a "{Shellコマンド}" | シェルコマンド実行 (暗黙に shell モジュール呼び出し) |
| -s | sudo |
ansible のモジュール †
Playbook で nginx をインストール・設定 †
- Vagrantfile を書き換えて、外部から 80/443 にアクセスできるようにする
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 443, host: 8443
end
$ vagrant reload
- nginx.yml (=Playbook =AnsibleのモジュールをYAML形式で並べたもの。詳細は ⇒ Ansible Playbook )
- name: Configure nginx
hosts: testserver
become: True
tasks:
- name: Add repository
yum_repository:
name: epel
description: EPEL YUM repo
baseurl: http://download.fedoraproject.org/pub/epel/$releasever/$basearch/
- name: Add gpg key of EPEL
rpm_key: key=https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7 state=present
- name: Install nginx
yum: name=nginx state=latest
- name: start nginx
service: name=nginx enabled=Yes state=started
- 実行!
$ ansible-playbook nginx.yml
PLAY [Configure nginx] *********************************************************
TASK [setup] *******************************************************************
ok: [testserver]
TASK [Add repository] **********************************************************
ok: [testserver]
TASK [Add gpg key of EPEL] *****************************************************
ok: [testserver]
TASK [Install nginx] ***********************************************************
ok: [testserver]
TASK [restart nginx] ***********************************************************
changed: [testserver]
PLAY RECAP *********************************************************************
testserver : ok=5 changed=1 unreachable=0 failed=0
- 無事起動しました
- RHEL系では /etc/nginx/nginx.conf がルートの設定ファイルになっているようだ。後でこいつらを変えてきます
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
- 全体の設定追加は /etc/nginx/conf.d/*.conf
- デフォルトサーバの設定追加は /etc/nginx/default.d/*.conf
- デフォルトサーバの静的コンテンツは /usr/share/nginx/html
vagrant up で ansible を呼び出す †
- Vagrantfile を書き換えて、ansible を呼び出すようにする
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 443, host: 8443
config.vm.provision "ansible" do |ansible|
ansible.limit = "all" # "all" or "${machine group}" on the ansible hosts file.
ansible.inventory_path = "hosts"
ansible.playbook = "nginx.yml"
end
end
これで、
$ vagrant up
または
$ vagrant provision
で ansible が走る。
初回しか provisioning は動かない。強制的に実行するには
$vagrant up --provision
または
$vagrant reload --provision
- cf. https://www.vagrantup.com/docs/provisioning/ansible_local.html
- ansible を実行するときに ssh でうまく通信できないようなら、ansible.cfg に次の設定をするといいらしい
[defaults]
host_key_checking = no
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes
Ansible