From c238a3406e88b2536eeec7cbe408054cdc7484d9 Mon Sep 17 00:00:00 2001 From: dark Date: Sun, 12 Dec 2021 13:10:42 +0100 Subject: [PATCH] Initial Commit of initial state At least better then the usual "initial commit!"-Commit! --- defaults/main.yml | 75 +++++++++++++++++++++++ handlers/main.yml | 13 ++++ tasks/main.yml | 22 +++++++ templates/docker-compose.override.yml.j2 | 5 ++ templates/docker-compose.yml.j2 | 77 ++++++++++++++++++++++++ templates/env/netbox.env.j2 | 46 ++++++++++++++ templates/env/postgres.env.j2 | 3 + templates/env/redis-cache.env.j2 | 1 + templates/env/redis.env.j2 | 1 + 9 files changed, 243 insertions(+) create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/docker-compose.override.yml.j2 create mode 100644 templates/docker-compose.yml.j2 create mode 100644 templates/env/netbox.env.j2 create mode 100644 templates/env/postgres.env.j2 create mode 100644 templates/env/redis-cache.env.j2 create mode 100644 templates/env/redis.env.j2 diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..5014c6e --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,75 @@ +--- +netbox: + conf: + metrics: "false" + secretkey: "r8OwDznj!!dci#P9ghmRfdu1Ysxm0AiPeDCQhKE+N_rClfWNj" + + mail: + from: "netbox@bar.com" + password: "" + port: "25" + host: "localhost" + user: "netbox" + tls: "false" + timeout: 10 # seconds + ssl: + enabled: "false" + certfile: "" + keyfile: "" + + user: + superuser: + name: "admin" + api_token: "0123456789abcdef0123456789abcdef01234567" + mail: "admin@example.com" + password: "admin" + + hostnames: + main: "netbox" + worker: "netbox-worker" + housekeeper: "netbox-housekeeper" + + napalm: + user: "" + password: "" + timeout: 10 + + base_dir: "/srv/netbox" + media_root: "/opt/netbox/netbox/media" + + + volumes: + main: + startup_scripts: "startup_scripts" + initializers: "initializers" + configuration: "configuration" + reports: "reports" + scripts: "scripts" + media_files: "netbox-media-files" + data: + postgres: "netbox-postgres-data" + redis: "netbox-redis-data" + + + envs: + netbox: "env/netbox.env" + postgres: "env/postgres.env" + redis: "env/redis.env" + redis_cache: "env/redis-cache.env" + + + services: + redis: + main: + password: "H733Kdjndks82" + hostname: "redis" + + cache: + password: "T4Ph722qJ5QHeQ1qfu36" + hostname: "redis-cache" + + postgres: + db: "netbox" + user: "netbox" + password: "J5brHrAXFLQSif0K" + hostname: "postgres" \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..5f69bdb --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,13 @@ +--- + +- name: install required packages + ansible.builtin.package: + name: + - docker + - docker-compose + state: present + +- name: restart netbox + docker_compose: + project_src: "{{ netbox.conf.base_dir }}" + restarted: true \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..b19cf83 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,22 @@ +--- + + +- name: create netbox directory + file: + path: "{{ netbox.conf.base_dir }}/env" + state: directory + recurse: true + notify: install required packages + +- name: update environment files + template: + src: "{{ item }}.j2" + dest: "{{ netbox.conf.base_dir }}/{{ item }}" + loop: + - env/netbox.env + - env/postgres.env + - env/redis.env + - env/redis-cache.env + - docker-compose.yml + - docker-compose.override.yml + notify: restart netbox diff --git a/templates/docker-compose.override.yml.j2 b/templates/docker-compose.override.yml.j2 new file mode 100644 index 0000000..2126f0f --- /dev/null +++ b/templates/docker-compose.override.yml.j2 @@ -0,0 +1,5 @@ +version: '3.4' +services: + netbox: + ports: + - "8080:8080" \ No newline at end of file diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 new file mode 100644 index 0000000..76069b0 --- /dev/null +++ b/templates/docker-compose.yml.j2 @@ -0,0 +1,77 @@ +version: '3.4' +services: + netbox: &netbox + image: netboxcommunity/netbox:${VERSION-v3.1-1.5.1} + hostname: {{ netbox.conf.hostnames.main }} + depends_on: + - postgres + - redis + - redis-cache + - netbox-worker + env_file: {{ netbox.envs.netbox }} + user: 'unit:root' + volumes: + - {{ netbox.volumes.main.startup_scripts }}:/opt/netbox/startup_scripts:z,ro + - {{ netbox.volumes.main.initializers }}:/opt/netbox/initializers:z,ro + - {{ netbox.volumes.main.configuration }}:/etc/netbox/config:z,ro + - {{ netbox.volumes.main.reports }}:/etc/netbox/reports:z,ro + - {{ netbox.volumes.main.scripts }}:/etc/netbox/scripts:z,ro + - {{ netbox.volumes.main.media_files }}:/opt/netbox/netbox/media:z + netbox-worker: + <<: *netbox + hostname: {{ netbox.conf.hostnames.worker }} + depends_on: + - redis + - postgres + command: + - /opt/netbox/venv/bin/python + - /opt/netbox/netbox/manage.py + - rqworker + netbox-housekeeping: + <<: *netbox + hostname: {{ netbox.conf.hostnames.housekeeper }} + depends_on: + - redis + - postgres + command: + - /opt/netbox/housekeeping.sh + + # postgres + postgres: + image: postgres:14-alpine + hostname: {{ netbox.services.postgres.hostname }} + env_file: {{ netbox.envs.postgres }} + volumes: + - {{ netbox.volumes.data.postgres }}:/var/lib/postgresql/data +# {{ spam_login }} + + # redis + redis: + image: redis:6-alpine + hostname: {{ netbox.services.redis.main.hostname }} + command: + - sh + - -c # this is to evaluate the $REDIS_PASSWORD from the env + - redis-server --appendonly yes --requirepass $$REDIS_PASSWORD ## $$ because of docker-compose + env_file: {{ netbox.envs.redis }} + volumes: + - {{ netbox.volumes.data.redis }}:/data + redis-cache: + image: redis:6-alpine + hostname: {{ netbox.services.redis.cache.hostname }} + command: + - sh + - -c # this is to evaluate the $REDIS_PASSWORD from the env + - redis-server --requirepass $$REDIS_PASSWORD ## $$ because of docker-compose + env_file: {{ netbox.envs.redis_cache }} + +volumes: + {% for key, volume in netbox.volumes.main.items() -%} + {{ volume }}: + driver: local + {% endfor -%} + + {% for key, volume in netbox.volumes.data.items() -%} + {{ volume }}: + driver: local + {% endfor %} diff --git a/templates/env/netbox.env.j2 b/templates/env/netbox.env.j2 new file mode 100644 index 0000000..e6977a1 --- /dev/null +++ b/templates/env/netbox.env.j2 @@ -0,0 +1,46 @@ +CORS_ORIGIN_ALLOW_ALL=True +DB_HOST={{ netbox.services.postgres.hostname }} +DB_NAME={{ netbox.services.postgres.db }} +DB_PASSWORD={{ netbox.services.postgres.password }} +DB_USER={{ netbox.services.postgres.user }} +EMAIL_FROM={{ netbox.conf.mail.from }} +EMAIL_PASSWORD={{ netbox.conf.mail.password }} +EMAIL_PORT={{ netbox.conf.mail.port }} +EMAIL_SERVER={{ netbox.conf.mail.host }} +EMAIL_SSL_CERTFILE={{ netbox.conf.mail.ssl.certfile }} +EMAIL_SSL_KEYFILE={{ netbox.conf.mail.ssl.keyfile }} +EMAIL_TIMEOUT={{ netbox.conf.mail.timeout }} +EMAIL_USERNAME={{ netbox.conf.mail.user }} +# EMAIL_USE_SSL and EMAIL_USE_TLS are mutually exclusive, i.e. they can't both be `true`! +EMAIL_USE_SSL={{ netbox.conf.mail.ssl.enabled }} +EMAIL_USE_TLS={{ netbox.conf.mail.tls }} +HOUSEKEEPING_INTERVAL=86400 +MAX_PAGE_SIZE=1000 +MEDIA_ROOT={{ netbox.conf.media_root }} +METRICS_ENABLED={{ netbox.conf.metrics }} +NAPALM_PASSWORD={{ netbox.conf.napalm.password }} +NAPALM_TIMEOUT={{ netbox.conf.napalm.timeout }} +NAPALM_USERNAME={{ netbox.conf.napalm.user }} +REDIS_CACHE_DATABASE=1 +REDIS_CACHE_HOST={{ netbox.services.redis.cache.hostname }} +REDIS_CACHE_INSECURE_SKIP_TLS_VERIFY=false +REDIS_CACHE_PASSWORD={{ netbox.services.redis.cache.password }} +REDIS_CACHE_SSL=false +REDIS_DATABASE=0 +REDIS_HOST={{ netbox.services.redis.main.hostname }} +REDIS_INSECURE_SKIP_TLS_VERIFY=false +REDIS_PASSWORD={{ netbox.services.redis.main.password }} +REDIS_SSL=false +RELEASE_CHECK_URL=https://api.github.com/repos/netbox-community/netbox/releases +SECRET_KEY={{ netbox.conf.secretkey }} +SKIP_STARTUP_SCRIPTS=false +SKIP_SUPERUSER=false +SUPERUSER_API_TOKEN={{ netbox.conf.user.superuser.api_token }} +SUPERUSER_EMAIL={{ netbox.conf.user.superuser.mail }} +SUPERUSER_NAME={{ netbox.conf.user.superuser.name }} +SUPERUSER_PASSWORD={{ netbox.conf.user.superuser.password }} +WEBHOOKS_ENABLED=true + +DEBUG=true +INTERNAL_IPS=* +ALLOWED_HOSTS=* \ No newline at end of file diff --git a/templates/env/postgres.env.j2 b/templates/env/postgres.env.j2 new file mode 100644 index 0000000..c0204a8 --- /dev/null +++ b/templates/env/postgres.env.j2 @@ -0,0 +1,3 @@ +POSTGRES_DB={{ netbox.services.postgres.db }} +POSTGRES_PASSWORD={{ netbox.services.postgres.password }} +POSTGRES_USER={{ netbox.services.postgres.user }} diff --git a/templates/env/redis-cache.env.j2 b/templates/env/redis-cache.env.j2 new file mode 100644 index 0000000..3d5ec08 --- /dev/null +++ b/templates/env/redis-cache.env.j2 @@ -0,0 +1 @@ +REDIS_PASSWORD={{ netbox.services.redis.cache.password }} diff --git a/templates/env/redis.env.j2 b/templates/env/redis.env.j2 new file mode 100644 index 0000000..ae3ce57 --- /dev/null +++ b/templates/env/redis.env.j2 @@ -0,0 +1 @@ +REDIS_PASSWORD={{ netbox.services.redis.main.password }}