258 lines
No EOL
6.4 KiB
YAML
258 lines
No EOL
6.4 KiB
YAML
---
|
|
- name: Install prerequisites for Docker
|
|
apt:
|
|
name:
|
|
- ca-certificates
|
|
- curl
|
|
- gnupg
|
|
- lsb-release
|
|
- git
|
|
- python3-full
|
|
state: present
|
|
update_cache: yes
|
|
when: ansible_os_family == "Debian"
|
|
|
|
- name: Add Docker's official GPG key
|
|
ansible.builtin.get_url:
|
|
url: https://download.docker.com/linux/debian/gpg
|
|
dest: /usr/share/keyrings/docker-archive-keyring.asc
|
|
mode: '0644'
|
|
|
|
- name: Add Docker repository
|
|
ansible.builtin.deb822_repository:
|
|
name: docker
|
|
types: deb
|
|
uris: https://download.docker.com/linux/debian
|
|
suites: "{{ ansible_distribution_release }}"
|
|
components: stable
|
|
architectures: "{{ ansible_architecture | replace('x86_64', 'amd64') }}"
|
|
signed_by: /usr/share/keyrings/docker-archive-keyring.asc
|
|
state: present
|
|
|
|
- name: Install Docker packages
|
|
apt:
|
|
name:
|
|
- docker-ce
|
|
- docker-ce-cli
|
|
- containerd.io
|
|
- docker-compose-plugin
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Ensure Docker service is started and enabled
|
|
systemd:
|
|
name: docker
|
|
state: started
|
|
enabled: yes
|
|
|
|
|
|
- name: Create netbox directories
|
|
file:
|
|
path: "{{ item }}"
|
|
state: directory
|
|
mode: '0755'
|
|
loop:
|
|
- "{{ netbox_home }}/netbox-docker"
|
|
- "{{ netbox_home }}/netbox-docker/env"
|
|
- "{{ netbox_home }}/data"
|
|
- "{{ netbox_home }}/data/redis"
|
|
- "{{ netbox_home }}/data/redis-cache"
|
|
- "{{ netbox_home }}/data/media"
|
|
- "{{ netbox_home }}/data/reports"
|
|
- "{{ netbox_home }}/data/scripts"
|
|
|
|
- name: Create postgres data directory with specific permissions
|
|
file:
|
|
path: "{{ netbox_home }}/data/postgres"
|
|
state: directory
|
|
mode: '0700'
|
|
|
|
- name: Clone netbox-docker repository
|
|
git:
|
|
repo: https://github.com/netbox-community/netbox-docker.git
|
|
dest: "{{ netbox_home }}/netbox-docker"
|
|
version: "{{ netbox_version }}"
|
|
force: yes
|
|
notify: restart netbox
|
|
|
|
# Configuration directory creation removed - using default configuration with env vars
|
|
|
|
- name: Deploy docker-compose override file
|
|
template:
|
|
src: docker-compose.override.yml.j2
|
|
dest: "{{ netbox_home }}/netbox-docker/docker-compose.override.yml"
|
|
mode: '0644'
|
|
notify: restart netbox
|
|
|
|
- name: Deploy netbox.env file
|
|
template:
|
|
src: env.j2
|
|
dest: "{{ netbox_home }}/netbox-docker/env/netbox.env"
|
|
mode: '0600'
|
|
notify: restart netbox
|
|
|
|
- name: Deploy postgres.env file
|
|
template:
|
|
src: postgres.env.j2
|
|
dest: "{{ netbox_home }}/netbox-docker/env/postgres.env"
|
|
mode: '0600'
|
|
notify: restart netbox
|
|
|
|
- name: Deploy redis.env file
|
|
template:
|
|
src: redis.env.j2
|
|
dest: "{{ netbox_home }}/netbox-docker/env/redis.env"
|
|
mode: '0600'
|
|
notify: restart netbox
|
|
|
|
- name: Deploy redis-cache.env file
|
|
template:
|
|
src: redis-cache.env.j2
|
|
dest: "{{ netbox_home }}/netbox-docker/env/redis-cache.env"
|
|
mode: '0600'
|
|
notify: restart netbox
|
|
|
|
- name: Remove custom configuration.py if exists
|
|
file:
|
|
path: "{{ netbox_home }}/netbox-docker/configuration/configuration.py"
|
|
state: absent
|
|
|
|
- name: Pull container images
|
|
command: docker compose -f docker-compose.yml -f docker-compose.override.yml pull
|
|
args:
|
|
chdir: "{{ netbox_home }}/netbox-docker"
|
|
register: pull_result
|
|
changed_when: "'Pulling' in pull_result.stdout"
|
|
|
|
|
|
- name: Deploy systemd service for NetBox
|
|
template:
|
|
src: netbox-docker.service.j2
|
|
dest: /etc/systemd/system/netbox-docker.service
|
|
owner: root
|
|
group: root
|
|
mode: '0644'
|
|
notify: restart netbox
|
|
|
|
- name: Reload systemd daemon
|
|
systemd:
|
|
daemon_reload: yes
|
|
|
|
- name: Start and enable NetBox service
|
|
systemd:
|
|
name: netbox-docker
|
|
state: started
|
|
enabled: yes
|
|
|
|
- name: Wait for initial container startup
|
|
pause:
|
|
seconds: 15
|
|
|
|
- name: Check container status
|
|
command: docker ps -a
|
|
register: container_status
|
|
changed_when: false
|
|
|
|
- name: Debug container status
|
|
debug:
|
|
var: container_status.stdout_lines
|
|
when: "'ANSIBLE_DEBUG' in ansible_env or ansible_verbosity > 0"
|
|
|
|
- name: Wait for NetBox to be ready
|
|
uri:
|
|
url: "http://localhost:{{ netbox_port }}/api/"
|
|
method: GET
|
|
status_code:
|
|
- 200
|
|
- 403 # API might return 403 if auth is required
|
|
validate_certs: no
|
|
timeout: 30
|
|
register: result
|
|
until: result.status in [200, 403]
|
|
retries: 30
|
|
delay: 10
|
|
|
|
- name: Check if superuser exists
|
|
command: >
|
|
docker exec netbox-docker-netbox-1
|
|
python /opt/netbox/netbox/manage.py shell -c
|
|
"from django.contrib.auth import get_user_model; User = get_user_model(); exit(0 if User.objects.filter(username='{{ netbox_admin_user }}').exists() else 1)"
|
|
register: superuser_check
|
|
failed_when: false
|
|
changed_when: false
|
|
|
|
- name: Create superuser
|
|
command: >
|
|
docker exec netbox-docker-netbox-1
|
|
python /opt/netbox/netbox/manage.py createsuperuser
|
|
--username {{ netbox_admin_user }}
|
|
--email {{ netbox_admin_email }}
|
|
--noinput
|
|
environment:
|
|
DJANGO_SUPERUSER_PASSWORD: "{{ netbox_admin_password }}"
|
|
when:
|
|
- netbox_create_superuser | bool
|
|
- superuser_check.rc != 0
|
|
|
|
# Caddy reverse proxy setup
|
|
- name: Install Caddy prerequisites
|
|
apt:
|
|
name:
|
|
- debian-keyring
|
|
- debian-archive-keyring
|
|
- apt-transport-https
|
|
- curl
|
|
state: present
|
|
update_cache: yes
|
|
|
|
- name: Add Caddy GPG key
|
|
shell: |
|
|
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
|
|
args:
|
|
creates: /usr/share/keyrings/caddy-stable-archive-keyring.gpg
|
|
|
|
- name: Add Caddy repository
|
|
shell: |
|
|
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | tee /etc/apt/sources.list.d/caddy-stable.list
|
|
args:
|
|
creates: /etc/apt/sources.list.d/caddy-stable.list
|
|
|
|
- name: Fix permissions on Caddy repository files
|
|
file:
|
|
path: "{{ item }}"
|
|
mode: '0644'
|
|
loop:
|
|
- /usr/share/keyrings/caddy-stable-archive-keyring.gpg
|
|
- /etc/apt/sources.list.d/caddy-stable.list
|
|
|
|
- name: Update apt cache after adding Caddy repo
|
|
apt:
|
|
update_cache: yes
|
|
|
|
- name: Install Caddy
|
|
apt:
|
|
name: caddy
|
|
state: present
|
|
|
|
- name: Create Caddy log directory
|
|
file:
|
|
path: /var/log/caddy
|
|
state: directory
|
|
owner: caddy
|
|
group: caddy
|
|
mode: '0755'
|
|
|
|
- name: Configure Caddy for NetBox
|
|
template:
|
|
src: Caddyfile.j2
|
|
dest: /etc/caddy/Caddyfile
|
|
owner: root
|
|
group: root
|
|
mode: '0644'
|
|
notify: restart caddy
|
|
|
|
- name: Ensure Caddy is started and enabled
|
|
systemd:
|
|
name: caddy
|
|
state: started
|
|
enabled: yes |