|
| 1 | +--- |
| 2 | +- hosts: localhost |
| 3 | + gather_facts: false |
| 4 | + |
| 5 | + pre_tasks: |
| 6 | + - name: Set the required variables |
| 7 | + set_fact: |
| 8 | + ingest_token: "{{ lookup('env','ACCESS_TOKEN') }}" |
| 9 | + rum_token: "{{ lookup('env','RUM_TOKEN') }}" |
| 10 | + realm: "{{ lookup('env','REALM') }}" |
| 11 | + instance: "{{ lookup('env','INSTANCE') }}" |
| 12 | + hec_url: "{{ lookup('env','HEC_URL') }}" |
| 13 | + hec_token: "{{ lookup('env','HEC_TOKEN') }}" |
| 14 | + |
| 15 | + tasks: |
| 16 | + - name: Check for existing K3s installation |
| 17 | + block: |
| 18 | + - name: Check if K3s is installed |
| 19 | + stat: |
| 20 | + path: /usr/local/bin/k3s |
| 21 | + register: k3s_binary |
| 22 | + |
| 23 | + - name: Check if K3s service is running |
| 24 | + systemd: |
| 25 | + name: k3s |
| 26 | + register: k3s_service |
| 27 | + failed_when: false |
| 28 | + |
| 29 | + - name: Uninstall K3s if present |
| 30 | + block: |
| 31 | + - name: Stop K3s service |
| 32 | + systemd: |
| 33 | + name: k3s |
| 34 | + state: stopped |
| 35 | + become: true |
| 36 | + when: k3s_service.status is defined and k3s_service.status.ActiveState == "active" |
| 37 | + |
| 38 | + - name: Run K3s uninstall script |
| 39 | + command: /usr/local/bin/k3s-uninstall.sh |
| 40 | + become: true |
| 41 | + when: k3s_binary.stat.exists |
| 42 | + |
| 43 | + - name: Remove K3s binary if still present |
| 44 | + file: |
| 45 | + path: /usr/local/bin/k3s |
| 46 | + state: absent |
| 47 | + become: true |
| 48 | + when: k3s_binary.stat.exists |
| 49 | + |
| 50 | + when: k3s_binary.stat.exists |
| 51 | + |
| 52 | + - name: Install and configure k3d |
| 53 | + block: |
| 54 | + - name: Add splunk user to docker group |
| 55 | + user: |
| 56 | + name: splunk |
| 57 | + groups: docker |
| 58 | + append: yes |
| 59 | + become: true |
| 60 | + |
| 61 | + - name: Reset SSH connection to pick up group changes |
| 62 | + meta: reset_connection |
| 63 | + |
| 64 | + - name: Get latest kubectl version |
| 65 | + uri: |
| 66 | + url: https://dl.k8s.io/release/stable.txt |
| 67 | + return_content: yes |
| 68 | + register: kubectl_version |
| 69 | + |
| 70 | + - name: Download kubectl binary |
| 71 | + get_url: |
| 72 | + url: "https://dl.k8s.io/release/{{ kubectl_version.content | trim }}/bin/linux/amd64/kubectl" |
| 73 | + dest: /usr/local/bin/kubectl |
| 74 | + mode: '0755' |
| 75 | + become: true |
| 76 | + |
| 77 | + - name: Download k3d install script |
| 78 | + get_url: |
| 79 | + url: https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh |
| 80 | + dest: /tmp/k3d-install.sh |
| 81 | + mode: '0755' |
| 82 | + become: true |
| 83 | + |
| 84 | + - name: Install k3d |
| 85 | + command: /tmp/k3d-install.sh |
| 86 | + become: true |
| 87 | + |
| 88 | + - name: Create 3-node k3d cluster |
| 89 | + command: k3d cluster create {{ instance }}-cluster --agents 2 |
| 90 | + become_user: splunk |
| 91 | + become: true |
| 92 | + |
| 93 | + - name: Create .kube directory for splunk user |
| 94 | + file: |
| 95 | + path: /home/splunk/.kube |
| 96 | + state: directory |
| 97 | + owner: splunk |
| 98 | + group: splunk |
| 99 | + mode: '0755' |
| 100 | + become: true |
| 101 | + |
| 102 | + - name: Generate kubeconfig for splunk user |
| 103 | + shell: k3d kubeconfig get {{ instance }}-cluster > /home/splunk/.kube/config |
| 104 | + become_user: splunk |
| 105 | + become: true |
| 106 | + |
| 107 | + - name: Set kubeconfig file permissions |
| 108 | + file: |
| 109 | + path: /home/splunk/.kube/config |
| 110 | + owner: splunk |
| 111 | + group: splunk |
| 112 | + mode: '0600' |
| 113 | + become: true |
| 114 | + |
| 115 | + - name: Install Chaos Mesh |
| 116 | + shell: curl -sSL https://mirrors.chaos-mesh.org/v2.7.2/install.sh | bash -s -- --k3s |
| 117 | + become_user: splunk |
| 118 | + become: true |
| 119 | + environment: |
| 120 | + KUBECONFIG: /home/splunk/.kube/config |
| 121 | + |
| 122 | + - name: Configure Demo-in-a-Box |
| 123 | + block: |
| 124 | + - name: Check to see if the config has run |
| 125 | + stat: |
| 126 | + path: /white_rabbit.followed |
| 127 | + register: wh_result |
| 128 | + |
| 129 | + - name: Extract demo-in-a-box.zip into /home/splunk/ |
| 130 | + unarchive: |
| 131 | + src: demo-in-a-box.zip |
| 132 | + dest: /home/splunk/ |
| 133 | + owner: splunk |
| 134 | + group: splunk |
| 135 | + remote_src: yes |
| 136 | + list_files: yes |
| 137 | + become: true |
| 138 | + register: diab_dir_name |
| 139 | + when: not wh_result.stat.exists |
| 140 | + |
| 141 | + #- name: Delete demo-in-a-box.zip |
| 142 | + # file: |
| 143 | + # path: /home/splunk/demo-in-a-box.zip |
| 144 | + # state: absent |
| 145 | + # become: true |
| 146 | + # when: not wh_result.stat.exists |
| 147 | + |
| 148 | + - debug: |
| 149 | + var: diab_dir_name.files[0] |
| 150 | + when: not wh_result.stat.exists |
| 151 | + |
| 152 | + - name: Create K8s secrets YAML |
| 153 | + file: |
| 154 | + path: /home/splunk/workshop-secrets.yaml |
| 155 | + owner: splunk |
| 156 | + group: splunk |
| 157 | + state: touch |
| 158 | + become: true |
| 159 | + when: not wh_result.stat.exists |
| 160 | + |
| 161 | + - name: Update secrets YAML |
| 162 | + ansible.builtin.blockinfile: |
| 163 | + path: /home/splunk/workshop-secrets.yaml |
| 164 | + block: | |
| 165 | + apiVersion: v1 |
| 166 | + kind: Secret |
| 167 | + metadata: |
| 168 | + name: workshop-secret |
| 169 | + namespace: default |
| 170 | + type: Opaque |
| 171 | + stringData: |
| 172 | + app: {{ instance }}-store |
| 173 | + env: {{ instance}} |
| 174 | + deployment: "deployment.environment={{ instance }}" |
| 175 | + access_token: {{ ingest_token }} |
| 176 | + realm: {{ realm }} |
| 177 | + rum_token: {{ rum_token }} |
| 178 | + hec_token: {{ hec_token }} |
| 179 | + hec_url: {{ hec_url }} |
| 180 | + url: "http://frontend-external:81" |
| 181 | + marker: "## {mark} Added by ansible (configuration Demo-in-a-Box)" |
| 182 | + become: true |
| 183 | + when: not wh_result.stat.exists |
| 184 | + |
| 185 | + - name: Demo-in-a-Box Kubernetes setup |
| 186 | + command: kubectl apply -f /home/splunk/workshop-secrets.yaml |
| 187 | + when: not wh_result.stat.exists |
| 188 | + |
| 189 | + - name: Create service for Demo-in-a-Box Manager API |
| 190 | + file: |
| 191 | + path: /etc/systemd/system/diab-manager-api.service |
| 192 | + state: touch |
| 193 | + become: true |
| 194 | + when: not wh_result.stat.exists |
| 195 | + |
| 196 | + - name: Update service for diab-manager-api.service |
| 197 | + ansible.builtin.blockinfile: |
| 198 | + path: /etc/systemd/system/diab-manager-api.service |
| 199 | + block: | |
| 200 | + [Unit] |
| 201 | + Description=Demo-in-a-Box Manager API Service |
| 202 | + After=network.target |
| 203 | + StartLimitIntervalSec=0 |
| 204 | + [Service] |
| 205 | + Type=simple |
| 206 | + Restart=always |
| 207 | + RestartSec=1 |
| 208 | + User=splunk |
| 209 | + Environment="KUBECONFIG=/home/splunk/.kube/config" |
| 210 | + WorkingDirectory=/home/splunk/{{ diab_dir_name.files[0] }}v3 |
| 211 | + ExecStart=/bin/bash scripts/run-manager-api.sh |
| 212 | + [Install] |
| 213 | + WantedBy=multi-user.target |
| 214 | + marker: "## {mark} Added by ansible (configuration Demo-in-a-Box)" |
| 215 | + become: true |
| 216 | + when: not wh_result.stat.exists |
| 217 | + |
| 218 | + - name: Create service for Demo-in-a-Box Manager UI |
| 219 | + file: |
| 220 | + path: /etc/systemd/system/diab-manager-ui.service |
| 221 | + state: touch |
| 222 | + become: true |
| 223 | + when: not wh_result.stat.exists |
| 224 | + |
| 225 | + - name: Update service for diab-manager-ui.service |
| 226 | + ansible.builtin.blockinfile: |
| 227 | + path: /etc/systemd/system/diab-manager-ui.service |
| 228 | + block: | |
| 229 | + [Unit] |
| 230 | + Description=Demo-in-a-Box Manager UI Service |
| 231 | + After=network.target |
| 232 | + StartLimitIntervalSec=0 |
| 233 | + [Service] |
| 234 | + Type=simple |
| 235 | + Restart=always |
| 236 | + RestartSec=1 |
| 237 | + User=splunk |
| 238 | + Environment="KUBECONFIG=/home/splunk/.kube/config" |
| 239 | + WorkingDirectory=/home/splunk/{{ diab_dir_name.files[0] }}v3 |
| 240 | + ExecStart=/bin/bash scripts/run-manager-ui.sh |
| 241 | + [Install] |
| 242 | + WantedBy=multi-user.target |
| 243 | + marker: "## {mark} Added by ansible (configuration Demo-in-a-Box)" |
| 244 | + become: true |
| 245 | + when: not wh_result.stat.exists |
| 246 | + |
| 247 | + - name: Helm OTel Collector repository add |
| 248 | + command: helm repo add splunk-otel-collector-chart https://signalfx.github.io/splunk-otel-collector-chart |
| 249 | + when: not wh_result.stat.exists |
| 250 | + |
| 251 | + - name: Helm repository update |
| 252 | + command: helm repo update |
| 253 | + when: not wh_result.stat.exists |
| 254 | + |
| 255 | + - name: Start the diab-manager-api service |
| 256 | + command: systemctl enable --now diab-manager-api.service |
| 257 | + become: true |
| 258 | + when: not wh_result.stat.exists |
| 259 | + |
| 260 | + - name: Wait for diab-manager-api.service to start |
| 261 | + pause: |
| 262 | + seconds: 20 |
| 263 | + when: not wh_result.stat.exists |
| 264 | + |
| 265 | + - name: Start the diab-manager service |
| 266 | + command: systemctl enable --now diab-manager-ui.service |
| 267 | + become: true |
| 268 | + when: not wh_result.stat.exists |
| 269 | + |
| 270 | + - name: Wait for diab-manager-ui.service to start |
| 271 | + pause: |
| 272 | + seconds: 5 |
| 273 | + when: not wh_result.stat.exists |
| 274 | + |
| 275 | + - name: Enable Chaos Mesh dashboard |
| 276 | + command: kubectl apply -f /home/splunk/{{ diab_dir_name.files[0] }}v3/chaos-mesh/chaos-mesh-dashboard-lb.yaml |
| 277 | + when: not wh_result.stat.exists |
| 278 | + |
| 279 | + - name: Save Demo-in-a-Box configuration |
| 280 | + uri: |
| 281 | + url: "http://localhost:8082/saveConfig" |
| 282 | + method: POST |
| 283 | + body: "realm={{ realm }}&ingest_token={{ ingest_token }}&rum_token={{ rum_token }}&hec_url={{ hec_url}}&hec_token={{ hec_token }}&splunk_index=splunk4rookies-workshop&instance={{ instance }}" |
| 284 | + status_code: [ 200, 201 ] |
| 285 | + timeout: 30 |
| 286 | + register: splunk_api |
| 287 | + until: splunk_api.status == 200 |
| 288 | + retries: 10 |
| 289 | + delay: 5 |
| 290 | + when: not wh_result.stat.exists |
| 291 | + |
| 292 | + - name: Create a file to signify that the config has run successfully |
| 293 | + file: |
| 294 | + path: "/white_rabbit.followed" |
| 295 | + state: touch |
| 296 | + become: true |
| 297 | + when: not wh_result.stat.exists |
0 commit comments