Difference between revisions of "How to install Netbox"

From ppwiki
Jump to navigation Jump to search
Line 176: Line 176:
 
In the files repertory we will have 4 files ( netbox, netbox.conf,gunicorn_config.py and config.sh)
 
In the files repertory we will have 4 files ( netbox, netbox.conf,gunicorn_config.py and config.sh)
 
Copy and paste the content of each of the 4 files below and place them in the files repertory  
 
Copy and paste the content of each of the 4 files below and place them in the files repertory  
#netbox
+
**netbox
  
 
  server {
 
  server {

Revision as of 14:34, 11 September 2018

NetBox is an open source web application the is design to manage a network infrastructure.

In this tutorial, we will be installing Netbox (netbox2001 (v2.4.5-dev)) using puppet for the installation and a bash script for the basic configuration.

Prerequisites

For this turtorail, we will need:

  • A puppet master node (puppet version 4.10)
  • The netbox node (Debian 9 Stretch and puppet client vesion 4.8)
  • The puppetlabs postgresql module
  • The puppetlabs-vcsrepo module

Installation

We will not convert the installation and configuration of puppet master in this tutorial. if you do not have a puppet master server in your environment, you can do all the steps below manually by following the instructions in the reference section. Once done, you can continue with this tutorial from the configuration section.

Download modules

Login to your puppet master node and issue the command below to download the puppetlabs postgresql module

sudo puppet module install puppetlabs-postgresql --version 5.9.0

This will download the postgresql moudle and save it into

/etc/puppetlabs/code/environments/production/modules$ 

Next we will download the puppetlabs vcsrepo module using the command below

sudo puppet module install puppetlabs-vcsrepo --version 2.3.0

This will download the vcsrepo moudle and save it into

/etc/puppetlabs/code/environments/production/modules$

Create the class

Let us create our netbox_install class that will be used to install Netbox and Posgresql. Under /etc/puppetlabs/code/environments/production/modules/$ create netbox directory

sudo mkdir /etc/puppetlabs/code/environments/production/modules/netbox/$ 

Then create the manifests director under the netbox directory

sudo mkdir /etc/puppetlabs/code/environments/production/modules/netbox/manifests$

Navigate to the sudo mkdir /etc/puppetlabs/code/environments/production/modules/netbox/manifests$ and create a file called netbox_install.pp

sudo vi netbox_install.pp

Copy and paste the content below into the file (netbox_install.pp)

#This class will install netbox on Debian 9
class netbox::netbox_install {
#Install needed packages
       Package { ensure => "installed"}
       package { "libpq-dev":}
       package { "python3":}
       package { "python3-dev":}
       package { "python3-setuptools":}
       package { "build-essential":}
       package { "libxml2-dev":}
       package { "libxslt1-dev":}
       package { "libffi-dev":}
       package { "graphviz":}
       package { "libssl-dev":}
       package { "zlib1g-dev":}
       package { "python3-pip":}
       package { "git":}
       package { "redis-server":}
       package { "nginx":}
       package { "supervisor":}
#Install postgresql to use as database
# Create a hash from Hiera Data with the Databases
$myPostgresDb = hiera('postgresql::server::db', {})
# With Create Resource Converts a hash into a set of resources
create_resources('postgresql::server::db', $myPostgresDb)
# Create the netbox user 
       user { 'netbox':
               ensure  => 'present',
               comment => 'netbox',
               managehome => false,
               password => 'netbox_user_password',
       }
# Create the netbox directory
       file { '/opt/netbox':
               ensure => directory,
               owner => 'netbox',
               group => 'netbox',
               mode => '0766',
      }
# Download netbox from github.com
      vcsrepo { '/opt/netox':
               ensure   => present,
               provider => git,
               source   => 'https://github.com/digitalocean/netbox.git',
               user     => 'netbox',
      }
# 
       package { 'napalm':
         ensure   => 'installed',
         provider => 'pip3',
       }
       package { 'django-rq':
         ensure => 'installed',
         provider => 'pip3',
       }
package { 'gunicorn':
         ensure => 'installed',
         provider => 'pip3',
       }
#configure Nginx
       service { 'nginx':
               ensure => running,
       }
       file { '/etc/nginx/sites-enabled/default':
               ensure => absent,
       }
       file { '/etc/nginx/sites-available/netbox':
               ensure => present,
               require => Package['nginx'],
               owner => 'root',
               group => 'root',
               mode => '0644',
               source => 'puppet:///modules/netbox/netbox',
               notify => Service['nginx'],
       }
#Configure gunicorn
       file { '/opt/netbox/gunicorn_config.py':
               ensure => present,
               owner => 'netbox',
               group => 'netbox',
               mode => '0644',
               source => 'puppet:///modules/netbox/gunicorn_config.py',
       }
#Configure supervisord
       service { 'supervisor':
               ensure => running,
       }
       file { '/etc/supervisor/conf.d/netbox.conf':
               ensure => present,
               require => Package['supervisor'],
               owner => 'root',
               group => 'root',
               mode => '0644',
               source => 'puppet:///modules/netbox/netbox.conf',
               notify => Service['supervisor'],
       }
#Create configuration file
       file { '/opt/netbox/netbox/netbox/configuration.py':
               ensure => present,
               owner => 'netbox',
               group => 'netbox',
               mode => '0644',
               content => template('netbox/configuration.erb'),
       }
}

Add the node to site.pp

On your puppet master node open the site.pp file and add the netbox node like below

node netbox2001 {
       include netbox::netbox_install
       include postgresql::server

}

The include postgresql::server will install postgresql on the node with no password.How to setup the password is convert in the hiera section below.

Setup hiera

Under your hieradata directory, create a directory called hosts

/etc/puppetlabs/code/environments/production/hieradata/hosts

Then create a yaml file with the name of the netbox node

vi netbox2001.yaml

Copy and paste the content below into the file

---
postgresql::server::postgres_password: postgre_password
postgresql::server::db:
 netbox:
   user: netbox
   password: netbox_user_passsword
postgresql::server::role:
 netbox:
   password_hash: '*EAA615A0970C05B84915C0772FD1C23831586837'
postgresql::server::database_grant:
 netbox:
   privilege: ALL
   db: netbox
   role: netbox

By default after installing Posgretsql there is no password so the first line of the code above will setup a password.In the same code we will create a database called netbox and a user for that database netbox who has full privilege on the database.

Others needed files

On your puppet master node navigate to Navigate to /etc/puppetlabs/code/environments/production/modules/netbox/ and create the repertories files and templates

cd /etc/puppetlabs/code/environments/production/modules/netbox/ 
sudo mkdir files
sudo mkdir templates
  • what's goes in the files repertory

In the files repertory we will have 4 files ( netbox, netbox.conf,gunicorn_config.py and config.sh) Copy and paste the content of each of the 4 files below and place them in the files repertory

    • netbox
server {
   listen 80;
   server_name netbox2001.dfw.ppnet;
   client_max_body_size 25m;
   location /static/ {
       alias /opt/netbox/netbox/static/;
   }
   location / {
       proxy_pass http://127.0.0.1:8001;
       proxy_set_header X-Forwarded-Host $server_name;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-Proto $scheme;
       add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
   }
}

Change the server_name to match your environment

  1. netbox.conf
[program:netbox]
command = gunicorn -c /opt/netbox/gunicorn_config.py netbox.wsgi
directory = /opt/netbox/netbox/
user = www-data
[program:netbox-rqworker]
command = python3 /opt/netbox/netbox/manage.py rqworker
directory = /opt/netbox/netbox/
user = www-data
  1. gunicorn_config.py
  2. config.sh
  • What's goes in the templates repertory

Run puppet

Login into your netbox node and run the command

sudo puppet agent -t

If you have no errors, go to the next step to verify that we have Postgresql installed and we can login with the postgrea default user and netbox user with the password created. Verify too that we have the netbox database was create.

Verification

  • login with the defaut user postgres
psql -U postgres -W -h localhost 

When prompt, enter the password

ppaul@netbox2001:~$ psql -U postgres -W -h localhost 
Password for user postgres: 
psql (9.6.10)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=# 

Type \q to exit and test login with the netbox user

  • Login with the netbox user
ppaul@netbox2001:~$ psql -U netbox -W -h localhost 
Password for user netbox: 
psql (9.6.10)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
netbox=> 
  • verify that the netbox database exist

after loging in as the netbox user, at the prompt, type "\l"

netbox=> \l
                                 List of databases
  Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
netbox    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =T/postgres          +
          |          |          |             |             | postgres=CTc/postgres+
          |          |          |             |             | netbox=CTc/postgres
postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          |          |          |             |             | postgres=CTc/postgres
template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
          |          |          |             |             | postgres=CTc/postgres
(4 rows)
netbox=>

Configuration

Testing

Conclusion

References

https://netbox.readthedocs.io/en/stable/