2016年2月16日火曜日

Ansibleを使ってFedora23にサーバを構築していく〜playbookでApacheのconfig編集

このエントリーをはてなブックマークに追加

まず今回はAnsibleでのconfファイルの編集手段の具体的な方法論の前に思いの丈からまとめておくことにする。

導入の目的を考える

先に行っておくがAnsibleで全てを自動で要件に合わせて編集するのは無理。この辺りのAnsible導入の焦点は2つあると思う。

  1. サーバ構築の手順をある程度標準化して自動化したい。環境依存、運用修正部分は直接編集
  2. サーバ構築(環境依存)〜運用修正までの手順をAnsibleのフォーマットに則ってInfrastructure as Codeしたい

この辺、きちんと目的をメンバー(プロジェクト関係者も含む)と共有しておかないと、「コマンド1つで誰でもすぐにサーバ構築できるようになったんじゃなかったの?」になっちゃうので要注意かなと思う。

#自分も含めて後でごっちゃになってきて、目的なんだったっけ?になりかねない。

2つに完全に分けるんじゃなくて導入する際のフェーズとして1→2を踏むというのはありかなと思うけど。

まずは導入の標準化を行おう


標準の/etc/httpd/conf/httpd.confを編集する。今回はだいたいいつも変えないといけないところを変えていこうかと思ったんだけど、でもAllowOverrideぐらいしかデフォルトでは変えない気がするので、そこだけ。

# DocumentRootも変えないし、変えるとしたらVirtualHost切る時だし。

あとは/etc/httpd/conf.d/welcome.confをリネームしておく。これらをInstallHttpd.ymlに書き加える。
---
- hosts: ChildServers
  gather_facts: no
  tasks:
    - name: install httpd by dnf
      dnf: name=httpd state=latest
      register: results
    - debug: var=results
    - name: Replace "AllowOverride None" in httpd.conf
      replace: dest=/etc/httpd/conf/httpd.conf regexp='AllowOverride None' replace='AllowOverride All' backup=yes
    - name: Replace "AllowOverride none" in httpd.conf
      replace: dest=/etc/httpd/conf/httpd.conf regexp='AllowOverride none' replace='AllowOverride All' backup=yes
    - name: rename welcome.conf
      shell: 'mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.org'
    - name: Auto Start httpd daemon
      service: name=httpd state=started enabled=yes
      register: results
    - debug: var=results

'None'と'none'が初めから混じっているのでめんどくさい。。。

でも結果としてはこれで実行したら成功した。無事にhttpd.confも書き換わってwelcome.confもリネームされていた。実際にこれぐらいであれば例となるhttpd.confを用意してcopyモジュールでコピーするのもありだと思う。
TASK: [Replace "AllowOverride None" in httpd.conf] ****************************
changed: [172.16.33.175]

TASK: [Replace "AllowOverride none" in httpd.conf] ****************************
changed: [172.16.33.175]

TASK: [rename welcome.conf] ***************************************************
changed: [172.16.33.175]

実際の運用を考えるとdnfでインストール後、テンプレートで初期起動までを作成。そのあとは環境に応じて編集および新規設定ファイルを作成した上で.confファイルはsvnやgitで管理し、メンテナンス後もリスタートをかけたトリガーでリポジトリにコミットする。そのあとは障害時にここからansibleでもってくるなど下ほうがいいのかもしれない。

2016年2月13日土曜日

Ansibleを使ってFedora23にサーバを構築していく〜playbookでApacheインストール

このエントリーをはてなブックマークに追加
少しコーナー化してきた感じもあるので、「Ansible」のラベルから纏めてみてください。


まずはApacheをインストールするためのPlaybookを作成する。InstallHttpd.ymlとかにしておく。
---
- hosts: ChildServers
  gather_facts: no
  tasks:
    - name: install httpd by dnf
      dnf: name=httpd state=latest
      register: results
    - debug: var=results
よし、実行。
# ansible-playbook InstallHttpd.yml --private-key=~/.ssh/ChildOneSV

PLAY [ChildServers] ***********************************************************

TASK: [install httpd by dnf] **************************************************
failed: [172.16.33.174] => {"failed": true}
msg: `python-dnf` is not installed, but it is required for the Ansible dnf module.

FATAL: all hosts have already failed -- aborting

PLAY RECAP ********************************************************************
           to retry, use: --limit @/root/InstallHttpd.retry

