diff --git a/defaults/main.yml b/defaults/main.yml index c284f57..e1f5f62 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,7 +1,7 @@ --- # defaults file for lae.netbox netbox_stable: false -netbox_stable_version: 2.10.10 +netbox_stable_version: 3.0.12 netbox_stable_uri: "https://github.com/netbox-community/netbox/archive/v{{ netbox_stable_version }}.tar.gz" netbox_git: false @@ -93,6 +93,9 @@ netbox_pip_constraints: # Blacklist django-rq 2.3.1 due to https://github.com/rq/django-rq/issues/421 # https://github.com/netbox-community/netbox/issues/4633 - 'django-rq!=2.3.1' + # If you're using Netbox 2.11.3 or below, uncomment the below to fix database + # migrations. https://github.com/pallets/markupsafe/issues/284 + # - 'MarkupSafe<2.1.0' netbox_keep_uwsgi_updated: false netbox_uwsgi_options: {} diff --git a/handlers/main.yml b/handlers/main.yml index d5f0f2e..f12fe30 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -22,3 +22,8 @@ name: netbox-rqworker.service state: restarted daemon_reload: true + +- name: reload netbox-rqworker.service + systemd: + name: netbox-rqworker.service + state: reloaded diff --git a/tasks/deploy_netbox.yml b/tasks/deploy_netbox.yml index c6fb86f..585db13 100644 --- a/tasks/deploy_netbox.yml +++ b/tasks/deploy_netbox.yml @@ -6,6 +6,7 @@ owner: "{{ netbox_user }}" group: "{{ netbox_group }}" loop: + - "{{ netbox_home }}" - "{{ netbox_releases_path }}" - "{{ netbox_shared_path }}" - "{{ _netbox_config.MEDIA_ROOT }}" @@ -26,12 +27,25 @@ owner: "{{ netbox_user }}" group: "{{ netbox_group }}" -- name: Create NetBox virtualenv and install needed Python dependencies +- name: Create NetBox virtualenv + pip: + name: + - pip + - setuptools + state: latest + virtualenv: "{{ netbox_virtualenv_path }}" + virtualenv_command: "{{ netbox_python_binary }} -m venv" + become: true + become_user: "{{ netbox_user }}" + retries: 2 + register: _netbox_virtualenv_setup + until: _netbox_virtualenv_setup is succeeded + +- name: Install needed Python dependencies pip: requirements: "{{ netbox_current_path }}/requirements.txt" extra_args: "-c {{ netbox_current_path }}/constraints.txt" virtualenv: "{{ netbox_virtualenv_path }}" - virtualenv_command: "{{ netbox_python_binary }} -m venv" become: true become_user: "{{ netbox_user }}" retries: 2 @@ -52,6 +66,7 @@ loop: "{{ _netbox_python_deps }}" notify: - restart netbox.service + - restart netbox-rqworker.service - name: Generate NetBox configuration file template: @@ -65,6 +80,7 @@ py_compile.compile(f, c); os.remove(c)\"" notify: - reload netbox.service + - reload netbox-rqworker.service - name: Generate LDAP configuration for NetBox if enabled template: @@ -115,6 +131,17 @@ group: "{{ netbox_group }}" loop: "{{ netbox_reports }}" +- name: Schedule daily housekeeping cronjob + cron: + name: "Netbox housekeeping" + special_time: daily + job: "{{ netbox_virtualenv_path }}/bin/python {{ netbox_current_path }}/netbox/manage.py housekeeping" + user: "{{ netbox_user }}" + cron_file: "netbox" + when: + - netbox_stable and netbox_stable_version is version('3.0.0', '>=') + or netbox_git and _netbox_git_contains_housekeeping.rc == 0 + - block: - name: Run database migrations for NetBox django_manage: @@ -174,6 +201,9 @@ command: "invalidate all" app_path: "{{ netbox_current_path }}/netbox" virtualenv: "{{ netbox_virtualenv_path }}" + when: + - netbox_stable and netbox_stable_version is version('3.0.0', '<') + or netbox_git and _netbox_git_contains_invalidate_removed.rc == 0 become: true diff --git a/tasks/install_via_git.yml b/tasks/install_via_git.yml index 6fb77ce..235d980 100644 --- a/tasks/install_via_git.yml +++ b/tasks/install_via_git.yml @@ -51,6 +51,24 @@ changed_when: False failed_when: "_netbox_git_contains_trace_paths.rc not in [0, 1]" +- name: Check existence of commit d87ec82, introducing nightly housekeeping command + shell: 'set -o pipefail; git log --format=%H "{{ netbox_git_version }}" | grep ^d87ec82fe34d92a84ee6f2a97ba0a87a53eed015' + args: + chdir: "{{ netbox_git_repo_path }}" + executable: /bin/bash + register: _netbox_git_contains_housekeeping + changed_when: False + failed_when: "_netbox_git_contains_housekeeping.rc not in [0, 1]" + +- name: Check existence of commit 028c876, removing the invalidate command + shell: 'set -o pipefail; git log --format=%H "{{ netbox_git_version }}" | grep ^028c876bcafbaede2731f191512bbebe3f1b6a9e' + args: + chdir: "{{ netbox_git_repo_path }}" + executable: /bin/bash + register: _netbox_git_contains_invalidate_removed + changed_when: False + failed_when: "_netbox_git_contains_invalidate_removed.rc not in [0, 1]" + - name: Archive and extract snapshot of git repository shell: 'set -o pipefail; git archive "{{ netbox_git_version }}" | tar -x -C "{{ netbox_git_deploy_path }}"' args: diff --git a/tasks/main.yml b/tasks/main.yml index b264aa0..ae917cd 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -18,16 +18,18 @@ # Can't quite check the effective version of a git installation this early since # the repo isn't checked out, but most users will be on develop in the first # place so this dumb check should be good enough. -- name: Ensure the installed Python version is newer than 3.6.0 for NetBox 2.8+ +- name: Ensure Python version installed is compatible with this version of Netbox assert: that: - - "ansible_python_version is version('3.6.0', '>=')" + - "ansible_python_version is version(item.python_needed, '>=')" msg: > - NetBox 2.8.0+ requires Python 3.6+. Please either specify an older NetBox - version, upgrade to a newer distribution that provides Python 3.6+, or - set netbox_python_binary to an appropriate Python 3.6+ binary. + NetBox {{ item.netbox_version_min }} requires at least Python {{ item.python_needed }}. + Please either specify an older NetBox version, upgrade to a newer + distribution that provides Python at least {{ item.python_needed }}, or + set netbox_python_binary to an appropriate Python {{ item.python_needed }} binary. when: - - netbox_stable and netbox_stable_version is version('2.8.0', '>=') or netbox_git + - netbox_stable and netbox_stable_version is version(item.netbox_version_min, '>=') or netbox_git + loop: "{{ netbox_python_compat_matrix }}" - name: Create NetBox user group group: diff --git a/vars/main.yml b/vars/main.yml index cdd93fb..17c39de 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -39,3 +39,7 @@ netbox_superuser_token: | token = Token.objects.create(user=user) print("api-token: " + token.key) print("changed") + +netbox_python_compat_matrix: + - { netbox_version_min: '2.8.0', python_needed: '3.6.0' } + - { netbox_version_min: '3.0.0', python_needed: '3.7.0' }