注意
動作確認は個人的に使っているクラウドサーバ上で使えることを確認したくらいなので、
使って頂く場合は自己責任でお願いいたします。
なぜAnsibleでデプロイするのか
世の中にはデプロイするために様々なツールがあります。
ChefやPuppet等など、その中でなぜAnsibleを選定したのかというと、
- 対向先にデーモンを常駐させなくて良い
- sshでログインして対向先でスクリプトを実行するというモデルで仕組を理解しやすかった
- 書き方によっては設定ファイルのdry runが実行できる
ラッパースクリプトの作成した背景
- ansible-playbookを使おうと思った時にオプションが多く(長く)てオペミスしそう
- デプロイ対象サーバにファイルの追加や修正する度にymlファイルを修正するのがしんどい
もう少し自分の中で運用方法が安定してくればJenkinsに組み込むことも考えているのですが、
日々ymlやディレクトリ構造をtry & errorしながら修正しているため、スクリプト化しています。
ラッパースクリプト@github
ディレクトリ構成
ラッパースクリプトの動作概要
サーバ構築時に使うスクリプトではデプロイ(ansible-playbook)を2回実行しています。
- 01_init.sh(サーバ構築時に実行するスクリプト)
- 初回
- パッケージアップデート/インストール
- ユーザ/グループ追加
- ディレクトリ/空ファイル作成
- 2回目(02_deploy.shを実行)
- example/roles/{サーバグループ名}/{files,template}配下をfindしてデプロイ先に流したいファイルリストを作成
- ファイルの流し込みを実行
- 初回
- 02_deploy.sh(運用フェーズでファイル修正/追加する時に使っているスクリプト)
- 上記の通り
本ページでの想定シナリオ
単純な設定だけしたwebサーバをAWSで作ってみます。
※ Ansibleでどういう風に設定ファイルが書けたりするのかをことを知ってもらうのが一番の目的なので、
セキュアにする設定やチューニング等の作業は盛り込んでいません。
デプロイイメージ
構築項目
- Apacheを使ってwebサーバを作る
- 全台共通作業
- パッケージのアップデート/インストール
- ユーザ/グループ追加
- 特定のサーバグループ(web-develop)で必要な作業
- ディレクトリ作成
- スクリプト配置
- webサーバで必要な作業
- パッケージインストール
- パッケージの自動起動設定
- 全台共通作業
前提
- ホスト名
- ansibleサーバ:deploy01
- webサーバ:web01-test
- 以下のとおりSSH接続がパスワードなしで接続できること
- loginuser@deploy01 -> loginuser@web01-test
- web01-test上でloginuserはroot権限を持ってること
- ansible version : ansible-2.0.0.1
構築手順
事前確認
bash
ssh loginuser@deploy01
$ whoami
>> loginuser
# 対向先にログイン出来ることを確認
$ ssh loginuser@web01-test
# パスワードなしでrootになれることを確認
$ sudo su -
デプロイの実行
bash
$ ssh loginuser@deploy01
$ git clone https://github.com/st1t/ansible-tools.git
$ cd ansible-tools/scripts
# 実行タスクとデプロイ先を確認(出力イメージは以下参照)
$ ./01_init.sh -g web-develop -c
>> Vault password: password
# デプロイの実行(出力イメージは以下参照)
$ ./01_init.sh -g web-develop -E
>> Vault password: password
単一ファイルの修正&反映したい時
ex) hellow_world_777.shにechoを追記
bash
# ファイル修正
vim ~/ansible-tools/example/roles/web-develop/files/opt/scripts/hellow_world_777.sh
>> 4 echo Hello hogehoge World!!
# 実行対象とタグの名称確認
./02_deploy.sh -g web-develop -c
>> copy /opt/scripts/hellow_world_777.sh TAGS: [task_2]
# task_2だけをdry run
./02_deploy.sh -g web-develop -t task_2
>> TASK [copy /opt/scripts/hellow_world_777.sh] ***********************************
>> changed: [web01-test]
>> --- before: /opt/scripts/hellow_world_777.sh
>> +++ after: /home/st.1t/ansible-tools/example/roles/web-develop/files/opt/scripts/hellow_world_777.sh
>> @@ -1,3 +1,4 @@
>> #!/bin/bash
>>
>> echo Hello World!!
>> +echo Hello hogehoge World!!
# task_2だけを実行
./02_deploy.sh -g web-develop -T task_2
>> TASK [copy /opt/scripts/hellow_world_777.sh] ***********************************
>> changed: [web01-test]
出力イメージ
出力イメージ@cオプション
-cオプション
$ ./01_init.sh -g web-develop -c
Vault password:
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This feature will be removed in a future release. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
playbook: {git cloneした場所}/ansible-tools/example/roles/web-develop/tasks/init.yml
play #1 (web-develop): TAGS: []
pattern: [u'web-develop']
hosts (1):
web01-test
tasks:
initial common task TAGS: [init_setup]
create directory TAGS: []
create dummy files TAGS: [create_dummy_files]
yum install httpd TAGS: [install_server_unique_package, setup_httpd]
chkconfig server_unique_service on TAGS: [setup_httpd]
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This feature will be removed in a future release. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
playbook: {git cloneした場所}/ansible-tools/example/roles/web-develop/tasks/fix_file.yml
play #1 (web-develop): TAGS: []
pattern: [u'web-develop']
hosts (1):
web01-test
tasks:
Definition Variable eth0 TAGS: [regist_variable]
template /etc/httpd/conf/httpd.conf TAGS: [template_1]
template /var/www/html/index.html TAGS: [template_2]
copy /opt/scripts/hellow_world_700.sh TAGS: [task_1]
copy /opt/scripts/hellow_world_777.sh TAGS: [task_2]
出力イメージ@Eオプション
-Eオプション
[2016/04/13-10:53:31] Start init web-develop
Vault password:
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This feature will be removed in a future release. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [web01-test]
TASK [initial common task] *****************************************************
included: /home/st.1t/ansible-tools/example/roles/web-develop/tasks/include_lnk/init_setup.yml for web01-test
TASK [yum update package] ******************************************************
changed: [web01-test] => (item=[u'glibc', u'glibc-common', u'glibc-headers', u'openssh', u'openssl'])
TASK [yum install common tools] ************************************************
ok: [web01-test] => (item=[u'tcpdump', u'telnet', u'vim', u'dstat'])
TASK [add group ec2] ***********************************************************
changed: [web01-test]
TASK [create password hash for ec2_user] ***************************************
changed: [web01-test]
TASK [add user ec2_user] *******************************************************
changed: [web01-test]
TASK [create directory] ********************************************************
changed: [web01-test]
TASK [create dummy files] ******************************************************
changed: [web01-test] => (item={u'owner': u'ec2_user', u'path': u'/opt/scripts/hellow_world_700.sh', u'group': u'ec2', u'mode': u'0700'})
changed: [web01-test] => (item={u'owner': u'ec2_user', u'path': u'/opt/scripts/hellow_world_777.sh', u'group': u'ec2', u'mode': u'0777'})
TASK [yum install httpd] *******************************************************
changed: [web01-test] => (item=[u'apr', u'apr-util', u'httpd', u'httpd-tools', u'mod_ssl'])
TASK [chkconfig server_unique_service on] **************************************
changed: [web01-test]
PLAY RECAP *********************************************************************
web01-test : ok=11 changed=8 unreachable=0 failed=0
[2016/04/13-10:54:35] Exit init web-develop
[2016/04/13-10:54:35] Start Deploy web-develop
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This feature will be removed in a future release. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [web01-test]
TASK [Definition Variable eth0] ************************************************
changed: [web01-test]
TASK [template /etc/httpd/conf/httpd.conf] *************************************
changed: [web01-test]
TASK [template /var/www/html/index.html] ***************************************
changed: [web01-test]
TASK [copy /opt/scripts/hellow_world_700.sh] ***********************************
changed: [web01-test]
TASK [copy /opt/scripts/hellow_world_777.sh] ***********************************
changed: [web01-test]
PLAY RECAP *********************************************************************
web01-test : ok=6 changed=5 unreachable=0 failed=0
[2016/04/13-10:54:38] Exit Deploy web-develop
まだまだコード書くのに不慣れで色々あると思いますが、
もし触っていただいて、もっとこうした方が良いというのがあればgithubからissueでもPRでも送っていただけると幸いです。