From 38a062e252eb646f3798f44c8b1b5a2ebadaf009 Mon Sep 17 00:00:00 2001 From: Ruan Bekker Date: Sun, 19 Jun 2022 05:03:51 +0200 Subject: [PATCH] Add ansible role --- .gitignore | 1 + Vagrantfile | 37 ++++++++++++ ansible/inventory | 1 + ansible/playbook.yml | 9 +++ ansible/roles/main/defaults/main.yml | 1 + ansible/roles/main/tasks/main.yml | 8 +++ ansible/roles/website/.travis.yml | 29 +++++++++ ansible/roles/website/README.md | 38 ++++++++++++ ansible/roles/website/defaults/main.yml | 21 +++++++ ansible/roles/website/files/styles.css | 50 ++++++++++++++++ ansible/roles/website/handlers/main.yml | 15 +++++ ansible/roles/website/meta/main.yml | 52 ++++++++++++++++ ansible/roles/website/tasks/configuration.yml | 59 +++++++++++++++++++ ansible/roles/website/tasks/main.yml | 13 ++++ ansible/roles/website/tasks/setup-ubuntu.yml | 8 +++ ansible/roles/website/templates/app.conf.j2 | 30 ++++++++++ ansible/roles/website/templates/index.html.j2 | 29 +++++++++ ansible/roles/website/templates/nginx.conf.j2 | 37 ++++++++++++ ansible/roles/website/tests/inventory | 2 + ansible/roles/website/tests/test.yml | 5 ++ ansible/roles/website/vars/main.yml | 2 + 21 files changed, 447 insertions(+) create mode 100644 .gitignore create mode 100644 Vagrantfile create mode 100644 ansible/inventory create mode 100644 ansible/playbook.yml create mode 100644 ansible/roles/main/defaults/main.yml create mode 100644 ansible/roles/main/tasks/main.yml create mode 100644 ansible/roles/website/.travis.yml create mode 100644 ansible/roles/website/README.md create mode 100644 ansible/roles/website/defaults/main.yml create mode 100644 ansible/roles/website/files/styles.css create mode 100644 ansible/roles/website/handlers/main.yml create mode 100644 ansible/roles/website/meta/main.yml create mode 100644 ansible/roles/website/tasks/configuration.yml create mode 100644 ansible/roles/website/tasks/main.yml create mode 100644 ansible/roles/website/tasks/setup-ubuntu.yml create mode 100644 ansible/roles/website/templates/app.conf.j2 create mode 100644 ansible/roles/website/templates/index.html.j2 create mode 100644 ansible/roles/website/templates/nginx.conf.j2 create mode 100644 ansible/roles/website/tests/inventory create mode 100644 ansible/roles/website/tests/test.yml create mode 100644 ansible/roles/website/vars/main.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c72071 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vagrant/* diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..935d99e --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,37 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +Vagrant.configure("2") do |config| + config.vm.box = "ubuntu/focal64" + config.vm.hostname = "sektor" + config.vm.define "sektor" + config.vagrant.plugins = ['vagrant-vbguest'] + config.vm.network "private_network", ip: "192.168.33.23" + config.vm.synced_folder "./", "/home/vagrant/project" + + config.vm.provider "virtualbox" do |vb| + vb.cpus = 2 + vb.memory = "2048" + vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] + vb.customize ["modifyvm", :id, "--ioapic", "on"] + end + + config.vm.provision "shell", inline: <<-SHELL + apt update + apt install curl git cmake vim -y + SHELL + + config.vm.provision "docker" do |d| + d.run "nginx", image: "nginx:stable", + args: "-p 8080:80" + end + + # Ansible provisioner + config.vm.provision "ansible" do |ansible| + ansible.compatibility_mode = "2.0" + ansible.playbook = "ansible/playbook.yml" + ansible.inventory_path = "ansible/inventory" + ansible.become = true + end + +end diff --git a/ansible/inventory b/ansible/inventory new file mode 100644 index 0000000..582d786 --- /dev/null +++ b/ansible/inventory @@ -0,0 +1 @@ +sektor ansible_host=192.168.33.23 diff --git a/ansible/playbook.yml b/ansible/playbook.yml new file mode 100644 index 0000000..a46f8c8 --- /dev/null +++ b/ansible/playbook.yml @@ -0,0 +1,9 @@ +--- +- name: my-playbook + hosts: sektor + become: yes + become_user: root + roles: + - { role: ./roles/website } + + diff --git a/ansible/roles/main/defaults/main.yml b/ansible/roles/main/defaults/main.yml new file mode 100644 index 0000000..73b314f --- /dev/null +++ b/ansible/roles/main/defaults/main.yml @@ -0,0 +1 @@ +--- \ No newline at end of file diff --git a/ansible/roles/main/tasks/main.yml b/ansible/roles/main/tasks/main.yml new file mode 100644 index 0000000..0e7df8f --- /dev/null +++ b/ansible/roles/main/tasks/main.yml @@ -0,0 +1,8 @@ +- name: Install Promtail Dependencies + apt: + name: "{{ item }}" + state: latest + update_cache: yes + with_items: + - tar + - unzip diff --git a/ansible/roles/website/.travis.yml b/ansible/roles/website/.travis.yml new file mode 100644 index 0000000..36bbf62 --- /dev/null +++ b/ansible/roles/website/.travis.yml @@ -0,0 +1,29 @@ +--- +language: python +python: "2.7" + +# Use the new container infrastructure +sudo: false + +# Install ansible +addons: + apt: + packages: + - python-pip + +install: + # Install ansible + - pip install ansible + + # Check ansible version + - ansible --version + + # Create ansible.cfg with correct roles_path + - printf '[defaults]\nroles_path=../' >ansible.cfg + +script: + # Basic role syntax check + - ansible-playbook tests/test.yml -i tests/inventory --syntax-check + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ \ No newline at end of file diff --git a/ansible/roles/website/README.md b/ansible/roles/website/README.md new file mode 100644 index 0000000..225dd44 --- /dev/null +++ b/ansible/roles/website/README.md @@ -0,0 +1,38 @@ +Role Name +========= + +A brief description of the role goes here. + +Requirements +------------ + +Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. + +Role Variables +-------------- + +A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. + +Dependencies +------------ + +A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. + +Example Playbook +---------------- + +Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: + + - hosts: servers + roles: + - { role: username.rolename, x: 42 } + +License +------- + +BSD + +Author Information +------------------ + +An optional section for the role authors to include contact information, or a website (HTML is not allowed). diff --git a/ansible/roles/website/defaults/main.yml b/ansible/roles/website/defaults/main.yml new file mode 100644 index 0000000..d7fdb31 --- /dev/null +++ b/ansible/roles/website/defaults/main.yml @@ -0,0 +1,21 @@ +--- +# defaults file for website +nginx_remove_default_vhost: true +nginx_default_vhost_path: /etc/nginx/sites-enabled/default +nginx_vhost_path: /etc/nginx/sites-enabled +nginx_conf_path: /etc/nginx/conf.d +nginx_log_path: /var/log/nginx +nginx_access_log_filename: access +nginx_error_log_filename: error +nginx_server_name: "_" +nginx_user: "www-data" +nginx_web_root: "/var/www/app" +root_group: "root" +webpage_title: "hello world" +webpage_text: "homepage" +webpage_url: "https://ruan.dev" +access_log_args: "main buffer=16k flush=2m" +nginx_log_format: |- + '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"' \ No newline at end of file diff --git a/ansible/roles/website/files/styles.css b/ansible/roles/website/files/styles.css new file mode 100644 index 0000000..82fd3ed --- /dev/null +++ b/ansible/roles/website/files/styles.css @@ -0,0 +1,50 @@ +html, body { + background-color: #fff; + color: #636b6f; + font-family: 'Nunito', sans-serif; + font-weight: 200; + height: 100vh; + margin: 0; +} + +.full-height { + height: 100vh; +} + +.flex-center { + align-items: center; + display: flex; + justify-content: center; +} + +.position-ref { + position: relative; +} + +.top-right { + position: absolute; + right: 10px; + top: 18px; +} + +.content { + text-align: center; +} + +.title { + font-size: 84px; +} + +.links > a { + color: #636b6f; + padding: 0 25px; + font-size: 13px; + font-weight: 600; + letter-spacing: .1rem; + text-decoration: none; + text-transform: uppercase; +} + +.m-b-md { + margin-bottom: 30px; +} \ No newline at end of file diff --git a/ansible/roles/website/handlers/main.yml b/ansible/roles/website/handlers/main.yml new file mode 100644 index 0000000..b4723d5 --- /dev/null +++ b/ansible/roles/website/handlers/main.yml @@ -0,0 +1,15 @@ +--- +# handlers file for website +- name: restart nginx + service: + name: nginx + state: restarted + +- name: validate nginx configuration + command: nginx -t -c /etc/nginx/nginx.conf + changed_when: false + +- name: reload nginx + service: + name: nginx + state: reloaded \ No newline at end of file diff --git a/ansible/roles/website/meta/main.yml b/ansible/roles/website/meta/main.yml new file mode 100644 index 0000000..c572acc --- /dev/null +++ b/ansible/roles/website/meta/main.yml @@ -0,0 +1,52 @@ +galaxy_info: + author: your name + description: your role description + company: your company (optional) + + # If the issue tracker for your role is not on github, uncomment the + # next line and provide a value + # issue_tracker_url: http://example.com/issue/tracker + + # Choose a valid license ID from https://spdx.org - some suggested licenses: + # - BSD-3-Clause (default) + # - MIT + # - GPL-2.0-or-later + # - GPL-3.0-only + # - Apache-2.0 + # - CC-BY-4.0 + license: license (GPL-2.0-or-later, MIT, etc) + + min_ansible_version: 2.1 + + # If this a Container Enabled role, provide the minimum Ansible Container version. + # min_ansible_container_version: + + # + # Provide a list of supported platforms, and for each platform a list of versions. + # If you don't wish to enumerate all versions for a particular platform, use 'all'. + # To view available platforms and versions (or releases), visit: + # https://galaxy.ansible.com/api/v1/platforms/ + # + # platforms: + # - name: Fedora + # versions: + # - all + # - 25 + # - name: SomePlatform + # versions: + # - all + # - 1.0 + # - 7 + # - 99.99 + + galaxy_tags: [] + # List tags for your role here, one per line. A tag is a keyword that describes + # and categorizes the role. Users find roles by searching for tags. Be sure to + # remove the '[]' above, if you add tags to this list. + # + # NOTE: A tag is limited to a single word comprised of alphanumeric characters. + # Maximum 20 tags per role. + +dependencies: [] + # List your role dependencies here, one per line. Be sure to remove the '[]' above, + # if you add dependencies to this list. diff --git a/ansible/roles/website/tasks/configuration.yml b/ansible/roles/website/tasks/configuration.yml new file mode 100644 index 0000000..4a4ca6c --- /dev/null +++ b/ansible/roles/website/tasks/configuration.yml @@ -0,0 +1,59 @@ +--- +- name: Copy nginx configuration in place. + template: + src: nginx.conf.j2 + dest: /etc/nginx/nginx.conf + owner: root + group: "{{ root_group }}" + mode: 0644 + notify: + - reload nginx + +- name: Remove default nginx vhost config file (if configured). + file: + path: "{{ nginx_default_vhost_path }}" + state: absent + when: nginx_remove_default_vhost | bool + notify: restart nginx + +- name: Ensure nginx_web_root exists. + file: + path: "{{ nginx_web_root }}" + state: directory + mode: 0755 + notify: reload nginx + +- name: Ensure nginx_web_root exists. + file: + path: "{{ nginx_web_root }}" + state: directory + mode: 0755 + notify: reload nginx + +- name: copy promtail systemd unit + copy: + src: styles.css + dest: "{{ nginx_web_root }}/styles.css" + owner: root + group: "{{ root_group }}" + mode: 0644 + notify: reload nginx + +- name: Add website files + template: + src: index.html.j2 + dest: "{{ nginx_web_root }}/index.html" + owner: root + group: "{{ root_group }}" + mode: 0644 + notify: reload nginx + +- name: Add managed vhost config files. + template: + src: app.conf.j2 + dest: "{{ nginx_conf_path }}/app.conf" + force: true + owner: root + group: "{{ root_group }}" + mode: 0644 + notify: reload nginx \ No newline at end of file diff --git a/ansible/roles/website/tasks/main.yml b/ansible/roles/website/tasks/main.yml new file mode 100644 index 0000000..641a5eb --- /dev/null +++ b/ansible/roles/website/tasks/main.yml @@ -0,0 +1,13 @@ +--- +# tasks file for website +- include_tasks: setup-ubuntu.yml + when: ansible_distribution == 'Ubuntu' + +# vhost configuration. +- import_tasks: configuration.yml + +- name: Ensure nginx service is running as configured. + service: + name: nginx + state: started + enabled: true \ No newline at end of file diff --git a/ansible/roles/website/tasks/setup-ubuntu.yml b/ansible/roles/website/tasks/setup-ubuntu.yml new file mode 100644 index 0000000..f6e3593 --- /dev/null +++ b/ansible/roles/website/tasks/setup-ubuntu.yml @@ -0,0 +1,8 @@ +--- +- name: Ensure dirmngr and nginx is installed + apt: + name: "{{ item }}" + state: present + with_items: + - dirmngr + - nginx \ No newline at end of file diff --git a/ansible/roles/website/templates/app.conf.j2 b/ansible/roles/website/templates/app.conf.j2 new file mode 100644 index 0000000..c5512bc --- /dev/null +++ b/ansible/roles/website/templates/app.conf.j2 @@ -0,0 +1,30 @@ +server { + listen 80; + server_name {{ nginx_server_name }}; + root {{ nginx_web_root }}; + + index index.html; + charset utf-8; + + location / { + try_files $uri $uri/ /index.html$is_args$args; + } + + location = /favicon.ico { + access_log off; + log_not_found off; + } + + location = /robots.txt { + access_log off; + log_not_found off; + } + + location ~ /\.(?!well-known).* { + deny all; + } + + location ~ /\.ht { + deny all; + } +} \ No newline at end of file diff --git a/ansible/roles/website/templates/index.html.j2 b/ansible/roles/website/templates/index.html.j2 new file mode 100644 index 0000000..c824007 --- /dev/null +++ b/ansible/roles/website/templates/index.html.j2 @@ -0,0 +1,29 @@ + + + + + + + {{ webpage_title }} + + + + + + + + + +
+
+
+ {{ webpage_title }} +
+ + +
+
+ + \ No newline at end of file diff --git a/ansible/roles/website/templates/nginx.conf.j2 b/ansible/roles/website/templates/nginx.conf.j2 new file mode 100644 index 0000000..e8a67c6 --- /dev/null +++ b/ansible/roles/website/templates/nginx.conf.j2 @@ -0,0 +1,37 @@ +user {{ nginx_user }}; +worker_processes auto; +pid /run/nginx.pid; +include /etc/nginx/modules-enabled/*.conf; + +events { + worker_connections 768; +} + +http { + + # Basic Settings + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + + include /etc/nginx/mime.types; + default_type application/octet-stream; + + # SSL Settings + ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE + ssl_prefer_server_ciphers on; + + # Logging Settings + log_format main {{ nginx_log_format|indent(23) }}; + + access_log {{ nginx_log_path }}/{{ nginx_access_log_filename }}.log {{ access_log_args }}; + error_log {{ nginx_log_path }}/{{ nginx_error_log_filename }}.log; + + # Gzip Settings + gzip on; + + # Virtual Host Configs + include {{ nginx_conf_path }}/*.conf; +} \ No newline at end of file diff --git a/ansible/roles/website/tests/inventory b/ansible/roles/website/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/ansible/roles/website/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/ansible/roles/website/tests/test.yml b/ansible/roles/website/tests/test.yml new file mode 100644 index 0000000..db427c4 --- /dev/null +++ b/ansible/roles/website/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - website diff --git a/ansible/roles/website/vars/main.yml b/ansible/roles/website/vars/main.yml new file mode 100644 index 0000000..35cb4a5 --- /dev/null +++ b/ansible/roles/website/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for website