ansibleを利用してnetboxをインストールする

github.com

netboxはIPアドレスの管理*1やラック、サーバ等を管理*2するソフトウェアです。

環境

# cat /etc/centos-release
CentOS Linux release 7.3.1611 (Core) 

# ansible --version
ansible 2.2.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

Roleのインストール

galaxy.ansible.com

今回はansible-galaxyを利用する方法を紹介します。

# ansible-galaxy install geerlingguy.repo-epel
# ansible-galaxy install geerlingguy.postgresql
# ansible-galaxy install geerlingguy.apache
# ansible-galaxy install erkax.netbox

Playbookの編集

netbox-install.yml

- hosts: all
  become: true
  vars:
    netbox_allowed_hosts: "'{{ ansible_fqdn }}'"
    netbox_db_name: "netbox"
    netbox_db_user: "netbox"
    netbox_db_password: "password@netbox_db"
    netbox_secret_key: "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGXYZ"
    netbox_from_email: "netbox@example.com"
    netbox_admin_password: "password@netbox"
    netbox_admin: "admin"
    netbox_admin_email: "admin.netbox@example.com"
    postgresql_databases:
      - name: netbox
        state: present
    postgresql_users:
      - name: "{{ netbox_db_user }}"
        password: "{{ netbox_db_password }}"
        db: "{{ netbox_db_name }}"
        role_attr_flags: NOSUPERUSER
    apache_vhosts_version: 2.4
    apache_vhosts:
      - servername: "{{ ansible_fqdn }}"
        documentroot: "/var/www/html"
        extra_parameters: |
          Alias /static /opt/netbox/netbox/static
          <Directory /opt/netbox/netbox/static>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Require all granted
          </Directory>
          ProxyPreserveHost On
          <Location /static>
            ProxyPass !
          </Location>
          ProxyPass / http://127.0.0.1:8001/
          ProxyPassReverse / http://127.0.0.1:8001/
  roles:
    - { role: geerlingguy.repo-epel }
    - { role: geerlingguy.postgresql }
    - { role: geerlingguy.apache }
    - { role: erkax.netbox }

事前の作業

libsemanage-python

libsemanage-python が必要ですが、こちらのRoleではインストールされません。

事前にインストールします。

# yum install -y libsemanage-python

libsemanage-pythonがインストールされていないと、Playbook実行時に以下のエラーが表示されます。

TASK [erkax.netbox : set httpd_can_network_connect selinux boolean] ************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "This module requires libsemanage-python support"}

apacheの設定

erkax.netboxのサンプルに以下の設定を追加しています。

<Directory /opt/netbox/netbox/static>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride None
  Require all granted
</Directory>

SELinux

netboxのRoleでは、sebooleanモジュールのタスクが存在します。このとき、作業対象のホストでSELinuxが無効な状態であるとエラーになります。

以下のような感じで修正することができます。

--- /etc/ansible/roles/erkax.netbox/tasks/security.yml.orig
+++ /etc/ansible/roles/erkax.netbox/tasks/security.yml
@@ -16,9 +16,14 @@
         setype=httpd_sys_rw_content_t
   tags: install, update
 ####
+- name: selinux status
+  shell: getenforce
+  register: is_enabled_selinux
+
 # allow httpd tagged process to connect to another server (proxy in this case)
 - name: set httpd_can_network_connect selinux boolean
   seboolean: name=httpd_can_network_connect
              state=yes
              persistent=yes
+  when: is_enabled_selinux == 'Enforcing'
   tags: install, update

Playbookの実行

# ansible-playbook -i localhost, -c local netbox-install.yml

スクリーンショット

ダッシュボード

f:id:biaxident:20170304120252p:plain

ラック

f:id:biaxident:20170304120346p:plain

デバイス

f:id:biaxident:20170304120607p:plain

Aggregates (1)

f:id:biaxident:20170304120748p:plain

Aggregates (2)

f:id:biaxident:20170304120815p:plain

Prefix

f:id:biaxident:20170304120848p:plain

API

以下のURLでAPIのドキュメントを確認できます。現時点では参照系のAPIが整備されていています。

http://<server>/api/docs/
$ curl http://<server>/api/dcim/manufacturers/
[{"id":1,"name":"APC","slug":"apc"},{"id":6,"name":"Arista","slug":"arista"},{"id":2,"name":"Cisco","slug":"cisco"},{"id":3,"name":"Dell","slug":"dell"},{"id":4,"name":"HP","slug":"hp"},{"id":5,"name":"Juniper","slug":"juniper"},{"id":7,"name":"Opengear","slug":"opengear"},{"id":8,"name":"Super Micro","slug":"super-micro"}]

*1:IP address management(IPAM)

*2:data center infrastructure management (DCIM)