Vagrantで立ち上げた仮想マシンを利用してAnsibleを導入する
いつも通り、ドットインストールでAnsible導入の流れを見たので、その内容をメモしておく。
Vagrantで仮想マシンを3台立ち上げる
~/Vagrant/ansible
にプロジェクトディレクトリを作成する。
$ mkdir ~/Vagrant/ansible $ cd ~/Vagrant/ansible
Boxの取得
今回、Boxはchef/centos-6.5
を使うため、vagrantcloud.com から目当てのBoxを探す。
https://vagrantcloud.com/discover/featured
コマンドをコピーする。
vagrant init
し、Vagrantfile
の設定を行なう。
$ vagrant init chef/centos-6.5 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. $ vi Vagrantfile
Vagrantfileの設定
config.vm.box = "chef/centos-6.5"
をコメントアウトし、host・web・dbの3台分の設定を行なう。
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| (省略) # config.vm.box = "chef/centos-6.5" config.vm.define "host" do |node| node.vm.box = "chef/centos-6.5" node.vm.hostname = "host" node.vm.network :private_network, ip: "192.168.43.51" end config.vm.define "web" do |node| node.vm.box = "chef/centos-6.5" node.vm.hostname = "web" node.vm.network :private_network, ip: "192.168.43.52" end config.vm.define "db" do |node| node.vm.box = "chef/centos-6.5" node.vm.hostname = "db" node.vm.network :private_network, ip: "192.168.43.53" end (省略) end
仮想マシンの立ち上げ
Vagrantfileの設定後、vagrant up
するが、この実行は時間がかかるので少し待つ。
$ vagrant up
設定した3台の仮想マシンが立ち上がっているかを確認する。3台ともrunning
となっているのでOK。
$ vagrant status Current machine states: host running (virtualbox) web running (virtualbox) db running (virtualbox) This environment represents multiple VMs. The VMs are all listed above with their current state. For more information about a specific VM, run `vagrant status NAME`.
Ansibleをインストールする
今回Ansibleをインストールするのはhostマシンのみ。まず、hostマシンにSSH接続する。
$ vagrant ssh host Last login: Fri Mar 7 16:57:20 2014 from 10.0.2.2
epelのインストール
Ansibleをインストールするには、epel(いーぺる)リポジトリが必要なため、epelをインストールしておく。下記サイトからepel-release-6-8.noarch.rpm
を探し、リンクアドレスをコピーする。
http://dl.fedoraproject.org/pub/epel/6/x86_64/
wget
コマンドを使い、コピーしたリンクアドレス先のファイルをDLし、rpm
コマンドを実行してインストールする。
[vagrant@host ~]$ wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [vagrant@host ~]$ sudo rpm -Uvh epel-release-6-8.noarch.rpm
Ansibleのインストール
これでepelのリポジトリが有効になったので、Ansibleをインストールする。
[vagrant@host ~]$ sudo yum -y install ansible (省略) Complete! [vagrant@host ~]$ ansible --version ansible 1.7
SSH接続の設定
.ssh/config
ファイルに複数の仮想マシンのSSH接続を設定する。
[vagrant@host ~]$ vi .ssh/config
.ssh/config
ファイルの設定。
Host web HostName 192.168.43.52 Host db HostName 192.168.43.53
configファイルのパーミッションを変更する。
[vagrant@host ~]$ chmod 600 .ssh/config
Hostマシン上で秘密鍵(id_rsa
)と公開鍵(id_rsa.pub
)を作成する。
[vagrant@host ~]$ ssh-keygen -t rsa (省略) Your identification has been saved in /home/vagrant/.ssh/id_rsa. Your public key has been saved in /home/vagrant/.ssh/id_rsa.pub. (省略)
作成した鍵をweb・dbにコピーする。web・dbともにパスワードを聞かれるが、ここはvagrant
と入れる。
[vagrant@host ~]$ ssh-copy-id web [vagrant@host ~]$ ssh-copy-id db
SSH接続ができるか確認する。
[vagrant@host ~]$ ssh web Last login: Fri Mar 7 16:57:20 2014 from 10.0.2.2 [vagrant@host ~]$ ssh db Last login: Thu Oct 2 09:21:08 2014 from 192.168.43.51
Ansibleコマンドを使う
ansible
コマンドの実行には、Inventoryファイル(デフォルトは/etc/ansible/hosts
)が必要。
Inventoryファイルのパスはansible.cfg
ファイルで設定している。
Inventoryファイルの作成
今回は、デフォルトの/etc/ansible/hosts
ではなく、hostマシンのホームディレクトリ上にInventoryファイル(hostsファイルとする)を作成する。
[vagrant@host ~]$ vi hosts
hostsファイルに設定を書く。
[web] 192.168.43.52 [db] 192.168.43.53
Ansibleの疎通確認
オプション-i
でInventoryファイルを指定し、pingモジュールを実行する。
[vagrant@host ~]$ ansible all -i hosts -m ping 192.168.43.52 | success >> { "changed": false, "ping": "pong" } 192.168.43.53 | success >> { "changed": false, "ping": "pong" }
ansible.cfgファイルの作成
上記では、モジュールの実行時にInventoryファイルを指定したが、ansible.cfg
ファイルを設定することで、Inventoryファイルの指定を省略できる。
[vagrant@host ~]$ vi ansible.cfg
ansible.cfg
ファイルに設定を書く。
[defaults] hostfile = ./hosts
今度は-i
オプション指定を省略して実行する。
[vagrant@host ~]$ ansible all -m ping 192.168.43.52 | success >> { "changed": false, "ping": "pong" } 192.168.43.53 | success >> { "changed": false, "ping": "pong" }
Playbookを使う
Playbookはサーバのあるべき状態(冪等性)を設定したもの。
今回は、webとdbにユーザを追加する処理をPlaybookに設定する。
Playbookの作成
ファイル形式はYAML。
[vagrant@host ~]$ vi playbook.yml
Playbookに設定を書く。モジュールの設定は公式ドキュメントを見るとよい。userモジュールであれば System Modules - System Modules - user ページを参照。
--- - hosts: all # 対象となるサーバ(この場合すべてのサーバ) sudo: yes # ユーザの追加には管理者権限が必要なため tasks: # モジュールを指定 - name: add a new user # タスクの名前を指定(必須ではない) user: name=ponkiti # userモジュールを使う、オプションのnameは必須
設定したPlaybookを実行する。
[vagrant@host ~]$ ansible-playbook playbook.yml [WARNING]: The version of gmp you have installed has a known issue regarding timing vulnerabilities when used with pycrypto. If possible, you should update it (ie. yum update gmp). PLAY [all] ******************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.43.53] ok: [192.168.43.52] TASK: [add a new user] ******************************************************** changed: [192.168.43.52] changed: [192.168.43.53] PLAY RECAP ******************************************************************** 192.168.43.52 : ok=2 changed=1 unreachable=0 failed=0 192.168.43.53 : ok=2 changed=1 unreachable=0 failed=0
ここで出力された[WARNING]は気にしなくていいらしい。ユーザが追加されたためchanged=1
となる。
冪等性を試す
先程と同様の処理を実行すると、既にユーザが追加済みなのでchanged=0
となる。
[vagrant@host ~]$ ansible-playbook playbook.yml (省略) PLAY RECAP ******************************************************************** 192.168.43.52 : ok=2 changed=0 unreachable=0 failed=0 192.168.43.53 : ok=2 changed=0 unreachable=0 failed=0
webにSSH接続し、Playbookにて追加したユーザが/etc/passwd
ファイルに登録されているかを確認する。
[vagrant@host ~]$ ssh web Last login: Thu Oct 2 09:51:57 2014 from 192.168.43.51 [vagrant@web ~]$ cat /etc/passwd (省略) vboxadd:x:498:1::/var/run/vboxadd:/bin/false ponkiti:x:501:501::/home/ponkiti:/bin/bash
stateオプション
userモジュールの他のオプションを使ってみる。userモジュールでよく使うのはstateオプション。これはユーザの存在有無を設定するもの。
state=absent
とすることで、ユーザが存在しない状態にできる。
--- - hosts: all sudo: yes tasks: - name: add a new user user: name=ponkiti state=absent
ユーザが削除されたことを確認する。
[vagrant@host ~]$ ssh web Last login: Thu Oct 2 09:58:02 2014 from 192.168.43.51 [vagrant@web ~]$ cat /etc/passwd (省略) vboxadd:x:498:1::/var/run/vboxadd:/bin/false
ansible-playbookのオプション
- Playbookの文法のチェックを行なう。
[vagrant@host ~]$ ansible-playbook playbook.yml --syntax-check [WARNING]: The version of gmp you have installed has a known issue regarding timing vulnerabilities when used with pycrypto. If possible, you should update it (ie. yum update gmp). playbook: playbook.yml
- タスクの一覧を出力する。
[vagrant@host ~]$ ansible-playbook playbook.yml --list-task [WARNING]: The version of gmp you have installed has a known issue regarding timing vulnerabilities when used with pycrypto. If possible, you should update it (ie. yum update gmp). playbook: playbook.yml play #1 (all): add a new user
- ドライランを行なう。
[vagrant@host ~]$ ansible-playbook playbook.yml --check (省略) PLAY RECAP ******************************************************************** 192.168.43.52 : ok=2 changed=0 unreachable=0 failed=0 192.168.43.53 : ok=2 changed=0 unreachable=0 failed=0
変数を使う
変数はvars
でKeyとValueを定義して、値を使う場合はKeyを{{ }}
で囲う。
--- - hosts: all sudo: yes vars: username: ponkiti tasks: - name: add a new user user: name={{username}}
実行時に変数をユーザに入力させる場合は、vars_prompt
で定義する。
--- - hosts: all sudo: yes vars_prompt: username: "Enter username" tasks: - name: add a new user user: name={{username}}