ラベル Ansible の投稿を表示しています。 すべての投稿を表示
ラベル Ansible の投稿を表示しています。 すべての投稿を表示

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

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

2016年2月10日水曜日

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

このエントリーをはてなブックマークに追加
新しくもう一台VMを起動し2台のVM構成を作る。
AnsibleSV →(構成)→ ChildOneSV
両サーバ間はSSHの鍵認証を使ってログインした後通信を行うので、AnsibleSVからChildOneSVへSSHログインできる鍵を作っておく。ここの具体的な手順はぐぐったら腐るほどでてくるので省略。

AnsibleSVの構築はAnsibleを使ってFedora23にサーバを構築していく〜準備編を参照。ちなみに構成先のChildOneSVにもPythonをインストールしておく。

まずはMotherSVからChildOneSVへの疎通確認をしてみる。Ansibleで通信するためには相手先のアドレスを/etc/ansible/hostsに書いておく必要がある。-m でansibleのモジュール、--private-keyでChiledOneSVにログインするための秘密鍵を指定する。今回使うのはpingモジュール。
# ansible <ChildOneSV IPAddress> -m ping --private-key=~/.ssh/ChildOneSV
<ChildOneSV IPAddress> | success >> {
    "changed": false,
    "ping": "pong"
}
ChildOneSVでのコマンドをAnsible経由で実行してみる。-a は引数付きのコマンド実行。
# ansible 172.16.33.173 -a 'uname -a' --private-key=~/.ssh/ChildOneSV
172.16.33.173 | success | rc=0 >>
Linux ChildOneSV 4.3.5-300.fc23.x86_64 #1 SMP Mon Feb 1 03:18:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
これで通信は確認できたので、次は実際にPlaybookを作ってみたいと思う。

2016年2月9日火曜日

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

このエントリーをはてなブックマークに追加
サーバの自動構築。なんという夢の響きだろうか。あのめんどくさい作業から解放されるためなんだったら何でもやろう。という事でAnsibleを使ってやってみることにした。Chefはなんかめんどくさそうなイメージが果てしなくあったので何となくAnsibleを選んだ。


今回はVMWare Fusionを使って試すことにした。まずは準備としてFedora23 Server EditionのMinimalインストールする。そしてすぐにシャットダウンし、この.vmwareファイルをオリジナルファイル(以下、OriginalVM)として保存。それをコピーしAnsible用のマシン(以下、AnsibleVM)として構築していく。

以降、操作するのは全てAnsibleVMの方なので注意すること。

ターミナルの方がやりやすいのでiTerm2でAnsibleVMへ接続し、iTermでログの取得(Shell→Log→Start)をしておく。この方が後々役にたつはず。

$ ssh root@xxx.xxx.xxx.xxx
dnfでできるだけ早いミラーサイトからパッケージを取ってくるために/etc/dnf/dnf.confに追記。
[main]
gpgcheck=1
installonly_limit=3
clean_requirements_on_remove=true
fastestmirror=true
ここでdnfでアップデート。
# dnf update
メタデータの期限切れの確認は、0:51:03 前の Mon Feb  8 03:24:13 2016 に実施しました。
依存性が解決されました。
========================================================================================================================
 パッケージ                            アーキテクチャ   バージョン                              リポジトリ         容量
========================================================================================================================
インストール中:
 kernel                                x86_64           4.3.4-300.fc23                          updates            52 k
 kernel-core                           x86_64           4.3.4-300.fc23                          updates            20 M
 kernel-modules                        x86_64           4.3.4-300.fc23                          updates            18 M
 libsecret                             x86_64           0.18.3-1.fc23                           fedora            156 k
