summaryrefslogtreecommitdiffstats
path: root/ansible/deployment_poc/tasks/configure_libvirt.yml
blob: b3e49b612e90670f3d7dbf9a484e1a25d88b19b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
---
- name: Provision VM
  block:
    - name: Create domain template
      ansible.builtin.template:
        src: "../templates/libvirt-template.xml.j2"
        dest: "../templates/libvirt-{{ inventory_hostname }}.xml"
        group: lysergic
        mode: '0660'

    - name: Create storage template
      ansible.builtin.template:
        src: "../templates/libvirt-storage-template.xml.j2"
        dest: "../templates/generated/libvirt-storage-{{ inventory_hostname }}.xml"
        group: lysergic
        mode: '0660'

    - name: Define domain        
      virt:        
        uri: "{{ libvirt_url }}"        
        command: define        
        xml: "{{ lookup('template', '../templates/libvirt-template.xml.j2') }}"        
        autostart: no        
    #  delegate_to: localhost        
    
    - name: Query volumes
      ansible.builtin.command:
        argv:
          - /usr/bin/virsh
          - -c
          - "{{ libvirt_url }}"
          - vol-list
          - "{{ storage.name }}"
      register: volumes
      no_log: true

    - name: Define volume  
      ansible.builtin.command:
        argv:        
          - /usr/bin/virsh        
          - -c        
          - "{{ libvirt_url }}"        
          - vol-create        
          - "{{ storage.name }}"        
          - "../templates/generated/libvirt-storage-{{ inventory_hostname }}.xml"
      when: vm_name not in volumes.stdout

    - name: Fetch MAC address
      ansible.builtin.shell: "/usr/bin/virsh -c {{ libvirt_url }} domiflist {{ vm_name }} | awk '{print $5}' | cut -d/ -f 1 | tail -n 2 | head -n1" # ewww :-(
      register: domiflist_mac

    - name: Store MAC address
      set_fact:
        mac_address: "{{ domiflist_mac.stdout }}"

  delegate_to: localhost

  always:
      - name: Debug
        ansible.builtin.debug:
          msg: "{{ libvirt_url if libvirt_url is defined }} - {{ storage.name if storage is defined }} - {{ mac_address if mac_address is defined }}"