现在介绍如何使用ansible安装node。
下面是安装node的信息:
node_dir: /data node_version: 0.10.21 node_port: 3301
可以看到node的版本是0.10.21,测试的node应用服务监听3301端口
备注:此playbook仅能对centos或者redhat的6.x版本进行安装。
下面是安装node的playbook结构
09:33:16 # tree node_* node_delete ├── files ├── handlers ├── meta │ └── main.yml ├── tasks │ ├── delete.yml │ └── main.yml ├── templates └── vars └── main.yml node_install ├── files │ └── node-0.10.21.tar.gz ├── handlers ├── meta │ └── main.yml ├── tasks │ ├── copy.yml │ ├── delete.yml │ └── main.yml ├── templates │ └── app.js └── vars └── main.yml 12 directories, 11 files
playbook安装node的是
09:32:54 # cat node_install.yml --- - hosts: "{{host}}" remote_user: "{{user}}" gather_facts: True roles: - common - node_install
playbook删除node的是
09:34:18 # cat node_delete.yml --- - hosts: "{{host}}" remote_user: "{{user}}" gather_facts: True roles: - node_delete
下面是安装与测试过程
1、安装node环境
09:42:34 # time ansible-playbook node_install.yml --extra-vars "host=172.17.0.3 user=root" -k SSH password: PLAY [172.17.0.3] ************************************************************* GATHERING FACTS *************************************************************** ok: [172.17.0.3] TASK: [common | Install initializtion require software] *********************** changed: [172.17.0.3] TASK: [node_install | Copy Node Software To Redhat Client] ******************** changed: [172.17.0.3] TASK: [node_install | Create Node Install Dir] ******************************** ok: [172.17.0.3] TASK: [node_install | Uncompression Node Software To Redhat Client] *********** changed: [172.17.0.3] TASK: [node_install | Mkdir Soft Link To Redhat Client] *********************** changed: [172.17.0.3] => (item=npm) changed: [172.17.0.3] => (item=node) changed: [172.17.0.3] => (item=forever) TASK: [node_install | Copy Node Test Config To Redhat Client] ***************** changed: [172.17.0.3] TASK: [node_install | Start Node Start App In Redhat Client] ****************** changed: [172.17.0.3] TASK: [node_install | Delete Node compression Software In Redhat Client] ****** changed: [172.17.0.3] PLAY RECAP ******************************************************************** 172.17.0.3 : ok=9 changed=7 unreachable=0 failed=0 real 0m17.806s user 0m5.550s sys 0m0.550s
2、安装后测试
09:44:15 # ssh 172.17.0.3 root@172.17.0.3‘s password: Last login: Mon Aug 4 09:41:15 2014 from 172.17.42.1 root@b9a9e6f2caed:~ 09:44:49 # ifconfig eth0 Link encap:Ethernet HWaddr 22:26:1C:DD:5A:1D inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::2026:1cff:fedd:5a1d/64 Scope:Link UP BROADCAST RUNNING MTU:1500 Metric:1 RX packets:41332 errors:0 dropped:0 overruns:0 frame:0 TX packets:38758 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:141178169 (134.6 MiB) TX bytes:11659568 (11.1 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:79 errors:0 dropped:0 overruns:0 frame:0 TX packets:79 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:6335 (6.1 KiB) TX bytes:6335 (6.1 KiB) root@b9a9e6f2caed:~ 09:44:56 # ps -ef|grep node root 12801 1 1 09:44 ? 00:00:00 /data/node-0.10.21/bin/node /data/node-0.10.21/lib/node_modules/forever/bin/monitor /data/node-0.10.21/app.js root 12813 12801 0 09:44 ? 00:00:00 /data/node-0.10.21/bin/node /data/node-0.10.21/app.js root 12832 12817 0 09:45 pts/0 00:00:00 grep node root@b9a9e6f2caed:~ 09:45:03 # ll /data/node-0.10.21/ total 16 -rw-r--r-- 1 root root 166 Aug 4 09:44 app.js drwxr-xr-x 2 root root 4096 Jul 3 16:42 bin drwxr-xr-x 4 root root 4096 Oct 19 2013 lib drwxr-xr-x 3 root root 4096 Oct 19 2013 share root@b9a9e6f2caed:~ 09:45:08 # cat /data/node-0.10.21/app.js var app = require(‘/data/node-0.10.21/lib/node_modules/express/index‘)() app.get(‘/‘, function (req, res) { res.send(‘Node install success!\n‘) }) app.listen(3301) root@b9a9e6f2caed:~ 09:45:29 # curl 172.17.0.3:3301 Node install success!
3、删除node
09:47:07 # time ansible-playbook node_delete.yml --extra-vars "host=172.17.0.3 user=root" -k SSH password: PLAY [172.17.0.3] ************************************************************* GATHERING FACTS *************************************************************** ok: [172.17.0.3] TASK: [node_delete | Stop Node Service In RedHat Client] ********************** changed: [172.17.0.3] TASK: [node_delete | Delete Redis Install Dir In RedHat Client] *************** changed: [172.17.0.3] TASK: [node_delete | Delete Node Soft Link Script] **************************** changed: [172.17.0.3] => (item=npm) changed: [172.17.0.3] => (item=node) changed: [172.17.0.3] => (item=forever) PLAY RECAP ******************************************************************** 172.17.0.3 : ok=4 changed=3 unreachable=0 failed=0 real 0m8.048s user 0m1.853s sys 0m0.244s
4、删除后测试
09:47:21 # ssh 172.17.0.3 root@172.17.0.3‘s password: Last login: Mon Aug 4 09:44:49 2014 from 172.17.42.1 root@b9a9e6f2caed:~ 09:47:54 # ps -ef|grep node root 12937 12923 0 09:47 pts/0 00:00:00 grep node root@b9a9e6f2caed:~ 09:47:56 # ll /data/ total 12 drwxr-xr-x 4 root root 4096 Jun 26 15:59 mysql drwxr-xr-x 2 root root 4096 Jun 5 10:21 soft drwxr-xr-x 3 root root 4096 Jul 16 13:37 webroot
如果大家想使用我的例子,可以从github里下载(地址是https://github.com/dl528888/ansible-examples/tree/master/node_install),然后放到/etc/ansible目录里,下面是内容
本文出自 “吟—技术交流” 博客,请务必保留此出处http://dl528888.blog.51cto.com/2382721/1535152
运维自动化之ansible playbook安装node环境,布布扣,bubuko.com
运维自动化之ansible playbook安装node环境
原文:http://dl528888.blog.51cto.com/2382721/1535152