アップグレード中:
 GeoIP-GeoLite-data                    noarch           2015.12-1.fc23                          updates           367 k
 NetworkManager                        x86_64           1:1.0.10-2.fc23                         updates           1.9 M
 NetworkManager-libnm                  x86_64           1:1.0.10-2.fc23                         updates           481 k
 audit                                 x86_64           2.4.5-1.fc23                            updates           243 k
 audit-libs                            x86_64           2.4.5-1.fc23                            updates            95 k
 avahi-autoipd                         x86_64           0.6.32-0.4.rc.fc23                      updates            31 k
 avahi-libs                            x86_64           0.6.32-0.4.rc.fc23                      updates            62 k
 bash                                  x86_64           4.3.42-3.fc23                           updates           1.4 M
 bind99-libs                           x86_64           9.9.8-2.P3.fc23                         updates           655 k
 bind99-license                        noarch           9.9.8-2.P3.fc23                         updates            12 k
 bzip2-libs                            x86_64           1.0.6-19.fc23                           updates            45 k
 ca-certificates                       noarch           2015.2.6-1.0.fc23                       updates           432 k
 chkconfig                             x86_64           1.7-1.fc23                              updates           179 k
 coreutils                             x86_64           8.24-5.fc23                             updates           2.8 M
 crypto-policies                       noarch           20151104-1.gitf1cba5f.fc23              updates            30 k
 curl                                  x86_64           7.43.0-5.fc23                           updates           285 k
 dbus                                  x86_64           1:1.10.6-1.fc23                         updates           244 k
 dbus-glib                             x86_64           0.106-1.fc23                            updates           121 k
 dbus-libs                             x86_64           1:1.10.6-1.fc23                         updates           173 k
 deltarpm                              x86_64           3.6-13.fc23                             updates            88 k
 device-mapper                         x86_64           1.02.109-2.fc23                         updates           292 k
 device-mapper-event                   x86_64           1.02.109-2.fc23                         updates           206 k
 device-mapper-event-libs              x86_64           1.02.109-2.fc23                         updates           209 k
 device-mapper-libs                    x86_64           1.02.109-2.fc23                         updates           346 k
 dhcp-client                           x86_64           12:4.3.3-8.P1.fc23                      updates           300 k
 dhcp-common                           noarch           12:4.3.3-8.P1.fc23                      updates           194 k
 dhcp-libs                             x86_64           12:4.3.3-8.P1.fc23                      updates           138 k
 dnf                                   noarch           1.1.6-2.fc23                            updates           290 k
 dnf-conf                              noarch           1.1.6-2.fc23                            updates            91 k
 dnf-plugins-core                      noarch           0.1.16-1.fc23                           updates            37 k
 dnf-yum                               noarch           1.1.6-2.fc23                            updates            85 k
 dnsmasq                               x86_64           2.75-3.fc23                             updates           294 k
 dracut                                x86_64           043-63.git20151211.fc23                 updates           324 k
 dracut-config-rescue                  x86_64           043-63.git20151211.fc23                 updates            45 k
 ebtables                              x86_64           2.0.10-18.fc23                          updates           126 k
 elfutils-default-yama-scope           noarch           0.165-2.fc23                            updates            38 k
 elfutils-libelf                       x86_64           0.165-2.fc23                            updates           213 k
 elfutils-libs                         x86_64           0.165-2.fc23                            updates           272 k
 emacs-filesystem                      noarch           1:24.5-7.fc23                           updates            65 k
 file                                  x86_64           5.22-6.fc23                             updates            66 k
 file-libs                             x86_64           5.22-6.fc23                             updates           423 k
 findutils                             x86_64           1:4.5.16-1.fc23                         updates           563 k
 gettext                               x86_64           0.19.7-3.fc23                           updates           1.0 M
 gettext-libs                          x86_64           0.19.7-3.fc23                           updates           504 k
 glib2                                 x86_64           2.46.2-1.fc23                           updates           2.3 M
 glibc                                 x86_64           2.22-7.fc23                             updates           3.6 M
 glibc-common                          x86_64           2.22-7.fc23                             updates            11 M
 gnupg2                                x86_64           2.1.9-1.fc23                            updates           1.8 M
 gnutls                                x86_64           3.4.8-1.fc23                            updates           657 k
 grep                                  x86_64           2.22-6.fc23                             updates           275 k
 grub2                                 x86_64           1:2.02-0.25.fc23                        updates           3.8 M
 grub2-tools                           x86_64           1:2.02-0.25.fc23                        updates           7.1 M
 gsettings-desktop-schemas             x86_64           3.18.1-1.fc23                           updates           518 k
 hawkey                                x86_64           0.6.2-3.fc23                            updates            92 k
 hwdata                                noarch           0.285-2.fc23                            updates           1.4 M
 initscripts                           x86_64           9.65-1.fc23                             updates           406 k
 ipcalc                                x86_64           0.1.4-2.fc23                            updates            31 k
 iproute                               x86_64           4.1.1-3.fc23                            updates           598 k
 iputils                               x86_64           20140519-7.fc23                         updates           161 k
 kbd                                   x86_64           2.0.2-8.fc23                            updates           379 k
 kbd-legacy                            noarch           2.0.2-8.fc23                            updates           476 k
 kbd-misc                              noarch           2.0.2-8.fc23                            updates           1.4 M
 kmod                                  x86_64           22-2.fc23                               updates           112 k
 kmod-libs                             x86_64           22-2.fc23                               updates            61 k
 krb5-libs                             x86_64           1.14-7.fc23                             updates           839 k
 libarchive                            x86_64           3.1.2-14.fc23                           updates           322 k
 libassuan                             x86_64           2.4.2-1.fc23                            updates            75 k
 libblkid                              x86_64           2.27.1-2.fc23                           updates           180 k
 libcurl                               x86_64           7.43.0-5.fc23                           updates           256 k
 libdrm                                x86_64           2.4.66-1.fc23                           updates           144 k
 libfdisk                              x86_64           2.27.1-2.fc23                           updates           221 k
 libgcc                                x86_64           5.3.1-2.fc23                            updates            88 k
 libgomp                               x86_64           5.3.1-2.fc23                            updates           152 k
 libgpg-error                          x86_64           1.21-1.fc23                             updates           154 k
 libmount                              x86_64           2.27.1-2.fc23                           updates           197 k
 libnghttp2                            x86_64           1.6.0-1.fc23                            updates            65 k
 libnl3                                x86_64           3.2.27-1.fc23                           updates           265 k
 libpng                                x86_64           2:1.6.19-2.fc23                         updates           118 k
 libreport-filesystem                  x86_64           2.6.4-1.fc23                            updates            46 k
 libsmartcols                          x86_64           2.27.1-2.fc23                           updates           139 k
 libsolv                               x86_64           0.6.14-7.fc23                           updates           346 k
 libsoup                               x86_64           2.52.2-1.fc23                           updates           385 k
 libssh2                               x86_64           1.6.0-3.fc23                            updates           147 k
 libstdc++                             x86_64           5.3.1-2.fc23                            updates           425 k
 libuuid                               x86_64           2.27.1-2.fc23                           updates            79 k
 libxml2                               x86_64           2.9.3-2.fc23                            updates           684 k
 linux-firmware                        noarch           20151214-60.gitbbe4917c.fc23            updates            32 M
 lua                                   x86_64           5.3.2-2.fc23                            updates           294 k
 lvm2                                  x86_64           2.02.132-2.fc23                         updates           1.0 M
 lvm2-libs                             x86_64           2.02.132-2.fc23                         updates           927 k
 nettle                                x86_64           3.2-1.fc23                              updates           306 k
 nspr                                  x86_64           4.10.10-2.fc23                          updates           134 k
 nss                                   x86_64           3.21.0-1.1.fc23                         updates           860 k
 nss-softokn                           x86_64           3.21.0-1.1.fc23                         updates           314 k
 nss-softokn-freebl                    x86_64           3.21.0-1.1.fc23                         updates           207 k
 nss-sysinit                           x86_64           3.21.0-1.1.fc23                         updates            56 k
 nss-tools                             x86_64           3.21.0-1.1.fc23                         updates           493 k
 nss-util                              x86_64           3.21.0-1.0.fc23                         updates            81 k
 openssh                               x86_64           7.1p2-3.fc23                            updates           430 k
 openssh-clients                       x86_64           7.1p2-3.fc23                            updates           635 k
 openssh-server                        x86_64           7.1p2-3.fc23                            updates           461 k
 openssl                               x86_64           1:1.0.2f-1.fc23                         updates           496 k
 openssl-libs                          x86_64           1:1.0.2f-1.fc23                         updates           1.2 M
 os-prober                             x86_64           1.70-1.fc23                             updates            48 k
 p11-kit                               x86_64           0.23.2-1.fc23                           updates           151 k
 p11-kit-trust                         x86_64           0.23.2-1.fc23                           updates           131 k
 pcre                                  x86_64           8.38-4.fc23                             updates           495 k
 pinentry                              x86_64           0.9.6-4.fc23                            updates            81 k
 plymouth                              x86_64           0.8.9-11.2013.08.14.fc23                updates           113 k
 plymouth-core-libs                    x86_64           0.8.9-11.2013.08.14.fc23                updates           100 k
 plymouth-scripts                      x86_64           0.8.9-11.2013.08.14.fc23                updates            38 k
 policycoreutils                       x86_64           2.4-18.fc23                             updates           922 k
 python3-dbus                          x86_64           1.2.0-12.fc23                           updates           121 k
 python3-dnf                           noarch           1.1.6-2.fc23                            updates           454 k
 python3-dnf-plugins-core              noarch           0.1.16-1.fc23                           updates            80 k
 python3-gobject-base                  x86_64           3.18.2-1.fc23                           updates           296 k
 python3-hawkey                        x86_64           0.6.2-3.fc23                            updates            73 k
 python3-sssdconfig                    noarch           1.13.3-3.fc23                           updates           100 k
 rpm                                   x86_64           4.13.0-0.rc1.11.fc23                    updates           510 k
 rpm-build-libs                        x86_64           4.13.0-0.rc1.11.fc23                    updates           116 k
 rpm-libs                              x86_64           4.13.0-0.rc1.11.fc23                    updates           294 k
 rpm-plugin-selinux                    x86_64           4.13.0-0.rc1.11.fc23                    updates            51 k
 rpm-plugin-systemd-inhibit            x86_64           4.13.0-0.rc1.11.fc23                    updates            51 k
 rpm-python3                           x86_64           4.13.0-0.rc1.11.fc23                    updates           100 k
 selinux-policy                        noarch           3.13.1-158.4.fc23                       updates           435 k
 selinux-policy-targeted               noarch           3.13.1-158.4.fc23                       updates           5.6 M
 sqlite                                x86_64           3.10.2-1.fc23                           updates           476 k
 sudo                                  x86_64           1.8.15-1.fc23                           updates           691 k
 systemd                               x86_64           222-14.fc23                             updates           6.3 M
 systemd-libs                          x86_64           222-14.fc23                             updates           474 k
 tzdata                                noarch           2016a-1.fc23                            updates           409 k
 util-linux                            x86_64           2.27.1-2.fc23                           updates           2.1 M
 xkeyboard-config                      noarch           2.16-2.fc23                             updates           761 k

