diff --git a/.travis.yml b/.travis.yml index 70322f0..3bcee4d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,28 +1,38 @@ --- +dist: bionic language: python -services: docker +python: "3.8" + +# Use the new container infrastructure +sudo: false env: global: - ROLE_NAME: nginx matrix: - - MOLECULE_DISTRO: centos7 - - MOLECULE_DISTRO: ubuntu1804 - - MOLECULE_DISTRO: ubuntu1604 - - MOLECULE_DISTRO: debian9 + - MOLECULE_DISTRIBUTION: ubuntu18 + - MOLECULE_DISTRIBUTION: ubuntu20 + - MOLECULE_DISTRIBUTION: debian8 + - MOLECULE_DISTRIBUTION: debian9 + - MOLECULE_DISTRIBUTION: debian10 +# Install ansible +addons: + apt: + packages: + - python-pip install: - # Install test dependencies. - - pip install molecule yamllint ansible-lint docker + - mv "$PWD" "${PWD%/*}/$ROLE_NAME" + # Install ansible + - pip install ansible molecule docker testinfra pytest pytest-xdist + + # Check ansible version + - ansible --version -before_script: - # Use actual Ansible Galaxy role name for the project directory. - - cd ../ - - mv ansible-role-$ROLE_NAME geerlingguy.$ROLE_NAME - - cd geerlingguy.$ROLE_NAME + - printf '[defaults]\nroles_path=../' >ansible.cfg script: - # Run tests. + # Basic role syntax check - molecule test notifications: diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index ee65197..c3ddc5d 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -6,14 +6,14 @@ nginx_use_ppa: true nginx_remove_default_vhost: true nginx_vhosts: - - server_name: "test.dev" + - server_name: "localhost" root: "/var/www/test" pre_tasks: - - name: Update apt cache. + - name: Update apt cache apt: update_cache=yes cache_valid_time=600 when: ansible_os_family == 'Debian' changed_when: false roles: - - role: geerlingguy.nginx + - role: nginx diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 2da47dd..076c745 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -5,11 +5,11 @@ driver: name: docker lint: | set -e - yamllint . - ansible-lint + yamllint . -d "{extends: default, ignore: .github}" + ansible-lint -r . platforms: - - name: instance - image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos7}-ansible:latest" + - name: ${MOLECULE_DISTRIBUTION:-debian10} + image: "nekeal/${MOLECULE_DISTRIBUTION:-debian10}-ansible-systemd:latest" command: ${MOLECULE_DOCKER_COMMAND:-""} volumes: - /sys/fs/cgroup:/sys/fs/cgroup:ro diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index 7e2995e..7723ad5 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -15,7 +15,8 @@ - name: Add managed vhost config files. template: src: "{{ item.template|default(nginx_vhost_template) }}" - dest: "{{ nginx_vhost_path }}/{{ item.filename|default(item.server_name.split(' ')[0] ~ '.conf') }}" + dest: "{{ nginx_vhost_path }}/{{ item.filename + | default(item.server_name.split(' ')[0] ~ '.conf') }}" force: true owner: root group: "{{ root_group }}" @@ -28,7 +29,8 @@ - name: Remove managed vhost config files. file: - path: "{{ nginx_vhost_path }}/{{ item.filename|default(item.server_name.split(' ')[0] ~ '.conf') }}" + path: "{{ nginx_vhost_path }}/{{ item.filename | + default(item.server_name.split(' ')[0] ~ '.conf') }}" state: absent when: item.state|default('present') == 'absent' with_items: "{{ nginx_vhosts }}" diff --git a/templates/django-vhost.j2 b/templates/django-vhost.j2 new file mode 100644 index 0000000..1dbf411 --- /dev/null +++ b/templates/django-vhost.j2 @@ -0,0 +1,69 @@ +# {{ ansible_managed }} + +upstream {{ item.upstream.name }} { + server {{ item.upstream.server }} +} + +server { + listen 80; + {% if item.enable_https %} + listen 443 ssl {% if item.enable_http2 %}http2{% endif %}; + {% endif %} + server_name {% for server_name in item.server_names %}{{ server_name }} {% endfor %}; + + {% if item.access_log %} + access_log /var/log/nginx/{{ item.access_log }}.access.log; + {% endif %} + + {% if item.error_log %} + error_log /var/log/nginx/{{ app_name }}.error.log; + {% endif %} + + {% if item.enable_https %} + if ($scheme = http) { + return 301 https://$host$request_uri; + } + {% endif %} + + ssl_certificate {{ item.certificate }}; + ssl_certificate_key {{ item.private_key }}; + ssl_trusted_certificate {{ item.certificate }}; + + {% if item.error_page is defined %} + error_page {{ item.error_page }}; + {% endif %} + + {% if item.static_root %} + location /static/ { + alias {{ item.static_root }}; + } + {% endif %} + + {% if item.media_root %} + location /media/ { + alias {{ item.media_root }}; + } + {% endif %} + + {% for location in item.additional_locations | default([]) %} + location {{ location.path }} { + alias {{ location.alias }}; + } + {% endfor %} + + {% if enable_default_cache %} + location ~* /static/.*\.(js|css)$ { + root {{ directories.project }}; + expires 2d; + add_header Cache-Control "public, no-transform"; + } + {% endif %} + + location / { + proxy_pass {{ item.upstream }}; + proxy_set_header X-Forwarded-Host $server_name; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-Proto $scheme; + } + }