Skip to content

Instantly share code, notes, and snippets.

@smolin
Last active September 28, 2017 20:52
Show Gist options
  • Save smolin/a7dfbf1fea0a14cda268fac7424b1fa0 to your computer and use it in GitHub Desktop.
Save smolin/a7dfbf1fea0a14cda268fac7424b1fa0 to your computer and use it in GitHub Desktop.
'django startproject' yields simplest working project; this Ansible playbook adds a bunch more steps, mostly from the official tutorial.
# Ansible Playbook
#
# This will create a new Django project with a single app; and will add files and set
# stuff up to demonstrate a Template, a Model, a Migration, and Url includes.
#
# you must have django-admin in your PATH, probably via virtualenv, to make this work
#
# By Steve Molin. GPL.
---
- hosts: all
tasks:
- name: say hello
shell: echo hello world
- name: django startproject
shell: django-admin startproject myproject
args:
creates: myproject
- name: django startapp
shell: cd myproject ; python manage.py startapp myapp
args:
creates: myproject/myapp
- name: add app to project
lineinfile:
dest: myproject/myproject/settings.py
insertbefore: ^MIDDLEWARE
line: INSTALLED_APPS.append('myapp')
- name: get timezone
shell: cat /etc/timezone
register: our_timezone
- name: set timezone
lineinfile:
dest: myproject/myproject/settings.py
regexp: ^TIME_ZONE =
line: TIME_ZONE = '{{ our_timezone.stdout }}'
- name: set up logging
blockinfile:
dest: myproject/myproject/settings.py
block: |
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
},
},
}
- name: add an item to the model
blockinfile:
dest: myproject/myapp/models.py
block: |
class myTable(models.Model):
myCharField = models.CharField(max_length=32)
myDateField = models.DateTimeField('created')
def __str__(self):
return self.myCharField
- name: migrate
shell: python myproject/manage.py makemigrations ; python myproject/manage.py migrate ; touch myproject/migrated
args:
creates: myproject/migrated
- name: populate
shell: python myproject/manage.py shell -c 'from myapp.models import myTable; from django.utils import timezone; x=myTable(myCharField="myValue",myDateField=timezone.now()).save()'
- name: template directory
file:
dest: myproject/myapp/templates/myapp
state: directory
#
# nb quoting: {{ '{{' }} results in single {{ in output
#
- name: add a template
blockinfile:
dest: myproject/myapp/templates/myapp/mytemplate.html
create: yes
marker: <!-- {mark} ANSIBLE MARKED BLOCK -->
block: |
<h1> {{ '{{' }} mytitle {{ '}}' }} </h1>
This page contains:
<hr>
{{ '{{' }} mystuff {{ '}}' }}
<hr>
Last Update:
{{ '{{' }} mydate {{ '}}' }}
- name: add a view function to app
blockinfile:
dest: myproject/myapp/views.py
block: |
from django.http import HttpResponse
from django.shortcuts import render
from .models import myTable
def myview(request):
context = {'mytitle': 'a thrilling title',
'mystuff': myTable.objects.all()[0].myCharField,
'mydate': myTable.objects.all()[0].myDateField,
}
return render(request, 'myapp/mytemplate.html', context)
- name: add a url for myview()
blockinfile:
dest: myproject/myapp/urls.py
create: yes
block: |
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^myview/', views.myview),
]
- name: configure urls, part 1
blockinfile:
dest: myproject/myproject/urls.py
marker: '# {mark} Managed Block from buildDjango.ansible'
block: |
from django.conf.urls import include
from django.views.debug import default_urlconf
urlpatterns.append(url(r'^$', default_urlconf))
urlpatterns.append(url(r'^myurl/', include('myapp.urls')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment