diff --git a/all.yml b/all.yml index 066f38c..51a8c45 100644 --- a/all.yml +++ b/all.yml @@ -1,28 +1,27 @@ --- - hosts: lxd_host tags: container_install - roles: - - lxd_host + roles: [lxd_host] - hosts: haproxy tags: containers, haproxy - roles: - - haproxy + roles: [haproxy] - hosts: - dokuwiki - freshrss - gitweb tags: containers, webserver - roles: - - webserver + roles: [webserver] - hosts: freshrss tags: containers, freshrss - roles: - - freshrss + roles: [freshrss] - hosts: dokuwiki tags: containers, dokuwiki - roles: - - dokuwiki + roles: [dokuwiki] + +- hosts: gallery + tags: containers, gallery + roles: [flask] diff --git a/roles/flask/tasks/main.yml b/roles/flask/tasks/main.yml new file mode 100644 index 0000000..8c88e0d --- /dev/null +++ b/roles/flask/tasks/main.yml @@ -0,0 +1,54 @@ +--- +- name: install packages + apk: + name: + - lighttpd + - py3-flask + - py3-gunicorn + state: installed + +- vars: + web_user: lighttpd + web_group: lighttpd + docroot: /var/www/localhost/htdocs + flask_entry: app.py + lighttpd_conf: /etc/lighttpd/lighttpd.conf + proxy_port: 3031 + + block: + - name: add mod_proxy.conf include to lighttpd.conf + lineinfile: + path: "{{ lighttpd_conf }}" + line: 'include "mod_proxy.conf"' + insertafter: "{{ '# {{{ includes' }}" + + - name: copy mod_proxy.conf template + template: + src: templates/mod_proxy.conf.j2 + dest: /etc/lighttpd/mod_proxy.conf + + - name: copy dummy app template + template: + src: "templates/{{ flask_entry }}.j2" + dest: "{{ docroot }}/{{ flask_entry }}" + mode: 0644 + owner: "{{ web_user }}" + group: "{{ web_group }}" + + - name: copy gunicorn-flask openrc init script + template: + src: "templates/flask_init.j2" + dest: "/etc/init.d/flask" + mode: 0755 + +- name: start and enable custom flask service + service: + name: flask + enabled: yes + state: restarted + +- name: start and enable lighttpd service + service: + name: lighttpd + enabled: yes + state: restarted diff --git a/roles/flask/templates/app.py.j2 b/roles/flask/templates/app.py.j2 new file mode 100644 index 0000000..13e6c85 --- /dev/null +++ b/roles/flask/templates/app.py.j2 @@ -0,0 +1,9 @@ + +from flask import Flask +app = Flask(__name__) + +@app.route("/{{ ansible_facts['hostname'] }}") +@app.route("/{{ ansible_facts['hostname'] }}/") +def hello_world(): + return 'Hello World!' + diff --git a/roles/flask/templates/flask_init.j2 b/roles/flask/templates/flask_init.j2 new file mode 100644 index 0000000..2e2c6e9 --- /dev/null +++ b/roles/flask/templates/flask_init.j2 @@ -0,0 +1,8 @@ +#!/sbin/openrc-run + +command="/usr/bin/gunicorn" +pidfile="/run/${RC_SVCNAME}.pid" +command_args="-w 4 -b 127.0.0.1:{{ proxy_port }} --chdir {{ docroot }} \ + -u {{ web_user }} -g {{ web_group }} app:app" +command_args_background="--daemon -p ${pidfile}" + diff --git a/roles/flask/templates/mod_proxy.conf.j2 b/roles/flask/templates/mod_proxy.conf.j2 new file mode 100644 index 0000000..d0cf76c --- /dev/null +++ b/roles/flask/templates/mod_proxy.conf.j2 @@ -0,0 +1,9 @@ + +server.modules += ("mod_proxy") +$HTTP["url"] =~ "" { + proxy.server = ("" => (( + "host" => "127.0.0.1", + "port" => "{{ proxy_port }}" + ))) +} +