Ansible使ってみる
chefさんとかpuppetさんとかと同じ感じのもの。
chefさんは登場人物が多すぎて、ドキュメントも多すぎてちょっと大変そうなイメージを受けた。
実際に前回軽くさわってみたもののBerkshelfさんも理解しないといけなかったり。。。
ansibleさんはドキュメントが少なくて(15ページないくらい)らしい。
ほんとか?と思いながらも、とっつきやすそうなのでこっちを掘り下げていきたいと考えています。
環境
- マシン: MacBook Pro 2.6 OS X 10.9.3(13D65)
- GHz Intel Core i5
- 8 GB 1600 MHz DDR3
- Vagrant: 1.6.3
- VBoxManage: 4.3.12r93733
- python: 2.7.5
- ゲストOS: CentOS 6.5
いってみよう、やってみよう
いつものようにvagrantさんでcentos6.5を立ち上げて接続。
vagrantさんマジ便利。。。
$ vagrant init centos6.5 $ vagrant up $ vagrant ssh
公式サイトに書いてある感じでインストールする。一応yum update
もしておく。
$ sudo yum -y update $ sudo yum -y install ansible
間違えた。Chef Soloの感覚で入れたけど、こういうものじゃないのか。
ホストからゲストにこれやってちょーってお願いするイメージ。
なのでやり直し。ホストにansibleをインストールする
$ brew install ansible $ ansible --version ansible 1.6.2
続いてinventory fileの作成、対象となるマシンを設定するファイル。
[sample]のように名前を付けてグルーピングすることができる。
ここでポートの指定も可能らしい。
cat > hosts [sample] 192.168.33.10
$ ssh-agent bash $ ssh-add ~/.ssh/id_rsa
さて、試してみる
$ ansible -i hosts all -m ping 192.168.33.10 | FAILED => 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
$ ansible -i hosts all -m ping --user=vagrant --private-key=~/.vagrant.d/insecure_private_key -vvvv 192.168.33.10 | success >> { "changed": false, "ping": "pong" }
毎回指定するのはめんどくさいのでinventory fileに指定しておくとよい。っぽい。
cat > hosts [sample] 192.168.33.10 ansible_ssh_user=vagrant ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key
コマンド実行とパッケージインストールを試す
コマンド実行を試してみる。
$ ansible all -a "/bin/echo hello" -i hosts 192.168.33.10 | success | rc=0 >> hello
・・・ドキュメントわかりやすい!!
そして$ ansible-doc yum
とするとドキュメントみれる。便利ぃ〜。
とりあえずでapacheさんを入れてみる。するとrootじゃなきゃだめよとおこられてしまった。
$ ansible sample -i hosts -m yum -a "name=httpd state=installed" 192.168.33.10 | FAILED >> { "changed": true, "msg": "You need to be root to perform this command.\n", "rc": 1, "results": [ "Loaded plugins: fastestmirror, versionlock\n" ] }
-s
を指定するとsudoしてくれるみたい。お試してみてらできましたー!!
ansible sample -i hosts -s -m yum -a "name=httpd state=installed" 192.168.33.10 | success >> { "changed": true, "msg": "", "rc": 0, "results": [ "Loaded plugins: fastestmirror, versionlock\nLoading mirror speeds from cached hostfile\n * base: ftp.nara.wide.ad.jp\n * epel: kartolo.sby.datautama.net.id\n * extras: ftp.nara.wide.ad.jp\n * updates: ftp.nara.wide.ad.jp\nSetting up Install Process\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.2.15-30.el6.centos will be installed\n--> Processing Dependency: httpd-tools = 2.2.15-30.el6.centos for package: httpd-2.2.15-30.el6.centos.x86_64\n--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-30.el6.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-30.el6.centos.x86_64\n--> Running transaction check\n---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed\n---> Package httpd-tools.x86_64 0:2.2.15-30.el6.centos will be installed\n---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.2.15-30.el6.centos updates 821 k\nInstalling for dependencies:\n apr-util-ldap x86_64 1.3.9-3.el6_0.1 base 15 k\n httpd-tools x86_64 2.2.15-30.el6.centos updates 73 k\n mailcap noarch 2.1.31-2.el6 base 27 k\n\nTransaction Summary\n================================================================================\nInstall 4 Package(s)\n\nTotal download size: 936 k\nInstalled size: 3.1 M\nDownloading Packages:\n--------------------------------------------------------------------------------\nTotal 163 kB/s | 936 kB 00:05 \nRunning rpm_check_debug\nRunning Transaction Test\nTransaction Test Succeeded\nRunning Transaction\n\r Installing : httpd-tools-2.2.15-30.el6.centos.x86_64 1/4 \n\r Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 2/4 \n\r Installing : mailcap-2.1.31-2.el6.noarch 3/4 \n\r Installing : httpd-2.2.15-30.el6.centos.x86_64 4/4 \n\r Verifying : mailcap-2.1.31-2.el6.noarch 1/4 \n\r Verifying : httpd-2.2.15-30.el6.centos.x86_64 2/4 \n\r Verifying : apr-util-ldap-1.3.9-3.el6_0.1.x86_64 3/4 \n\r Verifying : httpd-tools-2.2.15-30.el6.centos.x86_64 4/4 \n\nInstalled:\n httpd.x86_64 0:2.2.15-30.el6.centos \n\nDependency Installed:\n apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 \n httpd-tools.x86_64 0:2.2.15-30.el6.centos \n mailcap.noarch 0:2.1.31-2.el6 \n\nComplete!\n" ] }
もう一回うってみる。既にインストール済みなので"changed": false
。これがベキトウセイというやつなのか???!!!
$ ansible sample -i hosts -s -m yum -a "name=httpd state=installed" 192.168.33.10 | success >> { "changed": false, "msg": "", "rc": 0, "results": [ "httpd-2.2.15-30.el6.centos.x86_64 providing httpd is already installed" ] }
Playbookを作ってみる
まずはapacheをインストールして起動するというPlaybookを作ってみる
$ cat > sample.yml - hosts: sample sudo: yes tasks: - name: be sure httpd is installed yum: name=httpd state=installed - name: be sure httpd is running and enabled service: name=httpd state=running enabled=yes
作った後は$ ansible-playbook -i hosts sample.yml --syntax-check
で構文チェック
タスクの一覧を確認するには--list-tasks
で確認できる
$ ansible-playbook -i hosts sample.yml --list-tasks playbook: sample.yml play #1 (sample): be sure httpd is installed be sure httpd is running and enabled
実行する前に。。。dry run!!!
$ ansible-playbook -i hosts sample.yml --check PLAY [sample] ***************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.33.10] TASK: [be sure httpd is installed] ******************************************** ok: [192.168.33.10] TASK: [be sure httpd is running and enabled] ********************************** changed: [192.168.33.10] PLAY RECAP ******************************************************************** 192.168.33.10 : ok=3 changed=1 unreachable=0 failed=0
実行!!!できた!!!
$ ansible-playbook -i hosts sample.yml PLAY [sample] ***************************************************************** GATHERING FACTS *************************************************************** ok: [192.168.33.10] TASK: [be sure httpd is installed] ******************************************** ok: [192.168.33.10] TASK: [be sure httpd is running and enabled] ********************************** changed: [192.168.33.10] PLAY RECAP ******************************************************************** 192.168.33.10 : ok=3 changed=1 unreachable=0 failed=0
おまけ
cowsayっていうのに対応しているらしい。brew install cowsay
した後にコマンドをたたいてみると面白いです。
export ANSIBLE_COW_SELECTION=dragon
ってしてみると。。。
cowsay -l
で指定可能な一覧が出ます。
_______________ < ENJOY COWSAY > --------------- \ / \ //\ \ |\___/| / \// \\ /0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \ ( //) | \/// | \ \ ( / /) _|_ / ) // | \ _\ ( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-. (( / / )) ,-{ _ `-.|.-~-. .~ `. (( // / )) '/\ / ~-. _ .-~ .-~^-. \ (( /// )) `. { } / \ \ (( / )) .----~-.\ \-' .~ \ `. \^-. ///.----..> \ _ -~ `. ^-` ^-_ ///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~