Automatiser la mise-à-jour de Netbox avec ansible
Introduction
Voici une façon d'automatiser un update de netbox à la dernière version avec Ansible.
💡
Cette façon de faire assume que vous avez déjà fait un dump de la b.d. de postgresql, un backup de mediaet le .tar.gz de la nouvelle version de netbox
Prérequis
- Dump de la b.d postgresql sous le nom netbox-full.sql
- Vous avez déjà le fichier netbox-<version>.tar.gz
- Vous avez un backup de votre dossier media sous le nom media.tar
Dans le fichier group_vars ou host_vars vous avez mis ceci
#Version
newver: "4.6.5"
oldver: "4.6.4"Ansible
- name: Netbox Upgrade
hosts: <your group or host>
become: true
tasks:
- name: Transfer upgrade package
ansible.builtin.copy:
src: "netbox-{{ newver }}.tar.gz"
dest: "/tmp/netbox-{{ newver }}.tar.gz"
- name: Untar netbox to /opt
ansible.builtin.unarchive:
src: "/tmp/netbox-{{ newver }}.tar.gz"
dest: /opt/
remote_src: true
- name: Create a symlink for "/opt/netbox-{{ newver }}" to /opt
ansible.builtin.file:
src: "/opt/netbox-{{ newver }}"
dest: /opt/netbox
state: link
force: true
- name: Create netbox database
become: true
become_user: postgres
community.postgresql.postgresql_db:
name: netbox
- name: Create database user netbox
become_user: postgres
community.postgresql.postgresql_user:
login_db: <netbox db>
name: <db username>
password: <db password or variable>
expires: infinity
- name: Start systemctl postgresql
ansible.builtin.systemd_service:
name: <database service name>
state: started
- name: Change database netbox owner to netbox
become: true
become_user: <user>
community.postgresql.postgresql_db:
name: <name>
owner: <user>
- name: Grant CREATE on schema public to netbox (socket, peer)
become: true
become_user: postgres
community.postgresql.postgresql_privs:
database: <database>
type: schema
objs: public
roles: netbox
privs: CREATE
state: present
- name: Transfer database dump
ansible.builtin.copy:
src: /etc/ansible/playbooks/netbox-full.sql
dest: /tmp/netbox-full.sql
- name: Import dcim database
become: true
become_user: postgres
community.postgresql.postgresql_db:
name: <database>
state: restore
target: /tmp/netbox-full.sql
- name: restart postgresql
ansible.builtin.systemd_service:
name: <database service name>
state: restarted
- name: Copy old configuration.py to the new version
ansible.builtin.copy:
remote_src: true
src: "/opt/netbox-{{ oldver }}/netbox/netbox/configuration.py"
dest: /opt/netbox/netbox/netbox/
- name: Create local_requirement.txt
ansible.builtin.file:
path: /opt/netbox/local_requirements.txt
state: touch
- name: Add nextbox-ui-plugin to local_requirements.txt
ansible.builtin.lineinfile:
path: /opt/netbox/local_requirements.txt
regexp: "^nextbox-ui-plugin"
line: "nextbox-ui-plugin"
- name: Add netbox-bgp to local_requirements.txt
ansible.builtin.lineinfile:
path: /opt/netbox/local_requirements.txt
regexp: "^netbox-bgp"
line: "netbox-bgp>=0.18.0"
- name: Add nextbox-qrcode to local_requirements.txt
ansible.builtin.lineinfile:
path: /opt/netbox/local_requirements.txt
regexp: "^netbox-qrcode"
line: "netbox-qrcode"
- name: Add netbox-themes to local_requirements.txt
ansible.builtin.lineinfile:
path: /opt/netbox/local_requirements.txt
regexp: "^netbox-plugin-themes"
line: "netbox-plugin-themes"
- name: Add dulwich to local_requirements.txt
ansible.builtin.lineinfile:
path: /opt/netbox/local_requirements.txt
regexp: "^dulwich"
line: "dulwich"
- name: Add topology-views to local_requirements.txt
ansible.builtin.lineinfile:
path: /opt/netbox/local_requirements.txt
regexp: "^netbox-topology-views"
line: "netbox-topology-views"
- name: Transfer media folders
ansible.builtin.copy:
src: /etc/ansible/playbooks/netbox_media.tar
dest: /tmp/
- name: Replace media folder
ansible.builtin.unarchive:
src: /tmp/netbox_media.tar
dest: /opt/netbox/netbox/media/
remote_src: true
- name: Copy gunicorn file
ansible.builtin.copy:
remote_src: true
src: "/opt/netbox-{{ oldver }}/gunicorn.py"
dest: /opt/netbox
- name: Remove old netbox venv
ansible.builtin.file:
path: /opt/netbox/venv
state: absent
- name: Create new netbox venv with python3.12
ansible.builtin.command: python3.12 -m venv /opt/netbox/venv
- name: Install requirements in venv
ansible.builtin.shell: |
source /opt/netbox/venv/bin/activate
pip install --upgrade pip wheel
pip install -r /opt/netbox/requirements.txt
args:
executable: /bin/bash
- name: Run upgrade.sh script
ansible.builtin.command: /opt/netbox/upgrade.sh
args:
chdir: /opt/netbox
environment:
PYTHON: /usr/bin/python3.12
- name: Restart netbox
ansible.builtin.systemd_service:
name: "{{ item }}"
state: restarted
loop:
- netbox
- netbox-rq
- nginxCe playbook fonctionne dans mon environnement. Je ne garantie pas qu'il va fonctionner tel quel dans votre environnement. En gros les étapes sont:
- Transfert du tar de la nouvelle version sur le host à mettre à jour
- L'extraire sous /opt
- Faire un symlink de /opt/netbox vers /opt/netbox-nouvelle-version
- S'assurer que la b.d. netbox et le user dans la b.d. sont là avec les bonnes permissions
- Transférer le backup de votre b.d.
- Importer le backup
- Redémarrer postgresql
- Ramener configuration.py de votre ancienne config vers la nouvelle version
- Ajouter vos plugins dans local_requirement.txt
- Remplace media.tar par votre backup
- Ramène gunicorn.py
- Recrée un nouveau venv
- Installe les requirements et roule upgrade.sh
- S'il faut que ca plante, c'est là que ça va arriver.
- Redémarrer netbox, netbox-rq et nginx
Si t'es pas sûr, check la doc