Puppet Class: profile::netbox::db

Defined in:
modules/profile/manifests/netbox/db.pp

Overview

SPDX-License-Identifier: Apache-2.0 Class: profile::netbox::db

This profile installs all the Netbox related database things.

Actions:

deploy and configure Postgresql for Netbox (or a synchronous replica)

Requires:

Sample Usage:

include profile::netbox::db

Parameters:

  • primary (String) (defaults to: lookup('profile::netbox::db::primary'))
  • password (String) (defaults to: lookup('profile::netbox::db::password'))
  • replication_password (String) (defaults to: lookup('profile::netbox::db::replication_password'))
  • dump_interval (String) (defaults to: lookup('profile::netbox::db::dump_interval'))
  • replicas (Array[Stdlib::Host]) (defaults to: lookup('profile::netbox::db::replicas'))
  • frontends (Array[Stdlib::Host]) (defaults to: lookup('profile::netbox::db::frontends'))
  • ipv6_ok (Boolean) (defaults to: lookup('profile::netbox::db::ipv6_ok'))
  • do_backups (Boolean) (defaults to: lookup('profile::netbox::db::do_backup'))


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'modules/profile/manifests/netbox/db.pp', line 14

class profile::netbox::db (
    String              $primary              = lookup('profile::netbox::db::primary'),
    String              $password             = lookup('profile::netbox::db::password'),
    String              $replication_password = lookup('profile::netbox::db::replication_password'),
    String              $dump_interval        = lookup('profile::netbox::db::dump_interval'),
    Array[Stdlib::Host] $replicas             = lookup('profile::netbox::db::replicas'),
    Array[Stdlib::Host] $frontends            = lookup('profile::netbox::db::frontends'),
    Boolean             $ipv6_ok              = lookup('profile::netbox::db::ipv6_ok'),
    Boolean             $do_backups           = lookup('profile::netbox::db::do_backup'),
) {
    # Inspired by modules/puppetprimary/manifests/puppetdb/database.pp
    if $primary == $facts['networking']['fqdn'] {
        # We do this for the require in postgres::db
        $require_class = 'postgresql::master'
        class { '::postgresql::master':
            root_dir => '/srv/postgres',
            use_ssl  => true,
        }
        $on_primary = true

        $replicas.each |$secondary| {
            $sec_ip4 = ipresolve($secondary, 4)

            # Main replication user
            postgresql::user { "replication@${secondary}-ipv4":
                ensure   => present,
                user     => 'replication',
                database => 'replication',
                password => $replication_password,
                cidr     => "${sec_ip4}/32",
                master   => $on_primary,
                attrs    => 'REPLICATION',
            }

            # User for standby netbox server to query the primary DB
            postgresql::user { "netbox@${secondary}-ipv4":
                ensure   => present,
                user     => 'netbox',
                database => 'netbox',
                password => $password,
                cidr     => "${sec_ip4}/32",
                master   => $on_primary,
            }

            if $ipv6_ok {
                $sec_ip6 = ipresolve($secondary, 6)
                postgresql::user { "replication@${secondary}-ipv6":
                    ensure   => present,
                    user     => 'replication',
                    database => 'replication',
                    password => $replication_password,
                    cidr     => "${sec_ip6}/128",
                    master   => $on_primary,
                    attrs    => 'REPLICATION',
                }
                # User for monitoring check running on secondary server
                # who needs replication user rights and uses IPv6 (T185504)
                postgresql::user { "replication-monitoring@${secondary}-ipv6":
                    ensure   => present,
                    user     => 'replication',
                    database => 'netbox',
                    password => $replication_password,
                    cidr     => "${sec_ip6}/128",
                    master   => $on_primary,
                }
            }

        }

        if !empty($frontends) {
            $frontends_ferm = join($frontends, ' ')

            ferm::service { 'netbox_fe':
                proto  => 'tcp',
                port   => '5432',
                srange => "(@resolve((${frontends_ferm})) @resolve((${frontends_ferm}), AAAA))",
            }
        }

        $frontends.each |$frontend| {
            # this cannot fail
            $fe_ip4 = ipresolve($frontend, 4)

            postgresql::user { "netbox@${frontend}-ipv4":
                ensure   => present,
                user     => 'netbox',
                database => 'netbox',
                password => $password,
                cidr     => "${fe_ip4}/32",
                master   => $on_primary,
            }
            if $ipv6_ok {
                $fe_ip6 = ipresolve($frontend, 6)
                postgresql::user { "netbox@${frontend}-ipv6":
                    ensure   => present,
                    user     => 'netbox',
                    database => 'netbox',
                    password => $password,
                    cidr     => "${fe_ip6}/128",
                    master   => $on_primary,
                }
            }
        }

        # Create the netbox user for localhost
        # This works on every server and is used for read-only db lookups
        postgresql::user { 'netbox@localhost':
            ensure   => present,
            user     => 'netbox',
            database => 'netbox',
            password => $password,
            master   => $on_primary,
        }

        # Create the database
        postgresql::db { 'netbox':
            owner   => 'netbox',
            require => Class[$require_class],
        }
        postgresql::user { 'prometheus@localhost':
            user     => 'prometheus',
            database => 'postgres',
            type     => 'local',
            method   => 'peer',
        }

        if !empty($replicas) {
            $replicas_ferm = join($replicas, ' ')
            # Access to postgres primary from postgres replicas
            ferm::service { 'netbox_postgres':
                proto  => 'tcp',
                port   => '5432',
                srange => "(@resolve((${replicas_ferm})) @resolve((${replicas_ferm}), AAAA))",
            }
        }
        # On the primary node, do a daily DB dump
        class { '::postgresql::backup':
            do_backups    => $do_backups,
        }
    } else {
        $require_class = 'postgresql::slave'
        class { '::postgresql::slave':
            master_server    => $primary,
            root_dir         => '/srv/postgres',
            replication_pass => $replication_password,
            use_ssl          => true,
            rep_app          => "replication-${::hostname}"
        }
        $on_primary = false

        class { '::postgresql::slave::monitoring':
            pg_master   => $primary,
            pg_user     => 'replication',
            pg_password => $replication_password,
            pg_database => 'netbox',
            description => 'netbox Postgres',
        }
        # On secondary nodes, do an hourly DB dump, keep 2 days of history
        class { '::postgresql::backup':
            do_backups    => $do_backups,
            dump_interval => $dump_interval,
            rotate_days   => 2
        }
    }
    if $do_backups {
        include ::profile::backup::host
        backup::set { 'netbox-postgres': }
    }
}