Puppet Class: profile::netbox::postgres

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

Overview

Class: profile::netbox::postgres

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::postgres

Parameters:

  • db_primary (String) (defaults to: lookup('profile::netbox::db::primary'))
  • db_password (String) (defaults to: lookup('profile::netbox::db::password'))
  • replication_password (String) (defaults to: lookup('profile::netbox::db::replication_password'))
  • db_secondaries (Array[Stdlib::Host]) (defaults to: lookup('profile::netbox::db::secondaries', {'default_value' => []}))
  • frontends (Array[Stdlib::Host]) (defaults to: lookup('profile::netbox::frontends', {'default_value' => []}))
  • ipv6_ok (Boolean) (defaults to: lookup('profile::netbox::db::ipv6_ok', {'default_value' => true}))
  • do_backups (Boolean) (defaults to: lookup('profile::netbox::backup', {'default_value' => true}))


13
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
# File 'modules/profile/manifests/netbox/postgres.pp', line 13

class profile::netbox::postgres (
    String $db_primary = lookup('profile::netbox::db::primary'),
    # private data
    String $db_password = lookup('profile::netbox::db::password'),
    String $replication_password = lookup('profile::netbox::db::replication_password'),
    #
    Array[Stdlib::Host] $db_secondaries = lookup('profile::netbox::db::secondaries', {'default_value' => []}),
    Array[Stdlib::Host] $frontends = lookup('profile::netbox::frontends', {'default_value' => []}),
    Boolean $ipv6_ok = lookup('profile::netbox::db::ipv6_ok', {'default_value' => true}),
    Boolean $do_backups = lookup('profile::netbox::backup', {'default_value' => true})
) {
    # Inspired by modules/puppetprimary/manifests/puppetdb/database.pp
    if $db_primary == $::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

        $db_secondaries.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 => $db_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 => $db_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 => $db_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 => $db_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($db_secondaries) {
            $secondaries_ferm = join($db_secondaries, ' ')
            # Access to postgres primary from postgres secondaries
            ferm::service { 'netbox_postgres':
                proto  => 'tcp',
                port   => '5432',
                srange => "(@resolve((${secondaries_ferm})) @resolve((${secondaries_ferm}), AAAA))",
            }
        }
    } else {
        $require_class = 'postgresql::slave'
        class { '::postgresql::slave':
            master_server    => $db_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   => $db_primary,
            pg_user     => 'replication',
            pg_password => $replication_password,
            pg_database => 'netbox',
            description => 'netbox Postgres',
        }
    }

    if $do_backups {
      # Have backups because Netbox is used as a source of truth (T190184)
      include ::profile::backup::host
      backup::set { 'netbox-postgres': }
      class { '::postgresql::backup': }
    }
}