现在介绍如何使用ansible安装node。
下面是安装node的信息:
node_dir: /datanode_version: 0.10.21node_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.ymlnode_install├── files│ └── node-0.10.21.tar.gz├── handlers├── meta│ └── main.yml├── tasks│ ├── copy.yml│ ├── delete.yml│ └── main.yml├── templates│ └── app.js└── vars └── main.yml12 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" -kSSH 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.806suser 0m5.550ssys 0m0.550s
2、安装后测试
09:44:15 # ssh 172.17.0.3root@172.17.0.3's password: Last login: Mon Aug 4 09:41:15 2014 from 172.17.42.1root@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 noderoot 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.jsroot 12813 12801 0 09:44 ? 00:00:00 /data/node-0.10.21/bin/node /data/node-0.10.21/app.jsroot 12832 12817 0 09:45 pts/0 00:00:00 grep noderoot@b9a9e6f2caed:~09:45:03 # ll /data/node-0.10.21/total 16-rw-r--r-- 1 root root 166 Aug 4 09:44 app.jsdrwxr-xr-x 2 root root 4096 Jul 3 16:42 bindrwxr-xr-x 4 root root 4096 Oct 19 2013 libdrwxr-xr-x 3 root root 4096 Oct 19 2013 shareroot@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:3301Node install success!
3、删除node
09:47:07 # time ansible-playbook node_delete.yml --extra-vars "host=172.17.0.3 user=root" -kSSH 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.048suser 0m1.853ssys 0m0.244s
4、删除后测试
09:47:21 # ssh 172.17.0.3root@172.17.0.3's password: Last login: Mon Aug 4 09:44:49 2014 from 172.17.42.1root@b9a9e6f2caed:~09:47:54 # ps -ef|grep noderoot 12937 12923 0 09:47 pts/0 00:00:00 grep noderoot@b9a9e6f2caed:~09:47:56 # ll /data/ total 12drwxr-xr-x 4 root root 4096 Jun 26 15:59 mysqldrwxr-xr-x 2 root root 4096 Jun 5 10:21 softdrwxr-xr-x 3 root root 4096 Jul 16 13:37 webroot
如果大家想使用我的例子,可以从github里下载(地址是