diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..f338d00 --- /dev/null +++ b/.flake8 @@ -0,0 +1,7 @@ +[flake8] +# Recommend matching the black line length (default 88), +# rather than using the flake8 default of 79: +max-line-length = 88 +extend-ignore = + # See https://github.com/PyCQA/pycodestyle/issues/373 + E203, diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 59900ff..81c9de1 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,3 +1,4 @@ +--- ko_fi: sleepingkyoto custom: - "https://monappy.jp/u/lae" diff --git a/.gitignore b/.gitignore index c8895fc..69c335d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .vscode/ .history *.retry -*.code-workspace \ No newline at end of file +*.code-workspace +*.sw? diff --git a/.yamllint b/.yamllint index 3a2255e..0e7de4f 100644 --- a/.yamllint +++ b/.yamllint @@ -1,3 +1,4 @@ +--- extends: default rules: @@ -11,3 +12,5 @@ rules: # NOTE(retr0h): Templates no longer fail this lint rule. # Uncomment if running old Molecule templates. # truthy: disable + +ignore: .travis.yml diff --git a/defaults/main.yml b/defaults/main.yml index 9f06b4b..5c7371b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -11,15 +11,15 @@ netbox_git_uri: "https://github.com/netbox-community/netbox.git" netbox_install_epel: true netbox_superuser_username: admin -#netbox_superuser_password: changeme +# netbox_superuser_password: changeme netbox_superuser_email: admin@localhost netbox_database: netbox netbox_database_user: netbox -#netbox_database_password: changeme -#netbox_database_host: localhost +# netbox_database_password: changeme +# netbox_database_host: localhost netbox_database_port: 5432 -#netbox_database_socket: /var/run/postgresql +# netbox_database_socket: /var/run/postgresql netbox_database_conn_age: 0 netbox_redis_host: 127.0.0.1 @@ -41,12 +41,12 @@ netbox_metrics_dir: netbox_metrics netbox_metrics_path: "/run/{{ netbox_metrics_dir }}" netbox_config: - #SECRET_KEY: + # SECRET_KEY: ALLOWED_HOSTS: - localhost - 127.0.0.1 - #NAPALM_USERNAME: - #NAPALM_PASSWORD: + # NAPALM_USERNAME: + # NAPALM_PASSWORD: MEDIA_ROOT: "{{ netbox_shared_path }}/media" REPORTS_ROOT: "{{ netbox_shared_path }}/reports" SCRIPTS_ROOT: "{{ netbox_shared_path }}/scripts" diff --git a/examples/netbox_config.yml b/examples/netbox_config.yml index d90ddff..0eaaf84 100644 --- a/examples/netbox_config.yml +++ b/examples/netbox_config.yml @@ -1,6 +1,6 @@ --- netbox_config: - #SECRET_KEY: "hLk3ED%o#P0teb&UjKN)Quvr=SW$HcM^Z(Rzy_sBXFfpY+5TVC" # let this be generated by the role so you don't have to store it + # SECRET_KEY: "hLk3ED%o#P0teb&UjKN)Quvr=SW$HcM^Z(Rzy_sBXFfpY+5TVC" # let this be generated by the role so you don't have to store it ALLOWED_HOSTS: - 127.0.0.1 - localhost @@ -17,22 +17,22 @@ netbox_config: - Dale Gribble - dgribble@example.com # note that the array is nested above - BANNER_BOTTOM: *BANNER_TOP + BANNER_BOTTOM: '*BANNER_TOP' BANNER_LOGIN: '' BANNER_TOP: &BANNER_TOP 'Your banner text' # you probably won't need this, but you can reference other variables like above BASE_PATH: netbox/ CACHE_TIMEOUT: 900 CHANGELOG_RETENTION: 90 - CORS_ORIGIN_ALLOW_ALL: False - #CORS_ORIGIN_REGEX_WHITELIST: + CORS_ORIGIN_ALLOW_ALL: false + # CORS_ORIGIN_REGEX_WHITELIST: CORS_ORIGIN_WHITELIST: - hostname.domain.example DATE_FORMAT: N j, Y DATETIME_FORMAT: 'N j, Y g:i a' - DEBUG: yes + DEBUG: true # yes, no, false, true, False, True are all valid booleans in Ansible - they will be inserted correctly in configuration.py - ENFORCE_GLOBAL_UNIQUE: False + ENFORCE_GLOBAL_UNIQUE: false EMAIL: SERVER: localhost PORT: 25 @@ -48,7 +48,7 @@ netbox_config: - ::1 LOGGING: version: 1 - disable_existing_loggers: False + disable_existing_loggers: false handlers: console: class: logging.StreamHandler @@ -57,19 +57,19 @@ netbox_config: handlers: - console level: INFO - LOGIN_REQUIRED: yes + LOGIN_REQUIRED: true LOGIN_TIMEOUT: 1209600 - MAINTENANCE_MODE: False + MAINTENANCE_MODE: false MAX_PAGE_SIZE: 500 MEDIA_ROOT: /srv/netbox_media - METRICS_ENABLED: True + METRICS_ENABLED: true NAPALM_USERNAME: netbox NAPALM_PASSWORD: NetBox42 NAPALM_TIMEOUT: 30 NAPALM_ARGS: keepalive: 60 PAGINATE_COUNT: 100 - PREFER_IPV4: False + PREFER_IPV4: false REPORTS_ROOT: /srv/netbox_reports SESSION_FILE_PATH: "{{ netbox_shared_path }}/sessions" # quotes aren't necessary as you can see - but in my opinion quotes make this more readable @@ -78,4 +78,4 @@ netbox_config: SHORT_TIME_FORMAT: 'H:i:s' TIME_FORMAT: 'g:i a' TIME_ZONE: UTC - WEBHOOKS_ENABLED: False + WEBHOOKS_ENABLED: false diff --git a/examples/playbook_single_host_deploy.yml b/examples/playbook_single_host_deploy.yml index 58cef67..a48afa1 100644 --- a/examples/playbook_single_host_deploy.yml +++ b/examples/playbook_single_host_deploy.yml @@ -1,6 +1,6 @@ --- - hosts: all - become: yes + become: true roles: - geerlingguy.postgresql - davidwittman.redis @@ -33,24 +33,24 @@ - rhel-*-extras-rpms when: "ansible_distribution == 'Red Hat Enterprise Linux'" - block: - - name: Set PostgreSQL facts to use PG10 for RedHat distributions - set_fact: - postgresql_version: 10 - postgresql_packages: - - postgresql10 - - postgresql10-server - - postgresql10-contrib - - postgresql10-libs - postgresql_data_dir: /var/lib/pgsql/10/data - postgresql_bin_path: /usr/pgsql-10/bin - postgresql_config_path: /var/lib/pgsql/10/data - postgresql_daemon: postgresql-10 - - name: Install the EPEL yum repository - yum: - name: epel-release - - name: Install the PostgreSQL yum repository - yum: - name: "https://download.postgresql.org/pub/repos/yum/reporpms/EL-{{ ansible_distribution_major_version }}-x86_64/pgdg-redhat-repo-latest.noarch.rpm" + - name: Set PostgreSQL facts to use PG10 for RedHat distributions + set_fact: + postgresql_version: 10 + postgresql_packages: + - postgresql10 + - postgresql10-server + - postgresql10-contrib + - postgresql10-libs + postgresql_data_dir: /var/lib/pgsql/10/data + postgresql_bin_path: /usr/pgsql-10/bin + postgresql_config_path: /var/lib/pgsql/10/data + postgresql_daemon: postgresql-10 + - name: Install the EPEL yum repository + yum: + name: epel-release + - name: Install the PostgreSQL yum repository + yum: + name: "https://download.postgresql.org/pub/repos/yum/reporpms/EL-{{ ansible_distribution_major_version }}-x86_64/pgdg-redhat-repo-latest.noarch.rpm" when: "ansible_os_family == 'RedHat' and ansible_distribution_major_version =='7'" # You can use this playbook as-is to setup a full NetBox server, just change diff --git a/handlers/main.yml b/handlers/main.yml index 379edb1..d5f0f2e 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -4,13 +4,13 @@ systemd: name: netbox.socket state: restarted - daemon_reload: yes + daemon_reload: true - name: restart netbox.service systemd: name: netbox.service state: restarted - daemon_reload: yes + daemon_reload: true - name: reload netbox.service systemd: @@ -21,4 +21,4 @@ systemd: name: netbox-rqworker.service state: restarted - daemon_reload: yes \ No newline at end of file + daemon_reload: true diff --git a/meta/main.yml b/meta/main.yml index d6b2e2b..cc686f5 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,3 +1,4 @@ +--- galaxy_info: author: Musee Ullah description: Installs and configures NetBox, a DCIM suite, in a production setting. diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 4af8e91..033dc38 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -5,9 +5,11 @@ driver: name: vagrant provider: name: virtualbox -lint: - name: yamllint - enabled: false +lint: | + set -e + yamllint . + ansible-lint + flake8 platforms: - name: debian-9 box: generic/debian9 @@ -21,5 +23,6 @@ scenario: name: default verifier: name: testinfra - lint: - name: flake8 + lint: | + set -e + flake8 diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml index 65dc19b..1f20ae6 100644 --- a/molecule/default/playbook.yml +++ b/molecule/default/playbook.yml @@ -18,7 +18,7 @@ netbox_database_socket: "{{ postgresql_unix_socket_directories[0] }}" # webhooks netbox_webhooks_enabled: true - #db + # db postgresql_users: - name: "{{ netbox_database_user }}" role_attr_flags: CREATEDB,NOSUPERUSER diff --git a/tasks/deploy_netbox.yml b/tasks/deploy_netbox.yml index dfee205..8d22fe8 100644 --- a/tasks/deploy_netbox.yml +++ b/tasks/deploy_netbox.yml @@ -32,7 +32,7 @@ extra_args: "-c {{ netbox_current_path }}/constraints.txt" virtualenv: "{{ netbox_virtualenv_path }}" virtualenv_command: "{{ netbox_python_binary }} -m venv" - become: True + become: true become_user: "{{ netbox_user }}" retries: 2 register: _netbox_virtualenv_setup @@ -43,7 +43,7 @@ name: "{{ item }}" state: present virtualenv: "{{ netbox_virtualenv_path }}" - become: True + become: true become_user: "{{ netbox_user }}" retries: 2 register: _netbox_pip_additional_install @@ -116,31 +116,31 @@ loop: "{{ netbox_reports }}" - block: - - name: Run database migrations for NetBox - django_manage: - command: migrate - app_path: "{{ netbox_current_path }}/netbox" - virtualenv: "{{ netbox_virtualenv_path }}" - - - name: Create a super user for NetBox - shell: "printf '{{ netbox_superuser_script }}' |\ - {{ netbox_virtualenv_path }}/bin/python {{ netbox_current_path }}/netbox/manage.py shell" - register: _netbox_superuser_result - changed_when: "'changed' in _netbox_superuser_result.stdout" - when: - - not netbox_ldap_enabled - - - name: Generate static assets for NetBox - django_manage: - command: collectstatic - app_path: "{{ netbox_current_path }}/netbox" - virtualenv: "{{ netbox_virtualenv_path }}" - - - name: Clear expired user sessions in NetBox - django_manage: - command: clearsessions - app_path: "{{ netbox_current_path }}/netbox" - virtualenv: "{{ netbox_virtualenv_path }}" - - become: True + - name: Run database migrations for NetBox + django_manage: + command: migrate + app_path: "{{ netbox_current_path }}/netbox" + virtualenv: "{{ netbox_virtualenv_path }}" + + - name: Create a super user for NetBox + shell: "printf '{{ netbox_superuser_script }}' |\ + {{ netbox_virtualenv_path }}/bin/python {{ netbox_current_path }}/netbox/manage.py shell" + register: _netbox_superuser_result + changed_when: "'changed' in _netbox_superuser_result.stdout" + when: + - not netbox_ldap_enabled + + - name: Generate static assets for NetBox + django_manage: + command: collectstatic + app_path: "{{ netbox_current_path }}/netbox" + virtualenv: "{{ netbox_virtualenv_path }}" + + - name: Clear expired user sessions in NetBox + django_manage: + command: clearsessions + app_path: "{{ netbox_current_path }}/netbox" + virtualenv: "{{ netbox_virtualenv_path }}" + + become: true become_user: "{{ netbox_user }}" diff --git a/tasks/generate_secret_key.yml b/tasks/generate_secret_key.yml index 943f1de..d8611b0 100644 --- a/tasks/generate_secret_key.yml +++ b/tasks/generate_secret_key.yml @@ -5,7 +5,7 @@ args: creates: "{{ netbox_shared_path }}/generated_secret_key" executable: /bin/bash - become: True + become: true become_user: "{{ netbox_user }}" - name: Load saved SECRET_KEY diff --git a/tasks/install_packages_apt.yml b/tasks/install_packages_apt.yml index c2ac584..e402430 100644 --- a/tasks/install_packages_apt.yml +++ b/tasks/install_packages_apt.yml @@ -4,7 +4,7 @@ name: "{{ _netbox_apt_packages | flatten }}" state: present cache_valid_time: 3600 - update_cache: yes + update_cache: true vars: _netbox_apt_packages: - "{{ netbox_python_packages }}" diff --git a/tasks/install_packages_dnf.yml b/tasks/install_packages_dnf.yml index ddf6590..a672fe1 100644 --- a/tasks/install_packages_dnf.yml +++ b/tasks/install_packages_dnf.yml @@ -3,7 +3,7 @@ dnf: name: "{{ _netbox_dnf_packages | flatten }}" state: present - update_cache: yes + update_cache: true vars: _netbox_dnf_packages: - "{{ 'epel-release' if netbox_install_epel else [] }}" @@ -12,5 +12,3 @@ - "{{ netbox_ldap_packages if netbox_ldap_enabled else [] }}" - "{{ 'git' if netbox_git else [] }}" - "{{ 'acl' if ('SUDO_USER' in ansible_env and ansible_env.SUDO_USER != 'root') else [] }}" - - diff --git a/tasks/install_packages_yum.yml b/tasks/install_packages_yum.yml index 66ffaf0..b74115f 100644 --- a/tasks/install_packages_yum.yml +++ b/tasks/install_packages_yum.yml @@ -9,7 +9,7 @@ yum: name: "{{ _netbox_yum_packages | flatten }}" state: present - update_cache: yes + update_cache: true vars: _netbox_yum_packages: - "{{ netbox_python_packages }}" diff --git a/tasks/install_via_git.yml b/tasks/install_via_git.yml index 7765934..0885f37 100644 --- a/tasks/install_via_git.yml +++ b/tasks/install_via_git.yml @@ -5,7 +5,7 @@ dest: "{{ netbox_git_repo_path }}" version: "{{ netbox_git_version }}" register: _netbox_git_repo - become: True + become: true become_user: "{{ netbox_user }}" - name: Create git deployment directory for NetBox @@ -21,7 +21,7 @@ chdir: "{{ netbox_git_repo_path }}" executable: /bin/bash register: _netbox_git_contains_issue_2239_fix - changed_when: False + changed_when: false failed_when: "_netbox_git_contains_issue_2239_fix.rc not in [0, 1]" - name: Check existence of commit 3590ed3, renaming webhooks to tasks @@ -30,7 +30,7 @@ chdir: "{{ netbox_git_repo_path }}" executable: /bin/bash register: _netbox_git_contains_tasks_rename - changed_when: False + changed_when: false failed_when: "_netbox_git_contains_tasks_rename.rc not in [0, 1]" - name: Archive and extract snapshot of git repository @@ -42,7 +42,7 @@ - reload netbox.service when: - _netbox_git_repo is changed - become: True + become: true become_user: "{{ netbox_user }}" - name: Symlink git repository to current NetBox directory diff --git a/tasks/install_via_stable.yml b/tasks/install_via_stable.yml index e295672..74fe093 100644 --- a/tasks/install_via_stable.yml +++ b/tasks/install_via_stable.yml @@ -6,7 +6,7 @@ creates: "{{ netbox_stable_path }}" owner: "{{ netbox_user }}" group: "{{ netbox_group }}" - remote_src: True + remote_src: true - name: Symlink stable release to current NetBox directory file: diff --git a/tasks/load_variables.yml b/tasks/load_variables.yml index a13a8e0..58592da 100644 --- a/tasks/load_variables.yml +++ b/tasks/load_variables.yml @@ -53,5 +53,5 @@ - name: Capture the current Python interpreter set_fact: - _netbox_global_python: "{{ ansible_python_interpreter }}" + _netbox_global_python: "{{ ansible_python_interpreter }}" when: ansible_python_interpreter is defined diff --git a/tasks/main.yml b/tasks/main.yml index 3e9693d..60b5476 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -44,7 +44,7 @@ name: "{{ netbox_database }}" login_user: "{{ netbox_database_user }}" login_unix_socket: "{{ netbox_database_socket }}" - become: True + become: true become_user: "{{ netbox_database_user }}" when: - netbox_database_socket is defined @@ -109,7 +109,7 @@ systemd: name: "{{ item }}" state: started - enabled: yes + enabled: true loop: - netbox.socket - netbox.service @@ -118,11 +118,11 @@ systemd: name: netbox-rqworker.service state: started - enabled: yes + enabled: true - name: Restore the previous Ansible Python interpreter set_fact: - ansible_python_interpreter: "{{ _netbox_global_python if _netbox_global_python is defined else 'auto_legacy' }}" + ansible_python_interpreter: "{{ _netbox_global_python if _netbox_global_python is defined else 'auto_legacy' }}" - name: Recheck the previous Python interpreter's version setup: diff --git a/tasks/validate_variables.yml b/tasks/validate_variables.yml index 53461c1..74ca52a 100644 --- a/tasks/validate_variables.yml +++ b/tasks/validate_variables.yml @@ -58,13 +58,13 @@ msg: "Please ensure that your script/report module names start with a lowercase letter and contain only lowercase letters, numbers, and underscores." - block: - - name: Identify selected storage module - set_fact: - _netbox_storages_module: "{{ netbox_config.STORAGE_BACKEND | regex_search('(?<=storages\\.backends\\.).*(?=\\.)') }}" + - name: Identify selected storage module + set_fact: + _netbox_storages_module: "{{ netbox_config.STORAGE_BACKEND | regex_search('(?<=storages\\.backends\\.).*(?=\\.)') }}" - - name: Ensure storage module is a valid option - assert: - that: - - _netbox_storages_module in _netbox_storages_map - msg: "Please ensure your STORAGE_BACKEND is correct." + - name: Ensure storage module is a valid option + assert: + that: + - _netbox_storages_module in _netbox_storages_map + msg: "Please ensure your STORAGE_BACKEND is correct." when: "'STORAGE_BACKEND' in netbox_config" diff --git a/tests/test.yml b/tests/test.yml index e23fdac..be428f9 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -1,27 +1,27 @@ --- - hosts: netbox - become: True + become: true tasks: - block: - - name: uWSGI/NetBox service status + - name: uWSGI/NetBox service status # noqa 303 305 shell: "systemctl status netbox.service" - changed_when: False - - name: uWSGI/NetBox service journal + changed_when: false + - name: uWSGI/NetBox service journal # noqa 305 shell: "journalctl --no-pager -xu netbox.service" - changed_when: False - - name: uWSGI configuration + changed_when: false + - name: uWSGI configuration # noqa 305 shell: cat /srv/netbox/shared/uwsgi.ini - changed_when: False - - name: NetBox rq-worker service status + changed_when: false + - name: NetBox rq-worker service status # noqa 303 305 shell: "systemctl status netbox-rqworker.service" - changed_when: False - - name: NetBox application log + changed_when: false + - name: NetBox application log # noqa 305 shell: cat /srv/netbox/shared/application.log - changed_when: False - - name: NetBox configuration + changed_when: false + - name: NetBox configuration # noqa 305 shell: cat /srv/netbox/shared/configuration.py - changed_when: False - ignore_errors: yes + changed_when: false + ignore_errors: true - name: Ensure that NetBox returns a successful HTTP response uri: url: "http://{{ inventory_hostname }}:8080" diff --git a/vars/debian-10.yml b/vars/debian-10.yml index 30a49fd..f58ca1d 100644 --- a/vars/debian-10.yml +++ b/vars/debian-10.yml @@ -12,7 +12,7 @@ _netbox_python_packages: - python3.7-dev - python3-venv - python3-pip - - python3-psycopg2 # used by ansible's postgres modules + - python3-psycopg2 # used by ansible's postgres modules _netbox_python_binary: /usr/bin/python3.7 _netbox_ldap_packages: - libldap2-dev diff --git a/vars/debian-9.yml b/vars/debian-9.yml index 6f3c693..cf26e2c 100644 --- a/vars/debian-9.yml +++ b/vars/debian-9.yml @@ -12,7 +12,7 @@ _netbox_python_packages: - python3.5-dev - python3-venv - python3-pip - - python3-psycopg2 # used by ansible's postgres modules + - python3-psycopg2 # used by ansible's postgres modules _netbox_python_binary: /usr/bin/python3.5 _netbox_ldap_packages: - libldap2-dev diff --git a/vars/ubuntu-16.yml b/vars/ubuntu-16.yml index 2282fa3..25bcc7f 100644 --- a/vars/ubuntu-16.yml +++ b/vars/ubuntu-16.yml @@ -12,7 +12,7 @@ _netbox_python_packages: - python3.5-dev - python3-pip - python3-venv - - python3-psycopg2 # used by ansible's postgres modules + - python3-psycopg2 # used by ansible's postgres modules _netbox_python_binary: /usr/bin/python3.5 _netbox_ldap_packages: - libldap2-dev diff --git a/vars/ubuntu-18.yml b/vars/ubuntu-18.yml index 82898d1..c6e94fa 100644 --- a/vars/ubuntu-18.yml +++ b/vars/ubuntu-18.yml @@ -12,7 +12,7 @@ _netbox_python_packages: - python3.6-dev - python3-pip - python3-venv - - python3-psycopg2 # used by ansible's postgres modules + - python3-psycopg2 # used by ansible's postgres modules _netbox_python_binary: /usr/bin/python3.6 _netbox_ldap_packages: - libldap2-dev