172.16.33.174              : ok=0    changed=0    unreachable=0    failed=1
なるほど。勢いづいてdnfモジュールを使おうとしたけどもChildOneSVの方にpython-dnfがインストールされていないので怒られた。インストールして再チャレンジする。
# dnf -y install python-dnf
メタデータの期限切れの確認は、0:36:24 前の Sat Feb 13 01:30:53 2016 に実施しました。
依存性が解決されました。
========================================================================================================================
 Package                        アーキテクチャ        バージョン                           リポジトリ              容量
========================================================================================================================
インストール:
 pygpgme                        x86_64                0.3-13.fc23                          fedora                  77 k
 pyliblzma                      x86_64                0.5.3-14.fc23                        fedora                  54 k
 python-hawkey                  x86_64                0.6.2-3.fc23                         updates                 78 k
 python-iniparse                noarch                0.4-16.fc23                          fedora                  45 k
 python-libcomps                x86_64                0.1.7-1.fc23                         fedora                  47 k
 python-librepo                 x86_64                1.7.16-2.fc23                        fedora                  53 k
 python2-dnf                    noarch                1.1.6-2.fc23                         updates                449 k
 rpm-python                     x86_64                4.13.0-0.rc1.11.fc23                 updates                100 k

トランザクションの要約
========================================================================================================================
インストール  8 パッケージ

総ダウンロード容量: 902 k
インストール済み容量: 3.0 M
パッケージをダウンロードしています:
(1/8): pyliblzma-0.5.3-14.fc23.x86_64.rpm                                               145 kB/s |  54 kB     00:00
(2/8): python2-dnf-1.1.6-2.fc23.noarch.rpm                                              1.1 MB/s | 449 kB     00:00
(3/8): pygpgme-0.3-13.fc23.x86_64.rpm                                                   187 kB/s |  77 kB     00:00
(4/8): python-iniparse-0.4-16.fc23.noarch.rpm                                           474 kB/s |  45 kB     00:00
(5/8): python-libcomps-0.1.7-1.fc23.x86_64.rpm                                          543 kB/s |  47 kB     00:00
(6/8): python-librepo-1.7.16-2.fc23.x86_64.rpm                                          555 kB/s |  53 kB     00:00
(7/8): python-hawkey-0.6.2-3.fc23.x86_64.rpm                                            977 kB/s |  78 kB     00:00
(8/8): rpm-python-4.13.0-0.rc1.11.fc23.x86_64.rpm                                       1.2 MB/s | 100 kB     00:00
------------------------------------------------------------------------------------------------------------------------
合計                                                                                    399 kB/s | 902 kB     00:02
トランザクションの確認を実行中...
トランザクションの確認に成功しました。
トランザクションのテストを実行中...
トランザクションのテストに成功しました。
トランザクションを実行中...
  インストール    : rpm-python-4.13.0-0.rc1.11.fc23.x86_64                                                          1/8
  インストール    : python-hawkey-0.6.2-3.fc23.x86_64                                                               2/8
  インストール    : python-librepo-1.7.16-2.fc23.x86_64                                                             3/8
  インストール    : python-libcomps-0.1.7-1.fc23.x86_64                                                             4/8
  インストール    : python-iniparse-0.4-16.fc23.noarch                                                              5/8
  インストール    : pyliblzma-0.5.3-14.fc23.x86_64                                                                  6/8
  インストール    : pygpgme-0.3-13.fc23.x86_64                                                                      7/8
  インストール    : python2-dnf-1.1.6-2.fc23.noarch                                                                 8/8
  検証中          : python2-dnf-1.1.6-2.fc23.noarch                                                                 1/8
  検証中          : pygpgme-0.3-13.fc23.x86_64                                                                      2/8
  検証中          : pyliblzma-0.5.3-14.fc23.x86_64                                                                  3/8
  検証中          : python-iniparse-0.4-16.fc23.noarch                                                              4/8
  検証中          : python-libcomps-0.1.7-1.fc23.x86_64                                                             5/8
  検証中          : python-librepo-1.7.16-2.fc23.x86_64                                                             6/8
  検証中          : python-hawkey-0.6.2-3.fc23.x86_64                                                               7/8
  検証中          : rpm-python-4.13.0-0.rc1.11.fc23.x86_64                                                          8/8

インストール:
  pygpgme.x86_64 0.3-13.fc23            pyliblzma.x86_64 0.5.3-14.fc23            python-hawkey.x86_64 0.6.2-3.fc23
  python-iniparse.noarch 0.4-16.fc23    python-libcomps.x86_64 0.1.7-1.fc23       python-librepo.x86_64 1.7.16-2.fc23
  python2-dnf.noarch 1.1.6-2.fc23       rpm-python.x86_64 4.13.0-0.rc1.11.fc23

完了しました!

# ansible-playbook InstallHttpd.yml --private-key=~/.ssh/ChildOneSV

PLAY [ChildServers] ***********************************************************

TASK: [install httpd by dnf] **************************************************
changed: [172.16.33.174]

TASK: [debug var=results] *****************************************************
ok: [172.16.33.174] => {
    "var": {
        "results": {
            "changed": true,
            "invocation": {
                "module_args": "name=httpd state=latest",
                "module_complex_args": {},
                "module_name": "dnf"
            },
            "results": [
                "Installed: httpd-2.4.18-1.fc23.x86_64",
                "Installed: mailcap-2.1.45-1.fc23.noarch",
                "Installed: httpd-tools-2.4.18-1.fc23.x86_64",
                "Installed: httpd-filesystem-2.4.18-1.fc23.noarch",
                "Installed: apr-1.5.2-2.fc23.x86_64",
                "Installed: fedora-logos-httpd-22.0.0-2.fc23.noarch",
                "Installed: apr-util-1.5.4-2.fc23.x86_64"
            ]
        }
    }
}

PLAY RECAP ********************************************************************
172.16.33.174              : ok=2    changed=1    unreachable=0    failed=0

うまくいったみたいだ。インストール先のChildOneSVでhttpdが自動起動に登録されているかを見る。
# systemctl list-unit-files | grep httpd
httpd.service                               disabled
httpd.socket                                disabled
disabledになっている。これをenableにまで持って行ってみよう。先ほどのInstallHttpd.ymlに付け足す。
---
- hosts: ChildServers
  gather_facts: no
  tasks:
    - name: install httpd by dnf
      dnf: name=httpd state=latest
      register: results
    - debug: var=results
    - name: Auto Start httpd daemon
      service: name=httpd enabled=yes
      register: results
    - debug: var=results
実行あるのみ。
# ansible-playbook InstallHttpd.yml --private-key=~/.ssh/ChildOneSV

PLAY [ChildServers] ***********************************************************

TASK: [install httpd by dnf] **************************************************
changed: [172.16.33.174]

TASK: [debug var=results] *****************************************************
ok: [172.16.33.174] => {
    "var": {
        "results": {
            "changed": true,
            "invocation": {
                "module_args": "name=httpd state=latest",
                "module_complex_args": {},
                "module_name": "dnf"
            },
            "results": [
                "Installed: httpd-2.4.18-1.fc23.x86_64",
                "Installed: mailcap-2.1.45-1.fc23.noarch",
                "Installed: httpd-tools-2.4.18-1.fc23.x86_64",
                "Installed: httpd-filesystem-2.4.18-1.fc23.noarch",
                "Installed: apr-1.5.2-2.fc23.x86_64",
                "Installed: fedora-logos-httpd-22.0.0-2.fc23.noarch",
                "Installed: apr-util-1.5.4-2.fc23.x86_64"
            ]
        }
    }
}

TASK: [Auto Start httpd daemon] ***********************************************
changed: [172.16.33.174]

TASK: [debug var=results] *****************************************************
ok: [172.16.33.174] => {
    "var": {
        "results": {
            "changed": true,
            "enabled": true,
            "invocation": {
                "module_args": "name=httpd enabled=yes",
                "module_complex_args": {},
                "module_name": "service"
            },
            "name": "httpd"
        }
    }
}

PLAY RECAP ********************************************************************
172.16.33.174              : ok=4    changed=2    unreachable=0    failed=0
成功したっぽい。インストール先のChildOneSVの方で確認してみる。
# systemctl list-unit-files | grep httpd
httpd.service                               enabled
httpd.socket                                disabled
もちろんhttpdは動いていない。
# ps -aef | grep httpd
root       2162   1195  0 03:07 pts/0    00:00:00 grep --color=auto httpd
 
動かすところまでしとこう。InstallHttpd.ymlをちょっと編集。
---
- hosts: ChildServers
  gather_facts: no
  tasks:
    - name: install httpd by dnf
      dnf: name=httpd state=latest
      register: results
    - debug: var=results
    - name: Auto Start httpd daemon
      service: name=httpd state=started enabled=yes
      register: results
    - debug: var=results
# ansible-playbook InstallHttpd.yml --private-key=~/.ssh/ChildOneSV

PLAY [ChildServers] ***********************************************************

TASK: [install httpd by dnf] **************************************************
changed: [172.16.33.174]

TASK: [debug var=results] *****************************************************
ok: [172.16.33.174] => {
    "var": {
        "results": {
            "changed": true,
            "invocation": {
                "module_args": "name=httpd state=latest",
                "module_complex_args": {},
                "module_name": "dnf"
            },
            "results": [
                "Installed: httpd-2.4.18-1.fc23.x86_64",
                "Installed: mailcap-2.1.45-1.fc23.noarch",
                "Installed: httpd-tools-2.4.18-1.fc23.x86_64",
                "Installed: httpd-filesystem-2.4.18-1.fc23.noarch",
                "Installed: apr-1.5.2-2.fc23.x86_64",
                "Installed: fedora-logos-httpd-22.0.0-2.fc23.noarch",
                "Installed: apr-util-1.5.4-2.fc23.x86_64"
            ]
        }
    }
}

TASK: [Auto Start httpd daemon] ***********************************************
changed: [172.16.33.174]

TASK: [debug var=results] *****************************************************
ok: [172.16.33.174] => {
    "var": {
        "results": {
            "changed": true,
            "enabled": true,
            "invocation": {
                "module_args": "name=httpd state=started enabled=yes",
                "module_complex_args": {},
                "module_name": "service"
            },
            "name": "httpd",
            "state": "started"
        }
    }
}

PLAY RECAP ********************************************************************
172.16.33.174              : ok=4    changed=2    unreachable=0    failed=0
# ps -aef | grep httpd
root       2283      1  0 03:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2285   2283  0 03:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2286   2283  0 03:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2290   2283  0 03:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2292   2283  0 03:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
apache     2294   2283  0 03:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND
root       2297   1195  0 03:13 pts/0    00:00:00 grep --color=auto httpd
できたできた。次の回はconfigの生成(編集?)をやりたいと思います。

2016年2月12日金曜日

Ansibleを使ってFedora23にサーバを構築していく〜playbook作成

このエントリーをはてなブックマークに追加
これまでの経緯は下のリンク先を参照。


Ansibleを使ってFedora23にサーバを構築していく〜準備編(1)
Ansibleを使ってFedora23にサーバを構築していく〜準備編(2)

構成はこんな感じ。
AnsibleSV →(構成)→ ChildOneSV

ChildOneSVについてはselinuxを無効にしておく。そうでないとPlaybook実行時に怒られるので。

/etc/ansible/hosts(インベントリファイルという)を/etc/ansible/hosts.orgなどリネームし新しく下記の内容で構築先のサーバを指定し作成する。
[ChildServers]
172.16.33.173

~/AnsiblePlayBookみたいな適当なディレクトリを作って、その下にサンプルのplaybook(SamplePlaybook.yml)を作る。
playbookはYAML形式で作成するので、書き方というかお作法はこの辺が参考になった。ansible使いのためのYAML入門

まずデフォルトではansibleの接続にはrootでされるのだが、本当にそうなのかどうか確かめる簡単なplaybookを書いた。
---
- hosts: ChildServers
  tasks:
    - name: Who am I?
      shell: 'whoami'
実行してみる。sshのprivate keyをコマンドの引数に渡しているのがあまりスマートじゃない気がするけど。あと、デバッグ用に-vvvも足している。

# ansible-playbook SamplePlaybook_whoami.yml --private-key=~/.ssh/ChildOneSV -vvv
PLAY [ChildServers] ***********************************************************

GATHERING FACTS ***************************************************************
<172.16.33.174> ESTABLISH CONNECTION FOR USER: root
<172.16.33.174> REMOTE_MODULE setup
<172.16.33.174> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o IdentityFile="/root/.ssh/ChildOneSV" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 172.16.33.174 /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1455208440.15-102322756142433 && echo $HOME/.ansible/tmp/ansible-tmp-1455208440.15-102322756142433'
<172.16.33.174> PUT /tmp/tmpLs6ZDa TO /root/.ansible/tmp/ansible-tmp-1455208440.15-102322756142433/setup
<172.16.33.174> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o IdentityFile="/root/.ssh/ChildOneSV" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 172.16.33.174 /bin/sh -c 'LANG=C LC_CTYPE=C /usr/bin/python /root/.ansible/tmp/ansible-tmp-1455208440.15-102322756142433/setup; rm -rf /root/.ansible/tmp/ansible-tmp-1455208440.15-102322756142433/ >/dev/null 2>&1'
ok: [172.16.33.174]

TASK: [Who am I?] *************************************************************
<172.16.33.174> ESTABLISH CONNECTION FOR USER: root
<172.16.33.174> REMOTE_MODULE command whoami #USE_SHELL
<172.16.33.174> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o IdentityFile="/root/.ssh/ChildOneSV" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 172.16.33.174 /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1455208507.7-272410210390849 && echo $HOME/.ansible/tmp/ansible-tmp-1455208507.7-272410210390849'
<172.16.33.174> PUT /tmp/tmp7LbN5R TO /root/.ansible/tmp/ansible-tmp-1455208507.7-272410210390849/command
<172.16.33.174> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/root/.ansible/cp/ansible-ssh-%h-%p-%r" -o IdentityFile="/root/.ssh/ChildOneSV" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 172.16.33.174 /bin/sh -c 'LANG=C LC_CTYPE=C /usr/bin/python /root/.ansible/tmp/ansible-tmp-1455208507.7-272410210390849/command; rm -rf /root/.ansible/tmp/ansible-tmp-1455208507.7-272410210390849/ >/dev/null 2>&1'
changed: [172.16.33.174] => {"changed": true, "cmd": "whoami", "delta": "0:00:00.003166", "end": "2016-02-12 01:35:08.740448", "rc": 0, "start": "2016-02-12 01:35:08.737282", "stderr": "", "stdout": "root", "warnings": []}

PLAY RECAP ********************************************************************
172.16.33.174              : ok=2    changed=1    unreachable=0    failed=0

最後の一行にちゃんと実行結果でrootと表示されているのがわかった。
changed: [172.16.33.174] => {"changed": true, "cmd": "whoami", "delta": "0:00:00.003166", "end": "2016-02-12 01:35:08.740448", "rc": 0, "start": "2016-02-12 01:35:08.737282", "stderr": "", "stdout": "root", "warnings": []}
しかし、やたら時間がかかるので、少し改良する。サーバ情報などを取得する"GATHERING FACTS"が邪魔なのでgather_facts: nを足す。ついでに-vvvもぬいた
---
- hosts: ChildServers
  gather_facts: no
  tasks:
    - name: Who am I?
      shell: 'whoami'

# ansible-playbook SamplePlaybook_whoami.yml --private-key=~/.ssh/ChildOneSV

PLAY [ChildServers] ***********************************************************

TASK: [Who am I?] *************************************************************
changed: [172.16.33.174]

PLAY RECAP ********************************************************************
172.16.33.174              : ok=1    changed=1    unreachable=0    failed=0

今度は結果が分からない。。。なので標準出力に出るように奮闘してみる。
---
- hosts: ChildServers
  gather_facts: no
  tasks:
    - name: Who am I?
      shell: 'whoami'
      register: result
    - debug: msg="{{result.stdout}}"]

registerresultという変数に結果を格納して、debugが実行結果をdumpするイメージ。debugを使う際にそのまま結果を出すのであればmsgパラメータに"{{result.stdout}}"として渡す。{{}}は変数としてresult.stdoutを出力してくれる。

これでrootで実行されているのは分かったので、dnf updateをしてみる。別にSamplePlaybook.ymlを作成する。
---
- hosts: ChildServers
  gather_facts: no
  tasks:
    - name: OS RPM Update by dnf
      shell: 'dnf -y update'
      register: result
    - debug: msg="{{result.stdout}}"

# ansible-playbook SamplePlaybook.yml --private-key=~/.ssh/ChildOneSV

PLAY [ChildServers] ***********************************************************

TASK: [OS RPM Update by dnf] **************************************************
changed: [172.16.33.174]

TASK: [debug msg="{{result.stdout}}"] *****************************************
ok: [172.16.33.174] => {
    "msg": "Last metadata expiration check performed 1:32:32 ago on Fri Feb 12 01:15:22 2016.\nDependencies resolved.\nNothing to do.\nComplete!"
}

PLAY RECAP ********************************************************************
172.16.33.174              : ok=2    changed=1    unreachable=0    failed=0

なにもすること無かったみたい。。。次は具体的に何かをインストールして環境の変更までやってみたいと思う。