From 180bc353146bc59d28951a6b9ae0de0bb1ce3e37 Mon Sep 17 00:00:00 2001
From: Karel van Klink <karel.vanklink@geant.org>
Date: Mon, 30 Jan 2023 15:26:44 +0100
Subject: [PATCH] Rework pretty much the entire thing

Remove dev_images role as we'll pull these from a docker repository instead of building them each time
Strip ContainerLab role from any tasks that don't actually install ContainerLab
Add Docker role that installs docker onto a machine
Update inventory file to include a sample cl machine in the lab
Update playbook to only install Docker and ContainerLab by applying the appropriate roles
---
 deploy_containerlab.yaml                | 14 ----------
 inventory.yml                           |  5 ++--
 playbook.yml                            | 19 ++------------
 roles/containerlab/tasks/main.yml       | 16 ++----------
 roles/dev_images/tasks/build_image.yml  | 20 ---------------
 roles/dev_images/tasks/import_image.yml | 25 ------------------
 roles/dev_images/tasks/main.yml         | 23 -----------------
 roles/docker/tasks/main.yml             | 34 +++++++++++++++++++++++++
 8 files changed, 40 insertions(+), 116 deletions(-)
 delete mode 100644 deploy_containerlab.yaml
 delete mode 100644 roles/dev_images/tasks/build_image.yml
 delete mode 100644 roles/dev_images/tasks/import_image.yml
 delete mode 100644 roles/dev_images/tasks/main.yml
 create mode 100644 roles/docker/tasks/main.yml

diff --git a/deploy_containerlab.yaml b/deploy_containerlab.yaml
deleted file mode 100644
index 24a2984..0000000
--- a/deploy_containerlab.yaml
+++ /dev/null
@@ -1,14 +0,0 @@
-- name: Sanity check
-  hosts: dev_environments
-
-- name: System Setup
-  hosts: dev_environments
-  become: true
-  roles:
-    - kvm
-    - containerlab
-    - dev_images
-- name: System Setup
-  hosts: dev_environments
-  roles:
-    - dev_networks
diff --git a/inventory.yml b/inventory.yml
index 42b0550..a3a9fab 100644
--- a/inventory.yml
+++ b/inventory.yml
@@ -1,5 +1,4 @@
 dev_environments:
   hosts:
-    test02:
-      ansible_host: 172.16.100.9
-      
+    karel-cl-01:
+      ansible_host: karel-cl-01
diff --git a/playbook.yml b/playbook.yml
index bc48952..7fb2825 100644
--- a/playbook.yml
+++ b/playbook.yml
@@ -1,21 +1,6 @@
-- name: Sanity check
-  hosts: dev_environments
-  tasks:
-    - name: Ping them
-      ansible.builtin.ping:
-    - name: Show something
-      ansible.builtin.debug:
-        msg: blah blah
-
-- name: System Setup
+- name: Install Docker and ContainerLab
   hosts: dev_environments
   become: true
   roles:
-    - kvm
+    - docker
     - containerlab
-    - dev_images
-
-- name: System Setup
-  hosts: dev_environments
-  roles:
-    - dev_networks
diff --git a/roles/containerlab/tasks/main.yml b/roles/containerlab/tasks/main.yml
index 43ef0e4..6cab3f4 100644
--- a/roles/containerlab/tasks/main.yml
+++ b/roles/containerlab/tasks/main.yml
@@ -1,4 +1,4 @@
-- name: Containerlab Repository
+- name: ContainerLab Repository
   ansible.builtin.apt_repository:
     repo: deb [trusted=yes] https://apt.fury.io/netdevops/ /
     state: present
@@ -6,19 +6,7 @@
   notify:
     - Update apt cache
 
-- name: Install Packages
+- name: Install ContainerLab package
   ansible.builtin.apt:
     pkg:
-      - docker.io
       - containerlab
