[project-hosts] root ansible_ssh_host=192.168.0.102 ansible_ssh_user=freylis ansible_ssh_pass=z ansible_sudo_pass=z user ansible_ssh_host=192.168.0.102 ansible_ssh_user=example2 ansible_ssh_pass=zz [user-hosts] user [root-hosts] root
# # system options # # linux username username: # about password crypt # http://docs.ansible.com/faq.html#how-do-i-generate-crypted-passwords-for-the-user-module # or run `mkpasswd --method=SHA-512` # here crypted user_crypt_password: # really password user_password: z user_homedir: "/home/{{ username }}" mysql_root_user: root # root mysql user mysql_root_password: "" # # project options # # project slug ( if u have `example/manage.py` and example/example/settings.py` - `example` is project_slug) project_slug: # url or list urls for nginx project_url: project_dir: "{{ user_homedir }}/projects/{{ project_slug }}" project_homedir: "{{ user_homedir }}/projects/{{ project_slug }}/{{ project_slug }}" # virtualenv name env: "{{ project_dir }}/env" # port for uwsgi, must be unique for each project uwsgi_port: 9000 # mysql database for current project mysql_database: "{{ project_slug }}" # mysql user for current project mysql_user: # mysql user password for current project mysql_user_password: # # django settings # debug: True local_settings: 'local_settings.py' # set empty string if not used requirements: 'requirements.txt'
--- - hosts: root-hosts sudo: true roles: - system
roles/ system/ handlers/ main.yml tasks/ main.yml templates/ nginx.j2 supervisor.j2
--- - name: restart site supervisorctl: name={{ project_url }} state=restarted - name: restart mysql service: name=mysql state=restarted enabled=yes - name: restart nginx service: name=nginx state=restarted enabled=yes
--- # apt-get update - name: updating the system apt: update_cache=yes cache_valid_time=86400 notify: - restart server # apt-key mariadb - name: Add mariadb apt repository key apt_key: id=0xcbcb082a1bb943db keyserver=hkp://keyserver.ubuntu.com:80 state=present # mariadb - name: Add mariadb apt repository apt_repository: repo='deb http://mirror.timeweb.ru/mariadb/repo/10.1/debian wheezy main' state=present # - name: install packages apt: pkg={{ item.name }} state=present with_items: - name: python-mysqldb - name: python-virtualenv - name: python-pip - name: supervisor - name: mariadb-server - name: nginx - name: uwsgi - name: uwsgi-plugin-python # supervisor.conf.j2 templates ( ) - name: copy supervisor config template: src=supervisor.conf.j2 dest=/etc/supervisor/conf.d/{{ project_url }}.conf notify: - restart site # - name: create linux user user: name={{ username }} shell=/bin/bash home={{ user_homedir }} password={{ user_crypt_password }} # mysql - name: Create MySQL user mysql_user: > name={{ mysql_user }} host=% password={{ mysql_user_password }} priv={{ mysql_database }}.*:ALL login_user={{ mysql_root_user }} login_password={{ mysql_root_password }} state=present notify: - restart mysql # create database - name: Create MySQL database mysql_db: > name={{ mysql_database }} collation=utf8_general_ci encoding=utf8 login_user={{ mysql_root_user }} login_password={{ mysql_root_password }} state=present notify: - restart mysql # nginx.j2 templates - name: copy nginx config template: src=nginx.j2 dest=/etc/nginx/sites-available/{{ project_url }} notify: - restart nginx - name: create symlink nginx config file: src=/etc/nginx/sites-available/{{ project_url }} dest=/etc/nginx/sites-enabled/{{ project_url }} state=link
server { root {{ project_dir }}/{{ project_slug }}; access_log {{ project_dir }}/logs/nginx-access.log; error_log {{ project_dir }}/logs/nginx-errors.log; server_name {{ project_url }}; gzip on; gzip_min_length 1000; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain application/xml; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:{{ uwsgi_port }}; } location /static { root {{ project_dir }}; } location /media { root {{ project_dir }}; } location /robots.txt { root {{ project_dir }}; } }
--- - hosts: user-hosts sudo: false roles: - django - hosts: root-hosts sudo: true tasks: - name: restart site in supervisor supervisorctl: name={{ project_url }} state=restarted - name: restart mysql service: name=mysql state=restarted enabled=yes - name: restart nginx service: name=nginx state=restarted enabled=yes
--- - name: create project directory file: path={{ project_dir }} state=directory - name: create logs directory file: path={{ project_dir }}/logs state=directory - name: create project home directory file: path={{ project_homedir }} state=directory # , - name: unarchive project archive unarchive: src=/tmp/django_deploy.tar dest={{ project_homedir }} - name: create virtualenv pip: virtualenv={{ env }} virtualenv_site_packages=yes {% if requirements %}requirements={{ project_homedir }}/{{ requirements }}{% endif %} # uwsgi.j2 , . - name: copy uwsg file template: src=uwsgi.j2 dest={{ project_homedir }}/uwsgi.{{ project_slug }}.ini # - name: copy local_settings.py template: src=local_settings.py dest={{ project_homedir }}/{{ project_slug }}/{{ local_settings }} - name: syncdb (for django<1.7) django_manage: command=syncdb virtualenv={{ env }} app_path={{ project_homedir }} - name: migrate database django_manage: command=migrate virtualenv={{ env }} app_path={{ project_homedir }} - name: collectstatic django_manage: command=collectstatic virtualenv={{ env }} app_path={{ project_homedir }} - name: create media directory file: path={{ project_dir }}/media state=directory # django-tinymce - name: create `uploads` directory file: path={{ project_dir }}/media/uploads state=directory
# ( ) ansible-playbook -i hosts root-playbook.yml # tar -cf /tmp/django-deploy.tar * # ansible-playbook -i hosts user-playbook.yml
Source: https://habr.com/ru/post/241579/
All Articles