This section describes how to use ansible to install node.
The following figure shows how to install the node:
node_dir: /datanode_version: 0.10.21node_port: 3301
We can see that the node version is 0.10.21, and the tested node application service listens to port 3301.
Note: This playbook can only be installed in centos or 6.x of RedHat.
The following figure shows the playbook structure for installing node.
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
Which of the following statements is used to install node in Playbook?
09:32:54 # cat node_install.yml ---- hosts: "{{host}}" remote_user: "{{user}}" gather_facts: True roles: - common - node_install
Which of the following operations does playbook delete a node?
09:34:18 # cat node_delete.yml ---- hosts: "{{host}}" remote_user: "{{user}}" gather_facts: True roles: - node_delete
The installation and testing process is as follows:
1. Install the node Environment
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 real0m17.806suser0m5.550ssys0m0.550s
2. Test after installation
09:44:15 # ssh 172.17.0.3[email protected]‘s password: Last login: Mon Aug 4 09:41:15 2014 from 172.17.42.1[email protected]:~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)[email protected]:~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 node[email protected]:~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 share[email protected]:~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)[email protected]:~09:45:29 # curl 172.17.0.3:3301Node install success!
3. delete a 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 real0m8.048suser0m1.853ssys0m0.244s
4. Test after deletion
09:47:21 # ssh 172.17.0.3[email protected]‘s password: Last login: Mon Aug 4 09:44:49 2014 from 172.17.42.1[email protected]:~09:47:54 # ps -ef|grep noderoot 12937 12923 0 09:47 pts/0 00:00:00 grep node[email protected]:~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
If you want to use my example, you can download it from GitHub (the address is https://github.com/dl528888/ansible-examples/tree/master/node_install) and put it in the/etc/ansible directory. below is the content
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/43/F0/wKioL1Pe6G2jcB4NAAHNubjSl98344.jpg "Title =" 8.jpg" alt = "wkiol1pe6g2jcb4naahnubjsl98344.jpg"/>
This article is from the "Yin-Technical Exchange" blog, please be sure to keep this source http://dl528888.blog.51cto.com/2382721/1535152