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

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

0 件のコメント:

コメントを投稿