From 828d143dd23f6978a5e9be14e2ee5a834cc5a247 Mon Sep 17 00:00:00 2001 From: Pavel Vyskocil Date: Sun, 29 Dec 2024 12:40:07 +0100 Subject: [PATCH] feat: prepare role ansible-role-exabgp-docker --- README.md | 57 ++++++++++++++++++++++- defaults/main.yml | 45 ++++++++++++++++++ handlers/main.yml | 36 ++++++++++++++ tasks/exabgp.yml | 62 +++++++++++++++++++++++++ tasks/exabgp_controls.yml | 21 +++++++++ tasks/main.yml | 7 +++ templates/default-docker-compose.yml.j2 | 18 +++++++ vars/main.yml | 2 + 8 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 tasks/exabgp.yml create mode 100644 tasks/exabgp_controls.yml create mode 100644 tasks/main.yml create mode 100644 templates/default-docker-compose.yml.j2 create mode 100644 vars/main.yml diff --git a/README.md b/README.md index 77b735d..fe872c7 100644 --- a/README.md +++ b/README.md @@ -1 +1,56 @@ -# ansible-role-exabgp-docker \ No newline at end of file +# exabgp_docker + +Role for setting up a docker running exabgp. +Configuration is done by the added files and templates(Not part of this role). + +## Requirements + +- Setting up network interfaces must be done before running this role. + +## Role Variables + +- exabgp_path - Base path where the files will be copied or created +- exabgp_dirs - List of directories which will be created (Path is relative to /) + - Example value: `- { path: "{{ exabgp_path }}" }` +- exabgp_files - List of files which will be copied to machine + - Example value: `- {src: "", dest: "", user: "", group: "", mode: ""}` where: + - src - required + - dest - optional; Default value: same as src; Relative to `exabgp_path` + - user - optional; Default value: root + - group - optional; Default value: root + - mode - optional; Default value: 0644 +- exabgp_templates - List of templates + - Example value: `- {src: "", dest: "", user: "", group: "", mode: ""}` where: + - src - required + - dest - optional; Default value: same as src; Relative to `exabgp_path` + - user - optional; Default value: root + - group - optional; Default value: root + - mode - optional; Default value: 0644 +- exabgp_docker_compose_template - Name of the used docker-compose template + - Relative to the folder with templates +- exabgp_custom_volumes - List of volumes connected to docker + - Only for default docker_compose_template + - Example value: `- "/etc/docker/data_volume:/data_volume"` +- exabgp_docker_image - Name of the docker image + - Only for default docker_compose_template + +## Dependencies + +-- + +## Example Playbook + + - hosts: all + roles: + - cesnet.exabgp_docker + +## Tags + +- `exabgp` - Runs all task to create directories, copy files, create files from templates and run docker with ExaBGP +- `exabgp_start` - Try to start docker with ExaBGP +- `exabgp_stop` - Try to stop docker with ExaBGP +- `exabgp_restart` - Try to restart docker with ExaBGP + +## Author Information + +Pavel Vyskocil . diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..74c63df --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,45 @@ +--- +# defaults file for cesnet.exabgp_docker + +# Base path where the files will be copied or created +exabgp_path: "/opt/exabgp" + +# List of directories which will be created (Path is relative to /) +exabgp_dirs: + - { path: "{{ exabgp_path }}" } + - { path: "{{ exabgp_path }}/etc" } + - { path: "{{ exabgp_path }}/etc/exabgp" } + +# List of files which will be copied to machine +# - Example value: - {src: "", dest: "", user: "", group: "", mode: ""} where: +# - src - required +# - dest - optional; Default value: same as src; Relative to exabgp_path +# - user - optional; Default value: root +# - group - optional; Default value: root +# - mode - optional; Default value: 0644 +exabgp_files: [] + +#- exabgp_templates - List of templates +# - Example value: - {src: "", dest: "", user: "", group: "", mode: ""} where: +# - src - required +# - dest - optional; Default value: same as src; Relative to exabgp_path +# - user - optional; Default value: root +# - group - optional; Default value: root +# - mode - optional; Default value: 0644 +exabgp_templates: [] + +# Name of the used docker-compose template +exabgp_docker_compose_template: "default-docker-compose.yml" + +# List of volumes connected to docker +# - Only for default docker_compose_template +# - Example value: `- "/etc/docker/data_volume:/data_volume"` +exabgp_custom_volumes: [] + +# Name of the docker image +# - Only for default docker_compose_template +exabgp_docker_image: "pierky/exabgp:4.2.7" + +# If TRUE, exabgp docker will be started/restarted +# If FALSE, exabgp docker will not be started/restarted +exabgp_force_start: false diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..f5aa67c --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,36 @@ +--- +# handlers file for cesnet.exabgp + +- name: "EXABGP - Restart" + community.docker.docker_compose_v2: + project_src: "{{ exabgp_path }}" + state: restarted + when: exabgp_force_start == True + listen: "docker exabgp restart" + +- name: "EXABGP - Force Restart" + community.docker.docker_compose_v2: + project_src: "{{ exabgp_path }}" + state: restarted + listen: "docker exabgp force restart" + +- name: "EXABGP - Stop" + community.docker.docker_compose_v2: + project_src: "{{ exabgp_path }}" + state: absent + listen: "docker exabgp stop" + +- name: "EXABGP - Start" + community.docker.docker_compose_v2: + project_src: "{{ exabgp_path }}" + state: present + pull: always + when: exabgp_force_start == True + listen: "docker exabgp start" + +- name: "EXABGP - Force Start" + community.docker.docker_compose_v2: + project_src: "{{ exabgp_path }}" + state: present + pull: always + listen: "docker exabgp force start" diff --git a/tasks/exabgp.yml b/tasks/exabgp.yml new file mode 100644 index 0000000..347dcd8 --- /dev/null +++ b/tasks/exabgp.yml @@ -0,0 +1,62 @@ +--- +- name: "EXABGP - Get interfaces" + shell: ifconfig -a + register: network_interfaces + +- name: "EXABGP - print" + debug: + msg: "{{ network_interfaces.stdout_lines }}" + +- name: "EXABGP - Confirm interfaces" + pause: + prompt: "Please check if network interfaces above are correct.\n\nPress any key to continue[Ctrl+C and then 'A' for abort]" + when: force == false + +- name: "EXABGP - Ensure that directory are created" + file: + path: "{{ item.path }}" + owner: "{{ item.owner | default('root') }}" + group: "{{ item.group | default('root') }}" + mode: "{{ item.mode | default('0755') }}" + state: directory + loop: "{{ exabgp_dirs }}" + +- name: "EXABGP - Copy files" + copy: + src: "{{ item.src }}" + dest: "{{ exabgp_path }}/{{ item.dest | default(item.src) }}" + owner: "{{ item.owner | default('root') }}" + group: "{{ item.group | default('root') }}" + mode: "{{ item.mode | default('0644') }}" + loop: "{{ exabgp_files }}" + notify: "docker exabgp restart" + +- name: "EXABGP - Create files from template" + template: + src: "{{ item.src }}.j2" + dest: "{{ exabgp_path }}/{{ item.dest | default(item.src) }}" + owner: "{{ item.owner | default('root') }}" + group: "{{ item.group | default('root') }}" + mode: "{{ item.mode | default('0644') }}" + loop: "{{ exabgp_templates }}" + notify: "docker exabgp restart" + +- name: "EXABGP - Create docker-compose.yml from template" + template: + src: "{{ exabgp_docker_compose_template }}.j2" + dest: "{{ exabgp_path }}/docker-compose.yml" + owner: root + group: root + mode: 0644 + tags: + - upgrade_docker_containers + - exabgp_upgrade_docker_container + notify: "docker exabgp restart" + +- name: "EXABGP - Ensure that docker image is started" + command: /bin/true + notify: "docker exabgp start" + when: exabgp_force_start == True + tags: + - upgrade_docker_containers + - exabgp_upgrade_docker_container diff --git a/tasks/exabgp_controls.yml b/tasks/exabgp_controls.yml new file mode 100644 index 0000000..31e8661 --- /dev/null +++ b/tasks/exabgp_controls.yml @@ -0,0 +1,21 @@ +--- +- name: "EXABGP - RESTART docker" + command: /bin/true + tags: + - "never" + - "exabgp_restart" + notify: "docker exabgp force restart" + +- name: "EXABGP - STOP docker" + command: /bin/true + tags: + - "never" + - "exabgp_stop" + notify: "docker exabgp stop" + +- name: "EXABGP - START docker" + command: /bin/true + tags: + - "never" + - "exabgp_start" + notify: "docker exabgp force start" diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..c9687e4 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,7 @@ +--- +# tasks file for cesnet.exabgp +- import_tasks: exabgp.yml + tags: + - exabgp + +- import_tasks: exabgp_controls.yml diff --git a/templates/default-docker-compose.yml.j2 b/templates/default-docker-compose.yml.j2 new file mode 100644 index 0000000..7fad836 --- /dev/null +++ b/templates/default-docker-compose.yml.j2 @@ -0,0 +1,18 @@ +{{ ansible_managed | comment}} + +services: + exabgp: + image: {{ exabgp_docker_image }} + container_name: exabgp + volumes: + - {{ exabgp_path }}/etc/exabgp:/etc/exabgp:rw + - /var/run/docker.sock:/var/run/docker.sock +{% for volume in exabgp_custom_volumes | default([]) %} + - {{ volume }} +{% endfor %} + network_mode: host + restart: always + logging: + driver: "syslog" + options: + tag: "proxyaai/exabgp" \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..da6919a --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for cesnet.exabgp