Create a python development environment with vagrant + ansible + fabric

Create a python development environment with vagrant + ansible + fabric

Like.

I haven't touched on the content that went into it.

It's halfway, but I spent a lot of time because I didn't have all the information. I wonder if it's okay to put together that area. Like tips or addiction.

The final file structure looks like this.

.
├── Vagrantfile
├── ansible.cfg
├── fabfile.py
├── flask
│   ├── main.py
│   ├── reload.trigger
│   ├── runserver.py
│   └── uwsgi.ini
├── inventory
├── playbooks
│   └── python.yml
└── ssh.config

Make a development vm with vagrant

box added

Added a vagrant box with the name centos6.

bash


vagrant box add centos6 http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box

Setting

There is no particular reason, but I'll make two. I want to deploy all at once with rsync.

If you only need one, just node1.

Vagrantfile


# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.define :node1 do |node|
    node.vm.box = "centos6"
    node.vm.network :private_network, ip: "192.168.33.10"

    node.vm.provider :virtualbox do |vb|
        vb.customize ["modifyvm", :id, "--memory", "512"]
    end
  end

  config.vm.define :node2 do |node|
    node.vm.box = "centos6"
    node.vm.network :private_network, ip: "192.168.33.11"

    node.vm.provider :virtualbox do |vb|
        vb.customize ["modifyvm", :id, "--memory", "512"]
    end
  end

end

Prepare ssh-config for vm

It's convenient when connecting with ssh.

bash


vagrant ssh-config > ssh.config

The contents should look like this.

ssh.config


Host node1
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/kuryu/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host node2
  HostName 127.0.0.1
  User vagrant
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/kuryu/.vagrant.d/insecure_private_key
  IdentitiesOnly yes
  LogLevel FATAL

Start-up

bash


vagrant up

Create a python environment with ansible

Since it will be long, I divided the articles. See here. http://qiita.com/arc279/items/44ac688a2df24569f8af

The inventory looks like this.

inventory


[servers]
node1
node2

Although it is not written in the article above, Write the setting that refers to config when sshing with ansible in the ansible configuration file. Also specify the inventory file.

ansible.cfg


[defaults]
hostfile = inventory

[ssh_connection]
ssh_args = -F ssh.config

If you put ansible.cfg in the current state, it will be read without permission.

Execution is like this.

ansible-playbook playbooks/python.yml

Use flask + uwsgi

Body

main.py


# -*- coding:utf-8 -*-

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "hello flask!!"

@app.route("/foo")
def foo():
    return "foo"

@app.route("/foo/bar")
def foo_bar():
    return "foobar"

Werkzeug startup file

runserver.py


#!/usr/bin/env python
# -*- coding:utf-8 -*-

from main import app

options = {
    "debug": True,
    "threaded": True,
    "host": "0.0.0.0",
}
app.run(**options)

If you want to start debugging with Werkzeug, give execution permission to this and hit it.

uwsgi config file

With touch-reload, specify the file to reload uwsgi when touched. If necessary, you should also specify daemonize.

uwsgi.ini


[uwsgi]
socket = /tmp/flask.wsgi.sock
master = true
uid = vagrant
gid = vagrant
http = :5000
python-path = .      #uwsgi startup directory is current
wsgi = main:app
processes = 3
threads = 2
pidfile = ./flask.pid
touch-reload = ./reload.trigger
env = environment=development

It's a very unnatural way of writing, If you specify here in the form of _env = _ __ [key] = [value] __, At startup

import os
print os.environ["environment"]

You can read it at.

start uwsgi

When I made pyenv with ansible in ↑, uwsgi should be included with pip, so

With the current of flask

bash


uwsgi uwsgi.ini

Should start with.

Deploy with fabric + rsync

rsync must be on both the sync side and the sync side.

Since rsync is also included in the ansible article above, If the playbook passes properly, rsync should be on the vm side.

I want to use ansible inventory in fabric

See here for details. http://qiita.com/shivaken/items/c679ae9d15ac1cbebd0b

