Skip to content

Instantly share code, notes, and snippets.

@platu
Last active March 24, 2024 14:29
Show Gist options
  • Save platu/5d8898ff4e0b63c834d6b41695318415 to your computer and use it in GitHub Desktop.
Save platu/5d8898ff4e0b63c834d6b41695318415 to your computer and use it in GitHub Desktop.
IaC Lab 3 Ansible copy and run virtual routers stage playbook
---
- name: COPY MASTER IMAGE AND RUN ROUTERS
hosts: hypervisors
tasks:
- name: COPY MASTER IMAGE TO LAB DIRECTORY
ansible.builtin.copy:
src: "{{ item }}"
dest: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/"
mode: "0644"
remote_src: true
follow: true
with_items:
- "{{ ansible_env.HOME }}/masters/{{ image_name }}.qcow2"
- "{{ ansible_env.HOME }}/masters/{{ image_name }}.qcow2_OVMF_VARS.fd"
- name: CHECK IF ROUTER IMAGE FILE EXISTS
ansible.builtin.stat:
path: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/{{ item }}.qcow2"
register: image_file
with_inventory_hostnames:
- routers
- name: COPY QCOW2 MASTER IMAGE FILE TO ROUTER IMAGE FILE
ansible.builtin.copy:
src: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/{{ image_name }}.qcow2"
dest: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/{{ item }}.qcow2"
mode: "0644"
remote_src: true
when: not (image_file.results | map(attribute='stat.exists')) is all
with_inventory_hostnames:
- routers
- name: CHECK IF ROUTER OVMF VARS FILE EXISTS
ansible.builtin.stat:
path: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/{{ item }}.qcow2_OVMF_VARS.fd"
register: ovmf_vars_file
with_inventory_hostnames:
- routers
- name: COPY MASTER OVMF VARS FILE TO ROUTER OVMF VARS FILE
ansible.builtin.copy:
src: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/{{ image_name }}.qcow2_OVMF_VARS.fd"
dest: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/{{ item }}.qcow2_OVMF_VARS.fd"
mode: "0644"
remote_src: true
when: not (ovmf_vars_file.results | map(attribute='stat.exists')) is all
with_inventory_hostnames:
- routers
- name: LAUNCH ROUTERS
ansible.builtin.shell:
cmd: |
set -o pipefail
touch launch_output.txt
$HOME/vm/scripts/ovs-iosxe.sh \
"{{ item }}.qcow2" \
"{{ hostvars[item].patches.mgmt | regex_search('[0-9]+') }}" \
"{{ hostvars[item].patches.g2 | regex_search('[0-9]+') }}" \
"{{ hostvars[item].patches.g3 | regex_search('[0-9]+') }}" | \
tee -a launch_output.txt > "{{ item }}_launched"
executable: /bin/bash
args:
chdir: "{{ ansible_env.HOME }}/vm/{{ lab_name }}"
creates: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/{{ item }}_launched"
with_inventory_hostnames:
- routers
- name: FORCE LAB INVENTORY REBUILD
delegate_to: localhost
ansible.builtin.file:
path: "{{ item }}"
state: absent
with_items:
- trace/launch_output.txt
- inventory/lab.yml
- name: FETCH LAUNCH OUTPUT MESSAGES
ansible.builtin.fetch:
src: "{{ ansible_env.HOME }}/vm/{{ lab_name }}/launch_output.txt"
dest: trace/launch_output.txt
mode: "0644"
flat: true
- name: BUILD LAB INVENTORY
delegate_to: localhost
ansible.builtin.command:
cmd: /usr/bin/env python3 ./build_lab_inventory.py
register: command_result
failed_when: "'launch trace file does not exist' in command_result.stderr"
args:
creates: inventory/lab.yml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment