环境:
-
- - - - . . .
- - - - . . .
- - - - . . .
- - - - . . .
一、部署ansible环境
1.安装ansible
[root@lb -01 ~]
[root@lb -01 ~]
2.配置免秘钥
[root@lb -01 ~]# ssh-keygen
[root@lb -01 ~]# for ip in 10 11 12 13 ;do ssh-copy-id -i /root/.ssh/id_rsa.pub 10.0 .0 .$ip ;done
3.配置ansible主机清单
[root@lb -01 ~]
[lb]
10.0 .0 .10
10.0 .0 .11
[client]
10.0 .0 .12
10.0 .0 .13
[root@lb -01 ~]
10.0 .0 .11 | SUCCESS => {
"changed" : false ,
"ping" : "pong"
}
10.0 .0 .12 | SUCCESS => {
"changed" : false ,
"ping" : "pong"
}
10.0 .0 .13 | SUCCESS => {
"changed" : false ,
"ping" : "pong"
}
10.0 .0 .10 | SUCCESS => {
"changed" : false ,
"ping" : "pong"
}
二、nginx环境部署
1.给两台client安装httpd服务
[root@lb -01 ~]
[root@lb -01 ~]
2.关闭所有虚拟机的防火墙
[root@lb -01 ~]
[root@lb -01 ~]
[root@lb -01 ~]
[root@lb -01 ~]
3.发送index.html首页到client
[root@lb -01 test]
{{ansible_default_ipv4.address}}
[root@lb -01 test]
---
- hosts: client
tasks:
- name: scp index.html
template: src=/test/httpd .index.html dest=/var/www /html/index .html
[root@lb -01 test]
查看效果
[root@lb -01 test]
10.0 .0 .12 | SUCCESS | rc=0 >>
10.0 .0 .12
10.0 .0 .13 | SUCCESS | rc=0 >>
10.0 .0 .13
[root@lb -01 test]
10.0 .0 .12
[root@lb -01 test]
10.0 .0 .13
三、安装nginx
1.上传nginx镜像到lb两台虚拟机
[root@lb01 ~]# ansible lb -m shell -a "ls -lh /root/nginx-1.6.2.tar.gz"
10.0 .0 .11 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 786 K Aug 12 15 :17 /root/nginx-1.6 .2 .tar .gz
10.0 .0 .10 | SUCCESS | rc=0 >>
-rw-r--r-- 1 root root 786 K Mar 24 2015 /root/nginx-1.6 .2 .tar .gz
2.安装依赖
[root@lb01 test]
[root@lb01 test]
[root@lb01 test]
3.解压编译安装
[root@lb01 test]# cat nginx.yml
---
- hosts: lb
tasks:
- name: add user
user: name=nginx group=nginx createhome=no shell=/sbin/nologin
- name: tar nginx
unarchive: src=/root/nginx-1.6.2.tar.gz dest=/root
- name: make install
shell: chdir=/root/nginx-1.6.2 ./configure --user=nginx \
--group=nginx --prefix=/usr/local /nginx-1.6.2 \
--with-http_stub_status_module --with-http_ssl_module \
&& make && make install
===>注:ansible-plabook里不能有斜线 需要删掉
[root@lb01 test]# ansible-playbook nginx.yml
4.创建目录与软链接
[root@lb01 ~]# ansible lb -m file -a "path=/application state=directory"
[root@lb01 ~]# ansible lb -m shell -a "ln -s /usr/local/nginx-1.6.2 /application/"
5.设置环境变量,启动nginx
[root@lb01 ~]
[root@lb01 ~]
[root@lb01 ~]
10.0 .0 .11 | SUCCESS | rc=0 >>
tcp 0 0 0 .0 .0 .0 : 80 0 .0 .0 .0 :* LISTEN 8570 /nginx
10.0 .0 .10 | SUCCESS | rc=0 >>
tcp 0 0 0 .0 .0 .0 : 80 0 .0 .0 .0 :* LISTEN 14554 /nginx