Ansible一键部署Flask(nginx+Uwsgi)

问题:在一台安装有Ansible的主机上如何一键部署Flask到另外一台centos系统的主机?

    前提:两台主机网络互通(参考“Ansible入门基础"进行配置)

一,新建目录app和其子目录conf,目录app用于存放Flaks项目文件(.py),uwsgi配置文件(.ini),playbook运行文件(.yml),子目录conf用于存放nginx配置文件(.conf)

1.

准备目录创建Flask文件
>mkdir app
>cd app
>vim hello.py           #创建Flask 项目hello.py
from flaskimportFlask
app = Flask(__name__)
 
 
@app.route(‘/’)
def index():
 return'<h1>Hello World!!'
 
 
if __name__ =="__main__":
 app.run(port=8088)


2.

创建uwsgi配置文件

>vim test_uwsgi.ini
[uwsgi]
socket =127.0.0.1:8088  
chdir = /root/app
processes =4
threads =2
master =true
pythonpath = /root/app         #Flask项目所在文件路径
module = hello                      #因为flask文件名为hello.py
callable = app                       #因为Flask项目文件中 app = Flask(__name__)
memory-report =true
daemonize =127.0.0.1:8088    #配置让uwsgi在后台运行
 

 

3.

创建nginx配置文件

>mkdir conf          #在app目录下创建conf文件,用于存放nginx.conf配置文件
>cd conf
>vim nginx.conf      #nginx 配置文件
include  /etc/nginx/conf.d/*.conf;
server {
    listen        80default_server;
    listen          [::]:80default_server;
    server_name   _;
     include  /etc/nginx/default.d/*.conf;
     location /{
         include  uwsgi_params:
         uwsgi_pass    127.0.0.1:8088    #IP和端口要求与uwsgi配置文件里面的socket值一致
        }
}

 

4.

回到app目录下,创建playbook文件

>cd ..
>vim test_build.yml         #ansible执行的playbook文件
---
- name: nginx and uwsgi build Flask
  hosts:testservers                                                                   
  remote_user: root
  tasks:
       - name: install nginx and uwsgi and uwsgi-plugin-python
       action: yum name={{ item.name }} state=present
       with_items:
              - { name:'nginx'}
              - { name:'uwsgi'}
              - { name:'uwsgi-plugin-python'}
        - name: create directory
          file: state=directory dest=/root/testb/conf
        - name: copy file
          copy: src={{ item.src }} dest={{ item.dest }}
          with_items:
               - { src:'/root/app/test_uwsgi.ini',dest: '/root/testb/test_uwsgi.ini'}
               - { src:'/root/app/hello.py', dest:'/root/testb/hello.py'}
               - { src:'/root/app/conf/nginx.conf', dest:'/root/testb/conf/nginx.conf'}
          - name: build flask
            command: uwsgi --http-socket127.0.0.1:8088--plugin python --ini /root/testb/test_uwsgi.ini
  handlers:         #handler 启动nginx
     - name: start nginx
       command: /root/nginx-1.2.7-c /root/app/conf/nginx.conf   #启动nginx指令为:nginx安装目录地址 -c nginx配置文件地址

 

5.

运行playbook文件test_build.yml
>ansible-playbook  test_build.yml

 运行结果截图:


6.

检测是否搭建成功
>curl127.0.0.1:8088


 返回 Hello World!!  即成功


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Q_QTowerQ_Q/article/details/78303681
个人分类: Ansible
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