トランザクションの要約
========================================================================================================================
インストール      4 Packages
アップグレード  133 Packages

総ダウンロード容量: 156 M

これでパッケージのアップデートは完了したので、Ansibleをdnfでインストールすることにした。いつもは最新版が欲しいのでソースから入れる派なんだけど、Fedoraだしその辺はそれなりに新しいパッケージが入るだろうという事で。

# dnf install ansible
メタデータの期限切れの確認は、0:33:38 前の Tue Feb  9 00:39:37 2016 に実施しました。
依存性が解決されました。
========================================================================================================================
 Package                           アーキテクチャ         バージョン                      リポジトリ               容量
========================================================================================================================
インストール:
 PyYAML                            x86_64                 3.11-11.fc23                    updates                 168 k
 ansible                           noarch                 1.9.4-1.fc23                    updates                 1.7 M
 libtomcrypt                       x86_64                 1.17-25.fc23                    fedora                  229 k
 libtommath                        x86_64                 0.42.0-6.fc23                   fedora                   39 k
 libyaml                           x86_64                 0.1.6-7.fc23                    fedora                   58 k
 python                            x86_64                 2.7.10-8.fc23                   fedora                   94 k
 python-babel                      noarch                 1.3-8.fc23                      fedora                  2.5 M
 python-crypto                     x86_64                 2.6.1-7.fc23                    fedora                  468 k
 python-ecdsa                      noarch                 0.11-4.fc23                     fedora                   73 k
 python-httplib2                   noarch                 0.9.1-2.fc23                    fedora                  119 k
 python-jinja2                     noarch                 2.8-2.fc23                      fedora                  464 k
 python-keyczar                    noarch                 0.71c-5.fc23                    fedora                  222 k
 python-libs                       x86_64                 2.7.10-8.fc23                   fedora                  5.8 M
 python-markupsafe                 x86_64                 0.23-7.fc23                     fedora                   35 k
 python-paramiko                   noarch                 1.15.2-3.fc23                   fedora                  254 k
 python-pip                        noarch                 7.1.0-1.fc23                    fedora                  1.5 M
 python-pyasn1                     noarch                 0.1.8-1.fc23                    fedora                  104 k
 python-setuptools                 noarch                 18.0.1-2.fc23                   fedora                  425 k
 python-six                        noarch                 1.9.0-3.fc23                    fedora                   34 k
 pytz                              noarch                 2015.4-1.fc23                   fedora                   60 k
 sshpass                           x86_64                 1.05-8.fc23                     fedora                   25 k

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

総ダウンロード容量: 14 M
インストール済み容量: 58 M

確認してみる。
# ansible --version
ansible 1.9.4

できた、できた。