【AWS】ansibleでEC2インスタンスにプロビジョニングする【ansible】

ansibleを使ってec2にプロビジョニングするときの導入部分をメモ程度に。

ansible自体のインストールは済ませてあるものとします。

 

まずhostsファイルを作ります。

 ~/ansible/hosts  <- フォルダは好きなところで構いません
[kudohamu-ec2s]
xx.xxx.xx.xx <- ec2インスタンスのIP

 

次にplay-bookの作成。今後ec2を増やすことを考慮してここではcommon.ymlという名前にします。

common.ymlでやること
  • ユーザ(kudohamu)の作成
  • kudohamuユーザにsudo権限を付与
  • gitのインストール

 

サーバではなるべくrootユーザを使わないようにします。必要なユーザを作って必要な権限を与え、そのユーザで作業するのが一般的です。

とはいえ趣味のEC2なのでひとまずkudohamuというユーザを作りsudo権限を与えます。権限部分は必要なものを設定してください。

ついでにgitだけインストールしてみることにします。

~/ansible/common.yml
- hosts: kudohamu-ec2s
sudo: yes
vars:
password: $1$kudohamu$xD7dcxxxxxxxxxx/.
tasks:
- user: name=kudohamu password={{password}} shell=/bin/bash state=present append=yes
- name: add a sudo to kudohamu
lineinfile: "dest=/etc/sudoers backup=yes state=present regexp='^kudohamu' line='kudohamu ALL=(ALL) ALL'"
- yum: name=git state=latest

ec2のOSはRHEL6.5を使っていますがAmazonLinuxでもこのままで大丈夫なはずです。Ubuntuとか使っている人はyumコマンドをそれぞれのパッケージ管理システムのものに変えてください。 

 

ユーザのパスワードは生のものを直書きしないようにします。

このファイルを見られてしまったら簡単に乗っ取られてしまいますからね!

下のopensslコマンドを打つことで暗号化したパスワードが出力されます。

$~/ansible% openssl passwd -salt <ソルト文字列> -1 <パスワード>
$1$kudohamu$xD7dcxxxxxxxxxx/.
$~/ansible%

この値をcommon.ymlにコピペしましょう。

ソルト文字列とパスワードはお好きな値を入れてください。

 

ここまででcheckオプションを付けて実際にプロビジョニングできるかテストしてみます。

$~/ansible% ansible-playbook -i hosts common.yml --check
PLAY [kudohamu-ec2s] **********************************************************

GATHERING FACTS ***************************************************************
fatal: [xx.xxx.xx.xx] => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue

TASK: [user name=kudohamu password=$1$kudohamu$xD7dcxxxxxxxxxx/. shell=/bin/bash state=present append=yes] ***
FATAL: no hosts matched or all hosts have already failed -- aborting


PLAY RECAP ********************************************************************
to retry, use: --limit @/Users/kudohamu/common.retry

xx.xxx.xx.xx : ok=0 changed=0 unreachable=1 failed=0

む。

-vvvvオプションを付けて具体的なエラー内容を見てみたところログインに失敗しているようです。

そういえばpem使っていません。

 

hostsファイルを下記のように書き換えます。

[kudohamu-ec2s]
xx.xxx.xx.xx ansible_ssh_user=ec2-user ansible_ssh_private_key_file=~/.ssh/kudohamu-ec2-test-key.pem

初期状態のawsにログイン出来るユーザはec2-userなのでansible_ssh_userにそれを指定します。

またec2インスタンスを立ち上げる時に作成(もしくは指定)したプライベートキー(.pem)の場所をansible_ssh_private_key_fileに指定します。

 

これでもう一度チェック

$~/ansible% ansible-playbook -i hosts common.yml --check

PLAY [kudohamu-ec2s] **********************************************************

GATHERING FACTS ***************************************************************
ok: [xx.xxx.xx.xx]

TASK: [user name=kudohamu password=$1$kudohamu$xD7dcxxxxxxxxxx/. shell=/bin/bash state=present append=yes] ***
changed: [xx.xxx.xx.xx]

TASK: [add a sudo user] *******************************************************
changed: [xx.xxx.xx.xx]

TASK: [yum name=git state=latest] *********************************************
changed: [xx.xxx.xx.xx]

PLAY RECAP ********************************************************************
xx.xxx.xx.xx : ok=4 changed=3 unreachable=0 failed=0

いいですね。

 

checkオプションを外して実際に入れてみましょう(エラーの内容によってはcheckでは通っても実際には通らないこともあります)。

$~/ansible% ansible-playbook -i hosts common.yml

PLAY [kudohamu-ec2s] **********************************************************

GATHERING FACTS ***************************************************************
ok: [xx.xxx.xx.xx]

TASK: [user name=kudohamu password=$1$kudohamu$xD7dcxxxxxxxxxx/. shell=/bin/bash state=present append=yes] ***
changed: [xx.xxx.xx.xx]

TASK: [add a sudo user] *******************************************************
changed: [xx.xxx.xx.xx]

TASK: [yum name=git state=latest] *********************************************
changed: [xx.xxx.xx.xx]

PLAY RECAP ********************************************************************
xx.xxx.xx.xx : ok=4 changed=3 unreachable=0 failed=0

ちゃんと入りました。

 

あとはいつも通りplay-bookにプロビジョニングしたいものを書いていくだけです。

ansibleでのプロビジョニングの詳しい書き方やファイルの構成等は僕もまだまだ勉強中なので他のサイトなり本なりでお願いします。

 

小・中規模のサーバを整えるにはansibleが手軽すぎますねー。

 

 

 

( 'ω' )