1、代码如下:
vim lnmp.py
------------------------------------------>
#!/usr/bin/env python from fabric.colors import * from fabric.api import * env.user = 'root' env.roledefs = { 'node1': ['172.16.1.207'], 'node2': ['172.16.1.208'] } env.passwords = { 'root@172.16.1.207:22': 'redhat', 'root@172.16.1.208:22': 'redhat' } @roles('node1') def webtask(): print yellow("Install nginx php php-fpm...") with settings(warn_only=True): run("apt-get install nginx -y")
run("apt-get install mysql-server -y") run("apt-get install php php-fpm php-mysql php-mbstring php-xml php-mcrypt php-gd -y") run("service nginx start") run("service php-fpm start") @roles('node2') def dbtask(): print yellow("Install Mysql...") with settings(warn_only=True): run("apt-get install mysql-server -y") run('service mysql restart') @roles('node1','node2') def publictask(): print yellow("Install epel ntp...") with settings(warn_only=True): run("apt-get install ntp -y") def deploy(): execute(publictask) execute(webtask) execute(dbtask)
2、执行文件
fab -f lnmp.py deploy