I want to refer to ssh-config

like this.

env.use_ssh_config = True
env.ssh_config_path = "ssh.config"

ssh-config is specified to connect as vagrant user, so The connection destination __ $ HOME__ is _ / home / vagrant_.

So

rysnc with rsync_project ().

fabfile.py


from fabric.api import *
from fabric.decorators import roles
from fabric.contrib.project import rsync_project
import ansible.inventory

env.use_ssh_config = True
env.ssh_config_path = "ssh.config"

inventory = ansible.inventory.Inventory('inventory')
env.roledefs = inventory.groups_list()

def hostname():
    run("echo %s" % env.host)

def deploy():
    rsync_project(
        local_dir='./flask/',
        remote_dir='~/flask',
        exclude=['.DS_Tore', '*.tmp'],
        delete=True
    )
    reload()

@roles('servers')
def deployall():
    deploy()

def reload():
    run('touch ~/flask/reload.trigger')

fab

Specify the role

Specify role with -R option.

It seems that the group name of the ansible inventory is role.

I haven't investigated this area very well.

bash


fab -R servers deploy

@roles ('servers') Even here with a decorator.

bash


fab deployall

Specify a single host name

bash


fab -H node1 deploy

so

The content has become disorganized.

Deploy to git pull instead of rsync, I think it's okay to make it modern.

If you want to do more

I think there are various things, but that's the point of creating a development environment.

Recommended Posts

Create a python development environment with vagrant + ansible + fabric
Create a virtual environment with Python!
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
[Python] Create a virtual environment with Anaconda
Create a Python environment
Create a simple Python development environment with VS Code and Docker
Build a python environment with ansible on centos6
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Create a python3 build environment with Sublime Text3
Create a Python virtual development environment on Windows
Get a quick Python development environment with Poetry
Create a directory with python
Build a machine learning application development environment with Python
Create a Python development environment on OS X Lion
Set up a Python development environment with Sublime Text 2
Building a kubernetes environment with ansible 2
Create a Python environment on Mac (2017/4)
Building a virtual environment with Python 3
Create a python environment on centos
Building a kubernetes environment with ansible 1
Prepare Python development environment with Atom
Set up a Python development environment with Visual Studio Code
Create a GO development environment with [Mac OS Big Sur]
Get a clean Python development environment with pyenv + pipx + Poetry
How to get into the python development environment with Vagrant
[Pyenv] Building a python environment with ubuntu 16.04
Create a Python execution environment for Windows with VScode + Remote WSL
Create a Python function decorator with Class
Building a Python3 environment with Amazon Linux2
Easily build a development environment with Laragon
[Development environment] Python with Xcode [With screen transition]
Build a blockchain with Python ① Create a class
Create a dummy image with Python + PIL.
Create a python environment on your Mac
Let's create a virtual environment for Python
Let's create a free group with Python
Try to create a python environment with Visual Studio Code & WSL
Building a Python 3.6 environment with Windows + PowerShell
[Python] Create a Batch environment using AWS-CDK
Create Python + uWSGI + Nginx environment with Docker
Create a virtual environment with Python_Mac version
Create a word frequency counter with Python 3.4
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
Build a python virtual environment with pyenv
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Build a modern Python environment with Neovim
Building a Python development environment for AI development
I wrote a script to create a Twitter Bot development environment quickly with AWS Lambda + Python 2.7
Create a USB boot Ubuntu with a Python environment for data analysis
[AWS] Create a Python Lambda environment with CodeStar and do Hello World
Create a Python development environment locally at the fastest speed (for beginners)
Create a Python development environment on Windows (Visual Studio Code remote WSL).
Steps to create a Python virtual environment with VS Code on Windows
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
I tried to build a Mac Python development environment with pythonz + direnv
Macbook Air with M1 is here! Quickly create a Python computing environment
Create a frame with transparent background with tkinter [Python]
Build a C language development environment with a container
Create a Vim + Python test environment in 1 minute