Skip to content

Commit

Permalink
add hello ansible and hello ansible form route
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed May 2, 2020
1 parent 9a1d4a4 commit b57e186
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ http://myhost:8001/queue
http://myhost:8001/originate/v1?dest_exten=1234&dest_context=my-dest-context&src_exten=777&src_context=my-src-context
http://myhost:8001/originate/v1?dest_exten=1234
```
### Sample url
```
http://myhost:8001/hello_ansible # lanch a example playbook
http://myhost:8001/hello_ansible_form # lanch a exampe playbook loading variable from a form
```
3 changes: 3 additions & 0 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[flask]
SECRET_KEY = 7d441f27d441f27567d441f2b6176a

[default]
network_whitelist = 192.168.0.0/16
default_tennant_context = thym-prod-key1991-internal
Expand Down
9 changes: 9 additions & 0 deletions playbooks/hello_ansible.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Hello ansible
hosts: 127.0.0.1
connection: local

tasks:
- name: Print debug message
debug:
msg: "Hello ansible !"
9 changes: 9 additions & 0 deletions playbooks/hello_ansible_form.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Hello ansible
hosts: 127.0.0.1
connection: local

tasks:
- name: Print debug message
debug:
msg: "Hello ansible, this is my var: {{ sample_var }}"
8 changes: 7 additions & 1 deletion potoo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from flask import Flask
from potoo.utils import get_config

app = Flask(__name__)
config = get_config()

app = Flask(__name__,root_path='potoo')
app.config.from_object(__name__)
app.config['SECRET_KEY'] = config['flask']['SECRET_KEY']

from potoo.routes import *

12 changes: 12 additions & 0 deletions potoo/routes/hello_ansible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ansible_runner
from potoo import app
from potoo.utils import *

@app.route("/hello_ansible")
def hello_ansible():

PLAYBOOK_FILE = os.path.join(os.getcwd(), "playbooks/hello_ansible.yml")

r = ansible_runner.run(playbook=PLAYBOOK_FILE, extravars={"my_var":"content"})

return r.stats
27 changes: 27 additions & 0 deletions potoo/routes/hello_ansible_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ansible_runner
from flask import Flask, render_template, flash, request
from wtforms import Form, TextField, TextAreaField, validators, StringField, SubmitField
from potoo import app
from potoo.utils import *

class helloAnsibleForm(Form):
sample_var = TextField('Sample var:', validators=[validators.required()])

@app.route("/hello_ansible_form", methods=['GET', 'POST'])
def hello():
PLAYBOOK_FILE = os.path.join(os.getcwd(), "playbooks/hello_ansible_form.yml")
form = helloAnsibleForm(request.form)

if request.method == 'POST':
sample_var=request.form['sample_var']

if form.validate():
extravars = {"sample_var": sample_var}
r = ansible_runner.run(playbook=PLAYBOOK_FILE, extravars=extravars)
flash('Sample var: ' + sample_var)
flash("{}: {}".format(r.status, r.rc))
flash(r.stats)
else:
flash('Error: All the form fields are required.')

return render_template('hello_ansible_form.html', form=form)
17 changes: 17 additions & 0 deletions potoo/templates/hello_ansible_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<title>Hello ansible form</title>

{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message[1] }}</li>
{% endfor %}</ul>
{% endif %}
{% endwith %}
<form action="" method="post">
{{ form.csrf }}
<div class="input text">
{{ form.sample_var.label }} {{ form.sample_var }}</div>
<div class="input submit">
<input type="submit" value="Submit"></div>
</form>
13 changes: 13 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
ansi2html==1.5.2
ansible==2.9.7
ansible-runner==1.4.6
cffi==1.14.0
click==7.1.1
cryptography==2.9.2
docutils==0.16
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.2
lockfile==0.12.2
MarkupSafe==1.1.1
pexpect==4.8.0
pkg-resources==0.0.0
psutil==5.7.0
ptyprocess==0.6.0
pycparser==2.20
python-daemon==2.2.4
PyYAML==5.3.1
six==1.14.0
Werkzeug==1.0.1
WTForms==2.3.1

0 comments on commit b57e186

Please sign in to comment.