-      - python3-pip
-
-- name: Extra pip packages
-  ansible.builtin.pip:
-    name: boto3
-
-- name: Add relevant users to docker group
-  ansible.builtin.user:
-    name: "{{ nat_user }}"
-    groups: docker
-    append: true
diff --git a/roles/dev_images/tasks/build_image.yml b/roles/dev_images/tasks/build_image.yml
deleted file mode 100644
index 8a26440..0000000
--- a/roles/dev_images/tasks/build_image.yml
+++ /dev/null
@@ -1,20 +0,0 @@
-- name: Inspect a single image
-  community.docker.docker_image_info:
-    name: "{{ image.image_name }}:{{ image.image_tag }}"
-  register: image_exists
-
-- name: Build image if necessary
-  when: image_exists.images | length == 0
-  block:
-    - name: Get VM image
-      amazon.aws.aws_s3:
-        aws_access_key: "{{ s3.access_key }}"
-        aws_secret_key: "{{ s3.secret_key }}"
-        region: "{{ s3.region }}"
-        bucket: "{{ s3.bucket }}"
-        object: "{{ image.vm_image }}"
-        dest: "$HOME/vrnetlab/{{ image.integration }}/{{ image.vm_image }}"
-        mode: get
-    - name: Build/install the docker image
-      community.general.make:
-        chdir: "$HOME/vrnetlab/{{ image.integration }}"
diff --git a/roles/dev_images/tasks/import_image.yml b/roles/dev_images/tasks/import_image.yml
deleted file mode 100644
index 968648b..0000000
--- a/roles/dev_images/tasks/import_image.yml
+++ /dev/null
@@ -1,25 +0,0 @@
-- name: Inspect a single image
-  community.docker.docker_image_info:
-    name: "{{ image.image_name }}:{{ image.image_tag }}"
-  register: image_exists
-
-- name: Add docker image if necessary
-  when: image_exists.images | length == 0
-  block:
-    - name: Get VM images
-      amazon.aws.aws_s3:
-        aws_access_key: "{{ s3.access_key }}"
-        aws_secret_key: "{{ s3.secret_key }}"
-        region: "{{ s3.region }}"
-        bucket: "{{ s3.bucket }}"
-        object: "{{ image.filename }}"
-        dest: "/tmp/{{ image.filename }}"
-        mode: get
-    - name: Add docker images
-      # community.docker.docker_image doesn't work in this case
-      ansible.builtin.shell:
-        cmd: "docker image import /tmp/{{ image.filename }} {{ image.image_name }}:{{ image.image_tag }}"
-    - name: Remove downloaded file
-      ansible.builtin.file:
-        path: "/tmp/{{ image.filename }}"
-        state: absent
diff --git a/roles/dev_images/tasks/main.yml b/roles/dev_images/tasks/main.yml
deleted file mode 100644
index 5d1180e..0000000
--- a/roles/dev_images/tasks/main.yml
+++ /dev/null
@@ -1,23 +0,0 @@
-- name: Install Packages
-  ansible.builtin.apt:
-    pkg:
-      - git
-      - make
-
-- name: Clone vrnetlab
-  ansible.builtin.git:
-    repo: https://github.com/hellt/vrnetlab
-    version: v0.8.1
-    dest: $HOME/vrnetlab
-
-- name: Build vrnetlab images
-  ansible.builtin.include_tasks: build_image.yml
-  loop: "{{ vrnetlab_images }}"
-  loop_control:
-    loop_var: image
-
-- name: Import normal images
-  ansible.builtin.include_tasks: import_image.yml
-  loop: "{{ normal_images }}"
-  loop_control:
-    loop_var: image
diff --git a/roles/docker/tasks/main.yml b/roles/docker/tasks/main.yml
new file mode 100644
index 0000000..77e2cee
--- /dev/null
+++ b/roles/docker/tasks/main.yml
@@ -0,0 +1,34 @@
+- name: Install required system packages
+  ansible.builtin.apt:
+    pkg:
+      - apt-transport-https
+      - ca-certificates
+      - curl
+      - software-properties-common
+      - python3-pip
+      - virtualenv
+      - python3-setuptools
+    state: latest
+    update_cache: true
+
+- name: Add Docker GPG apt Key
+  ansible.builtin.apt_key:
+    url: https://download.docker.com/linux/ubuntu/gpg
+    state: present
+
+- name: Add Docker Repository
+  ansible.builtin.apt_repository:
+    repo: deb https://download.docker.com/linux/ubuntu focal stable
+    state: present
+
+- name: Update apt and install docker-ce
+  ansible.builtin.apt:
+    name: docker-ce
+    state: latest
+    update_cache: true
+
+- name: Add relevant user to docker group
+  ansible.builtin.user:
+    name: "{{ nat_user }}"
+    groups: docker
+    append: true
-- 
GitLab