两台虚拟机
192.168.234.135
192.168.234.130
rpm -qa | grep ansible # 检查是否有ansible的包
yum install ansible -y # 安装ansible
ssh-keygen -t rsa # 创建公钥
ssh-copy-id root@192.168.234.135 # 给135拷贝密钥
ssh-copy-id root@192.168.234.130 # 给130拷贝密钥
# 拷贝完之后尝试连接一下任意一台机子
ssh root@192.168.234.130
# 修改配置文件加入ip
vim /etc/ansible/hosts
[webservers] # 名字随便起
192.168.234.135
192.168.234.130
:wq
# ping一下看通不通(绿色就是通了)
ansible webservers -m ping
# 创建nginx目录
mkdir lnmp
# 进入nginx目录
cd lnmp
# 拖包
nginx-1.12.0.tar.gz # nginx包
tinyshopV2.5_data.zip # 电商包
wget http://10.30.5.70:8000/repo.sh
chmod +x repo.sh
# 解压
tar zxf nginx-1.12.0.tar.gz
cp nginx-1.12.0/conf/nginx.conf ./
# 修改nginx配置文件
vim nginx.conf
location / {
root html;
index index.php index.htm;
}
location ~ \.php$ {
root /usr/local/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
---
- hosts: webservers
vars:
- nginx_user: www
tasks:
- name: 配置yum源
script: repo.sh
- name: 安装依赖环境
yum: name=unzip,gcc,pcre-devel,zlib-devel,php,php-mysql,php-gd,mysql,mysql-server,php-fpm state=latest
- name: 推送nginx源码包
unarchive: src=nginx-1.12.0.tar.gz dest=/root/
- name: 安装nginx
shell: cd /root/nginx-1.12.0 && ./configure && make && make install
- name: 推送配置文件
template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf
- name: 创建用户
user: name=www createhome=no shell=/sbin/nologin
- name: 启动nginx
shell: ss -nltp | grep -wq 80 || /usr/local/nginx/sbin/nginx
- name: 上线电商
unarchive: src=tinyshopV2.5_data.zip dest=/usr/local/nginx/html mode=777
- name: 关闭防火墙
service: name=iptables state=stopped
ansible-playbook --syntax-check lnmp.yml
ansible-playbook lnmp.yml
ansible webservers -m service -a 'name=php-fpm state=started enabled=yes'
chmod -R 777 lnmp.yml
ansible webservers -m service -a 'name=mysqld state=started enabled=yes'
ansible webservers -m shell -a 'chmod -R 777 /usr/local/nginx/html'