Deploy Application on Atomic host with Ansible
Trishna Guha, IRC: trishnag
Software Engineering Intern, Red Hat
Fedora Engineering
DevConf.cz 2017
Atomic Host
Build Docker Image & Container
Ansible Automation
Cockpit
Remote
Manager
$ mkdir flask-helloworld
$ cd flask-helloworld
$ mkdir -p ansible flask-helloworld flask-helloworld/static flask-helloworld/templates
$ touch Dockerfile ansible/{main.yml,inventory,ansible,cfg}
$ touch flask-helloworld/hello-world.py flask-helloworld/static/style.css
$ touch flask-helloworld/templates/{index,master}.html
$ cd ..
$ tree flask-helloworld
flask-helloworld/
├── ansible
│ ├── ansible.cfg
│ ├── inventory
│ └── main.yml
├── Dockerfile
├── flask-helloworld
│ ├── hello_world.py
│ ├── static
│ │ └── style.css
│ └── templates
│ ├── index.html
│ └── master.html
"""
hello-world.py
"""
from flask import Flask, render_template
APP = Flask(__name__)
@APP.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
APP.run(debug=True, host='0.0.0.0')
<!-- master.html -->
<!doctype html>
<html>
<head>
{% block head %}
<title>{% block title %}{% endblock %}</title>
{% endblock %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous">
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link href='http://fonts.googleapis.com/css?family=Lato:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="container">
{% block content %}
{% endblock %}
</div>
</body>
</html>
<!-- index.html -->
{% extends "master.html" %}
{% block title %}Welcome to Flask App{% endblock %}
{% block content %}
<div class="description">
<p>Hello World</p>
</div>
{% endblock %}
/* style.css */
body {
background: #F8A434;
font-family: 'Lato', sans-serif;
color: #FDFCFB;
text-align: center;
position: relative;
bottom: 35px;
top: 65px;
}
.description {
position: relative;
top: 55px;
font-size: 50px;
letter-spacing: 1.5px;
line-height: 1.3em;
margin: -2px 0 45px;
}
# Dockerfile
FROM fedora
MAINTAINER Trishna Guha<tguha@redhat.com>
RUN dnf -y update && dnf -y install python-flask python-jinja2 && dnf clean all
RUN mkdir -p /app
COPY source/ /app/
WORKDIR /app
ENTRYPOINT ["python"]
CMD ["hello-world.py"]
# ansible/ansible.cfg
[defaults]
inventory=inventory
remote_user=<USER>
# USER = fedora [If you are going to use Fedora Atomic host]
[privilege_escalation]
become_method=sudo
become_user=root
# ansible/inventory
[atomic]
<IP_ADDRESS_OF_HOST> ansible_ssh_private_key_file=<'PRIVATE_KEY_FILE'>
# ansible/main.yml
---
- name: Deploy Flask App
hosts: atomic
become: yes
vars:
src_dir: [Source Directory] # /path/to/src_dir of the current host.
dest_dir: [Destination Directory] # /path/to/dest_dir of the remote atomic host.
# src_dir = /home/tguha/DevConf2017/flask-helloworld
# dest_dir = /var/home/fedora/projects/flask-app
tasks:
- name: Create Destination Directory
file:
path: "{{ dest_dir }}/source"
state: directory
recurse: yes
- name: Copy Dockerfile to host
copy:
src: "{{ src_dir }}/Dockerfile"
dest: "{{ dest_dir }}"
- name: Copy Application to host:
copy:
src: "{{ src_dir }}/flask-helloworld/"
dest: "{{ dest_dir }}/source/"
- name: Build Docker Image
command: docker build --rm -t fedora/flask-app:test -f "{{ dest_dir }}/Dockerfile" "{{ dest_dir }}"
- name: Run Docker Container
command: docker run -d --name helloworld -p 5000:5000 fedora/flask-app:test
- name: Start Cockpit Service
command: /usr/bin/atomic run fedora/cockpitws
Questions ?