RUNNING FLASK WEB SERVER ON VIRTUALBOX USING ANSIBLE AND VAGRANT
Vagrant is a tool that allows the application run independently of the platform. Vagrant does this with tools that include virtualization technology such as VirtualBox and WMware. There are a wide range of use cases, but we will use VirtualBox as a provider in our example here. As a DevOps Engineer, we can easily run it with Dockerized structures in a way that is platform-independent, but today we will do it with Vagrant.
With Vagrant, we can create our own environment independently of Ansible, but we will create the necessary environment with Ansible for diversity. Of course, we’ll do it to show off our DevOps skills.
Tools to be installed:
1) Vagrant( Here you can download the one that is suitable for your operating system. https://www.vagrantup.com/downloads )
After installation, check with vagrant — version
.
2) Ansible ( Here you can download the one that is suitable for your operating system. https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html )
After installation, check with ansible — version
.
3) VirtualBox ( Here you can download the one that is suitable for your operating system. https://www.virtualbox.org/wiki/Downloads )
Let’s start preparing.
1)First, we prepare the Vagrantfile
necessary for the vagrant to work. Let’s create an empty folder and create the file in the terminal and run the vagrant init
command. In this way, you will basically see the lines required to run vagrant and comments that show other features to use on it. Since our file content is already ready, let’s delete all the contents and paste the following lines into Vagrantfile
.
Vagrant.configure("2") do |config|config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "private_network", ip: "192.165.33.10"
config.vm.define "trusty" do |node|
node.vm.hostname = "trusty.local"
node.vm.provision "ansible" do |ansible|
ansible.playbook = "main.yml"
end
endconfig.vm.provider "virtualbox" do |vb|vb.memory = "512"
vb.cpus = "1"
endconfig.vm.provision "shell", inline: <<-SHELL
apt-get update
SHELL
end
2)Let’s prepare a simple Flask file called as test.py
. Paste the following lines into it.
from flask import Flaskapp=Flask(__name__)@app.route("/")
def test():
return ("CONGRATULATIONS. If you can see this page, it means that you have completed it successfully.")if __name__=="__main__":
app.run(host="0.0.0.0", port=80)
3)In the final stage, we create a playbook called main.yml
, which will create the necessary environment. Paste the following lines into it.
---
- hosts: trusty
gather_facts: no
become: yes
tasks:
- name: Copy your flask app file to the Vagrant servers
copy:
src: /$PWD/test.py
dest: /home/vagrant/test.py
- name: installing pip3
apt:
name: python3-pip
update_cache: yes
- name: Installing flask module
pip:
executable: pip3
name: flask
state: latest
- name: run flask app
shell: |
nohup python3 test.py > log.txt 2>&1 &
LAST STEP: We go to the Vagrantfile
location in the terminal and run the vagrant up
command.
After completing all steps, open any browser and type the private IP (192.168.33.10) to see the result.
If you want to connect to the machine at the terminal, run the vagrant ssh
command. Run the exit
command to exit again.
Run vagrant destroy
to delete the machine.
This study is my first article on this platform. I hope it has been useful for you. Happy studying.
You can copy content from my GitHub account.
GitHub URL: https://github.com/mehmetakifdevops/medium