diff --git a/README.md b/README.md index ce446fa..fcdbfe3 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,9 @@ concise area of concern. - [`openproject`](roles/openproject/README.md): Deploys an [openproject.org](https://www.openproject.org) installation using the upstream provided docker-compose setup. +- [`snipe_it`](roles/snipe_it/README.md): Deploys [Snipe-IT](https://snipeitapp.com/), + the free and open-source IT asset (and license) management with a powerful REST API + - [`vaultwarden`](roles/vaultwarden/README.md): Deploy [vaultwarden](https://github.com/dani-garcia/vaultwarden/), an open-source implementation of the Bitwarden Server (formerly Bitwarden\_RS). diff --git a/galaxy.yml b/galaxy.yml index b9500fc..fabb80f 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -19,4 +19,5 @@ tags: - hedgedoc - jellyfin - vaultwarden + - snipe-it - docker diff --git a/playbooks/snipe_it.yml b/playbooks/snipe_it.yml new file mode 100644 index 0000000..6554bfd --- /dev/null +++ b/playbooks/snipe_it.yml @@ -0,0 +1,6 @@ +--- +- name: Install and configure Snipe-IT + hosts: "{{ snipe_it_hosts | default('snipe_it') }}" + become: "{{ snipe_it_become | default(true, false) }}" + roles: + - role: finallycoffee.services.snipe_it diff --git a/roles/snipe_it/README.md b/roles/snipe_it/README.md new file mode 100644 index 0000000..27d91fc --- /dev/null +++ b/roles/snipe_it/README.md @@ -0,0 +1,46 @@ +# `finallycoffee.services.snipe_it` ansible role + +[Snipe-IT](https://snipeitapp.com/) is an open-source asset management with +a powerful JSON-REST API. This ansible role deploys and configures Snipe-IT. + +## Requirements + +Snipe-IT requires a MySQL-Database like MariaDB and a working email service +for sending email. For installing and configuring MariaDB, see +[`finallycoffee.base.mariadb`](https://galaxy.ansible.com/ui/repo/published/finallycoffee/base/content/role/mariadb/). + +## Configuration + +Required variables to set are: + +- `snipe_it_domain` - domain name of the snipe-it instance +- `snipe_it_config_app_url` - URL where snipe-it will be reachable including protocol and port +- `snipe_it_config_app_key` - Laravel application key + +### Database configuration + +All (database) options from the upstream laravel `.env` file are available +under the `snipe_it_config_db_*` prefix. Configure a database as follows: +``` +snipe_it_config_db_host: localhost # defaults to localhost +snipe_it_config_db_port: "3306" # defaults to 3306 +snipe_it_config_db_database: my_snipe_db_name # defaults to 'snipeit' +snipe_it_config_db_username: my_snipe_db_user # defaults to 'snipeit' +snipe_it_config_db_password: my_snipe_db_password +# Set this if the database is shared with +# other applications. defaults to not set +snipe_it_config_db_prefix: snipe_ +``` + +### Email configuration + +Configuring an email server is mandatory. An example is provided below: +```yaml +snipe_it_config_mail_host: smtp.example.com +snipe_it_config_mail_username: snipe_user@snipe.example.com +snipe_it_config_mail_password: i_want_to_be_strong_and_long +snipe_it_config_mail_from_addr: "noreply@snipe.example.com" +snipe_it_config_mail_from_name: "Example.com SnipeIT instance" +``` + +The default smtp port is `587` and can be set in `snipe_it_config_mail_port`. diff --git a/roles/snipe_it/defaults/main/config.yml b/roles/snipe_it/defaults/main/config.yml new file mode 100644 index 0000000..67e9ca7 --- /dev/null +++ b/roles/snipe_it/defaults/main/config.yml @@ -0,0 +1,133 @@ +--- +snipe_it_config_app_version: "v{{ snipe_it_version }}" +snipe_it_config_app_port: 8000 +snipe_it_config_app_env: "production" +snipe_it_config_app_debug: false +snipe_it_config_app_key: ~ +snipe_it_config_app_url: "http://localhost:{{ snipe_it_config_app_port }}" +snipe_it_config_app_timezone: UTC +snipe_it_config_app_locale: en-US +snipe_it_config_app_locked: false +snipe_it_config_app_cipher: "AES-256-GCM" +snipe_it_config_app_force_tls: false +snipe_it_config_app_trusted_proxies: + - '192.168.0.0/16' + - '172.16.0.0/12' + - '10.0.0.0/8' + +snipe_it_config_db_connection: mysql +snipe_it_config_db_host: localhost +snipe_it_config_db_port: "3306" +snipe_it_config_db_database: snipeit +snipe_it_config_db_username: snipeit +snipe_it_config_db_password: ~ +snipe_it_config_db_prefix: ~ +snipe_it_config_db_dump_path: /usr/bin/ +snipe_it_config_db_charset: utf8mb4 +snipe_it_config_db_collation: utf8mb4_unicode_ci +snipe_it_config_db_ssl: false +snipe_it_config_db_ssl_is_paas: false +snipe_it_config_db_ssl_key_path: ~ +snipe_it_config_db_ssl_cert_path: ~ +snipe_it_config_db_ssl_ca_path: ~ +snipe_it_config_db_ssl_cipher: ~ +snipe_it_config_db_ssl_verify_server: ~ + +snipe_it_config_mail_mailer: smtp +snipe_it_config_mail_host: ~ +snipe_it_config_mail_port: 587 +snipe_it_config_mail_username: ~ +snipe_it_config_mail_password: ~ +snipe_it_config_mail_tls_verify_peer: true +snipe_it_config_mail_from_addr: ~ +snipe_it_config_mail_from_name: ~ +snipe_it_config_mail_replyto_addr: "{{ snipe_it_config_mail_from_addr }}" +snipe_it_config_mail_replyto_name: "{{ snipe_it_config_mail_from_name }}" +snipe_it_config_mail_auto_embed_method: attachment +snipe_it_config_mail_backup_notification_driver: ~ +snipe_it_config_mail_backup_notification_address: ~ + +snipe_it_config_private_filesystem_disk: >-2 + {{ snipe_it_data_directory }}/uploads/private +snipe_it_config_public_filesystem_disk: >-2 + {{ snipe_it_data_directory }}/uploads/public +snipe_it_config_allow_backup_delete: false +snipe_it_config_allow_data_purge: false +snipe_it_config_image_lib: 'gd' + +snipe_it_config_log_channel: 'stderr' +snipe_it_config_log_max_days: 10 + +snipe_it_config_cookie_name: ".snipeit_session" +snipe_it_config_cookie_domain: "{{ snipe_it_domain }}" +snipe_it_config_secure_cookies: true + +snipe_it_config_session_driver: file +snipe_it_config_session_lifetime: 12000 +snipe_it_config_cache_driver: file +snipe_it_config_cache_prefix: snipeit +snipe_it_config_queue_driver: file + +snipe_it_base_config: + APP_VERSION: "{{ snipe_it_config_app_version }}" + APP_PORT: "{{ snipe_it_config_app_port }}" + APP_ENV: "{{ snipe_it_config_app_env }}" + APP_DEBUG: "{{ snipe_it_config_app_debug }}" + APP_KEY: "{{ snipe_it_config_app_key }}" + APP_URL: "{{ snipe_it_config_app_url }}" + APP_TIMEZONE: "{{ snipe_it_config_app_timezone }}" + APP_LOCALE: "{{ snipe_it_config_app_locale }}" + APP_LOCKED: "{{ snipe_it_config_app_locked }}" + APP_CIPHER: "{{ snipe_it_config_app_cipher }}" + APP_FORCE_TLS: "{{ snipe_it_config_app_force_tls }}" + APP_TRUSTED_PROXIES: "{{ snipe_it_config_app_trusted_proxies | join(',') }}" + DB_CONNECTION: "{{ snipe_it_config_db_connection }}" + DB_HOST: "{{ snipe_it_config_db_host }}" + DB_PORT: "{{ snipe_it_config_db_port }}" + DB_DATABASE: "{{ snipe_it_config_db_database }}" + DB_USERNAME: "{{ snipe_it_config_mail_username }}" + DB_PASSWORD: "{{ snipe_it_config_db_password }}" + DB_PREFIX: "{{ snipe_it_config_db_prefix | default('null', true) }}" + DB_DUMP_PATH: "{{ snipe_it_config_db_dump_path }}" + DB_CHARSET: "{{ snipe_it_config_db_charset }}" + DB_COLLATION: "{{ snipe_it_config_db_collation }}" + DB_SSL: "{{ snipe_it_config_db_ssl }}" + DB_SSL_IS_PAAS: "{{ snipe_it_config_db_ssl_is_paas }}" + DB_SSL_KEY_PATH: "{{ snipe_it_config_db_ssl_key_path | default('null', true) }}" + DB_SSL_CERT_PATH: "{{ snipe_it_config_db_ssl_cert_path | default('null', true) }}" + DB_SSL_CA_PATH: "{{ snipe_it_config_db_ssl_ca_path | default('null', true) }}" + DB_SSL_CIPHER: "{{ snipe_it_config_db_ssl_cipher | default('null', true) }}" + DB_SSL_VERIFY_SERVER: "{{ snipe_it_config_db_ssl_verify_server | default('null', true) }}" + MAIL_MAILER: "{{ snipe_it_config_mail_mailer }}" + MAIL_HOST: "{{ snipe_it_config_mail_host }}" + MAIL_PORT: "{{ snipe_it_config_mail_port }}" + MAIL_USERNAME: "{{ snipe_it_config_mail_username }}" + MAIL_PASSWORD: "{{ snipe_it_config_mail_password }}" + MAIL_TLS_VERIFY_PEER: "{{ snipe_it_config_mail_tls_verify_peer }}" + MAIL_FROM_ADDR: "{{ snipe_it_config_mail_from_addr | default('null', true) }}" + MAIL_FROM_NAME: "{{ snipe_it_config_mail_from_name | default('null', true) }}" + MAIL_REPLYTO_ADDR: "{{ snipe_it_config_mail_replyto_addr | default('null', true) }}" + MAIL_REPLYTO_NAME: "{{ snipe_it_config_mail_replyto_name | default('null', true) }}" + MAIL_AUTO_EMBED_METHOD: "{{ snipe_it_config_mail_auto_embed_method }}" + MAIL_BACKUP_NOTIFICATION_DRIVER: "{{ snipe_it_config_mail_backup_notification_driver }}" + MAIL_BACKUP_NOTIFICATION_ADDRESS: "{{ snipe_it_config_mail_backup_notification_address }}" + SESSION_DRIVER: "{{ snipe_it_config_session_driver }}" + SESSION_LIFETIME: "{{ snipe_it_config_session_lifetime }}" + CACHE_DRIVER: "{{ snipe_it_config_cache_driver }}" + CACHE_PREFIX: "{{ snipe_it_config_cache_prefix }}" + QUEUE_DRIVER: "{{ snipe_it_config_queue_driver }}" + PRIVATE_FILESYSTEM_DISK: "{{ snipe_it_config_private_filesystem_disk }}" + PUBLIC_FILESYSTEM_DISK: "{{ snipe_it_config_public_filesystem_disk }}" + ALLOW_BACKUP_DELETE: "{{ snipe_it_config_allow_backup_delete }}" + ALLOW_DATA_PURGE: "{{ snipe_it_config_allow_data_purge }}" + IMAGE_LIB: "{{ snipe_it_config_image_lib }}" + LOG_CHANNEL: "{{ snipe_it_config_log_channel }}" + LOG_MAX_DAYS: "{{ snipe_it_config_log_max_days }}" + COOKIE_NAME: "{{ snipe_it_config_cookie_name }}" + COOKIE_DOMAIN: "{{ snipe_it_config_cookie_domain }}" + SECURE_COOKIES: "{{ snipe_it_config_secure_cookies }}" + +snipe_it_config: ~ +snipe_it_merged_config: >-2 + {{ (snipe_it_base_config | default({}, true)) + | combine((snipe_it_config | default({}, true)), recursive=True) }} diff --git a/roles/snipe_it/defaults/main/container.yml b/roles/snipe_it/defaults/main/container.yml new file mode 100644 index 0000000..dbbfd1c --- /dev/null +++ b/roles/snipe_it/defaults/main/container.yml @@ -0,0 +1,49 @@ +--- +snipe_it_container_image_registry: docker.io +snipe_it_container_image_namespace: snipe +snipe_it_container_image_name: 'snipe-it' +snipe_it_container_image_tag: ~ +snipe_it_container_image_flavour: alpine +snipe_it_container_image_source: pull +snipe_it_container_image_force_source: >-2 + {{ snipe_it_container_image_tag | default(false, true) | bool }} +snipe_it_container_image: >-2 + {{ + ([ + snipe_it_container_image_registry | default([], true), + snipe_it_container_image_namespace | default([], true), + snipe_it_container_image_name, + ] | flatten | join('/')) + + ':' + + (snipe_it_container_image_tag | default( + 'v' + snipe_it_version + ( + ((snipe_it_container_image_flavour is string) + and (snipe_it_container_image_flavour | length > 0)) + | ternary( + '-' + snipe_it_container_image_flavour | default('', true), + '' + ) + ), + true + )) + }} + +snipe_it_container_env_file: "/var/www/html/.env" +snipe_it_container_data_directory: "/var/lib/snipeit" +snipe_it_container_volumes: + - "{{ snipe_it_env_file }}:{{ snipe_it_container_env_file }}:ro" + - "{{ snipe_it_data_directory }}:{{ snipe_it_container_data_directory }}:z" + +snipe_it_container_name: 'snipe-it' +snipe_it_container_state: >-2 + {{ (snipe_it_state == 'present') | ternary('started', 'absent') }} +snipe_it_container_env: ~ +snipe_it_container_user: ~ +snipe_it_container_ports: ~ +snipe_it_container_labels: ~ +snipe_it_container_recreate: ~ +snipe_it_container_networks: ~ +snipe_it_container_etc_hosts: ~ +snipe_it_container_dns_servers: ~ +snipe_it_container_network_mode: ~ +snipe_it_container_restart_policy: 'unless-stopped' diff --git a/roles/snipe_it/defaults/main/main.yml b/roles/snipe_it/defaults/main/main.yml new file mode 100644 index 0000000..d66c18e --- /dev/null +++ b/roles/snipe_it/defaults/main/main.yml @@ -0,0 +1,9 @@ +--- +snipe_it_user: snipeit +snipe_it_version: "7.0.13" +snipe_it_domain: ~ +snipe_it_state: present +snipe_it_deployment_method: docker + +snipe_it_env_file: /etc/snipeit/env +snipe_it_data_directory: /var/lib/snipeit diff --git a/roles/snipe_it/defaults/main/user.yml b/roles/snipe_it/defaults/main/user.yml new file mode 100644 index 0000000..8faf98b --- /dev/null +++ b/roles/snipe_it/defaults/main/user.yml @@ -0,0 +1,5 @@ +--- +snipe_it_run_user_id: >-2 + {{ snipe_it_user_info.uid | default(snipe_it_user) }} +snipe_it_run_group_id: >-2 + {{ snipe_it_user_info.group | default(snipe_it_user) }} diff --git a/roles/snipe_it/meta/main.yml b/roles/snipe_it/meta/main.yml new file mode 100644 index 0000000..539582c --- /dev/null +++ b/roles/snipe_it/meta/main.yml @@ -0,0 +1,12 @@ +--- +allow_duplicates: true +dependencies: [] +galaxy_info: + role_name: snipe_it + description: >-2 + Deploy Snipe-IT, an open-source asset / license management system with + powerful JSON REST API + galaxy_tags: + - snipeit + - asset-management + - docker diff --git a/roles/snipe_it/tasks/check.yml b/roles/snipe_it/tasks/check.yml new file mode 100644 index 0000000..8fec363 --- /dev/null +++ b/roles/snipe_it/tasks/check.yml @@ -0,0 +1,14 @@ +--- +- name: Ensure state is valid + ansible.builtin.fail: + msg: >-2 + Unsupported state '{{ snipe_it_state }}'! + Supported states are {{ snipe_it_states | join(', ') }}. + when: snipe_it_state is not in snipe_it_states + +- name: Ensure deployment method is valid + ansible.builtin.fail: + msg: >-2 + Unsupported deployment_method '{{ snipe_it_deployment_method }}'! + Supported values are {{ snipe_it_deployment_methods | join(', ') }}. + when: snipe_it_deployment_method is not in snipe_it_deployment_methods diff --git a/roles/snipe_it/tasks/deploy-docker.yml b/roles/snipe_it/tasks/deploy-docker.yml new file mode 100644 index 0000000..4044ae1 --- /dev/null +++ b/roles/snipe_it/tasks/deploy-docker.yml @@ -0,0 +1,30 @@ +--- +- name: Ensure container image '{{ snipe_it_container_image }}' is {{ snipe_it_state }} + community.docker.docker_image: + name: "{{ snipe_it_container_image }}" + state: "{{ snipe_it_state }}" + source: "{{ snipe_it_container_image_source }}" + force_source: "{{ snipe_it_container_image_force_source }}" + register: snipe_it_container_image_info + until: snipe_it_container_image_info is success + retries: 5 + delay: 3 + +- name: Ensure container '{{ snipe_it_container_name }}' is {{ snipe_it_container_state }} + community.docker.docker_container: + name: "{{ snipe_it_container_name }}" + image: "{{ snipe_it_container_image }}" + env_file: "{{ snipe_it_env_file }}" + env: "{{ snipe_it_container_env | default(omit, true) }}" + user: "{{ snipe_it_container_user | default(omit, true) }}" + ports: "{{ snipe_it_container_ports | default(omit, true) }}" + labels: "{{ snipe_it_container_labels | default(omit, true) }}" + volumes: "{{ snipe_it_container_volumes | default(omit, true) }}" + networks: "{{ snipe_it_container_networks | default(omit, true) }}" + etc_hosts: "{{ snipe_it_container_etc_hosts | default(omit, true) }}" + dns_servers: "{{ snipe_it_container_dns_servers | default(omit, true) }}" + network_mode: "{{ snipe_it_container_network_mode | default(omit, true) }}" + restart_policy: >-2 + {{ snipe_it_container_restart_policy | default(omit, true) }} + recreate: "{{ snipe_it_container_recreate | default(omit, true) }}" + state: "{{ snipe_it_container_state }}" diff --git a/roles/snipe_it/tasks/main.yml b/roles/snipe_it/tasks/main.yml new file mode 100644 index 0000000..4e0cec4 --- /dev/null +++ b/roles/snipe_it/tasks/main.yml @@ -0,0 +1,60 @@ +--- +- name: Check preconditions + ansible.builtin.include_tasks: + file: "check.yml" + +- name: Ensure snipe-it user '{{ snipe_it_user }}' is {{ snipe_it_state }} + ansible.builtin.user: + name: "{{ snipe_it_user }}" + state: "{{ snipe_it_state }}" + system: "{{ snipe_it_user_system | default(true, true) }}" + create_home: "{{ snipe_it_user_create_home | default(false, true) }}" + groups: "{{ snipe_it_user_groups | default(omit, true) }}" + append: >-2 + {{ + snipe_it_user_groups_append | default( + snipe_it_user_groups | default([], true) | length > 0, + true, + ) + }} + register: snipe_it_user_info + +- name: Ensure snipe-it environment file is {{ snipe_it_state }} + ansible.builtin.file: + path: "{{ snipe_it_env_file }}" + state: "{{ snipe_it_state }}" + when: snipe_it_state == 'absent' + +- name: Ensure snipe-it config directory is {{ snipe_it_state }} + ansible.builtin.file: + path: "{{ snipe_it_env_file | dirname }}" + state: "{{ (snipe_it_state == 'present') | ternary('directory', 'absent') }}" + owner: "{{ snipe_it_run_user_id }}" + group: "{{ snipe_it_run_group_id }}" + mode: "0755" + when: snipe_it_state == 'present' + +- name: Ensure snipe-it data directory '{{ snipe_it_data_directory }}' is {{ snipe_it_state }} + ansible.builtin.file: + path: "{{ snipe_it_data_directory }}" + state: "{{ (snipe_it_state == 'present') | ternary('directory', 'absent') }}" + owner: "{{ snipe_it_run_user_id }}" + group: "{{ snipe_it_run_group_id }}" + mode: "0755" +- debug: + msg: "{{ snipe_it_base_config }}" +- name: Ensure snipe-it environment file is templated + ansible.builtin.copy: + content: |+2 + {% for entry in snipe_it_merged_config | dict2items %} + {{ entry.key }}={{ entry.value }} + {% endfor %} + dest: "{{ snipe_it_env_file }}" + owner: "{{ snipe_it_run_user_id }}" + group: "{{ snipe_it_run_group_id }}" + mode: "0640" + when: snipe_it_state == 'present' + +- name: Deploy using {{ snipe_it_deployment_method }} + ansible.builtin.include_tasks: + file: "deploy-{{ snipe_it_deployment_method }}.yml" diff --git a/roles/snipe_it/vars/main.yml b/roles/snipe_it/vars/main.yml new file mode 100644 index 0000000..9111541 --- /dev/null +++ b/roles/snipe_it/vars/main.yml @@ -0,0 +1,6 @@ +--- +snipe_it_states: + - present + - absent +snipe_it_deployment_methods: + - docker