playbooks

ansible config management
git clone git://git.pyratebeard.net/playbooks.git
Log | Files | Refs | README

playbook.yml (5227B)


      1 ---
      2 #    ██                                     ████
      3 #   ░██                                    ░██░
      4 #  ██████  █████  ██████ ██████  ██████   ██████  ██████  ██████ ██████████
      5 # ░░░██░  ██░░░██░░██░░█░░██░░█ ░░░░░░██ ░░░██░  ██░░░░██░░██░░█░░██░░██░░██
      6 #   ░██  ░███████ ░██ ░  ░██ ░   ███████   ░██  ░██   ░██ ░██ ░  ░██ ░██ ░██
      7 #   ░██  ░██░░░░  ░██    ░██    ██░░░░██   ░██  ░██   ░██ ░██    ░██ ░██ ░██
      8 #   ░░██ ░░██████░███   ░███   ░░████████  ░██  ░░██████ ░███    ███ ░██ ░██
      9 #    ░░   ░░░░░░ ░░░    ░░░     ░░░░░░░░   ░░    ░░░░░░  ░░░    ░░░  ░░  ░░
     10 #
     11 #  author ▓▒ pyratebeard <root@pyratebeard.net>
     12 #    code ▓▒ https://git.pyratebeard.net/playbooks/
     13 
     14 - hosts: pigley
     15   gather_facts: true
     16   become: true
     17   pre_tasks:
     18     - name: "load vars"
     19       ansible.builtin.include_vars:
     20         file: vars.yml
     21       tags: always
     22 
     23   tasks:
     24     - name: "check for terraform"
     25       ansible.builtin.command: which terraform
     26       register: command
     27       check_mode: false
     28       failed_when: false
     29       tags: always
     30       delegate_to: localhost
     31 
     32 # ▓▒ start of block ▒▓
     33     - name: "prepare"
     34       block:
     35 
     36         - name: "prepare | install pre-reqs"
     37           ansible.builtin.pacman:
     38             name:
     39               #- tofu (aur package)
     40               - cdrtools
     41             state: present
     42 
     43         - name: "prepare | disable security for libvirt"
     44           ansible.builtin.lineinfile:
     45             path: "/etc/libvirt/qemu.conf"
     46             line: 'security_device = "none"'
     47             insertafter: '#security_device = "selinux"'
     48           notify: restart libvirtd
     49 
     50       when: command.rc != 0
     51       delegate_to: localhost
     52 # ▓▒ end of block ▒▓
     53 
     54     - name: "creation"
     55       block:
     56         - name: "creation | create vm"
     57           community.general.terraform:
     58             project_path: '{{ tofu_project }}'
     59             state: present
     60             complex_vars: true
     61             variables:
     62               vm_name: "{{ vm_name }}"
     63               vm_vcpus: "{{ vm_vcpus }}"
     64               vm_mem: "{{ vm_mem }}"
     65               vm_size: "{{ vm_size }}"
     66             force_init: true
     67           delegate_to: localhost
     68 
     69         - name: "creation | shutdown vm & dumpxml"
     70           ansible.builtin.shell: |
     71             virsh shutdown {{ vm_name }} && \
     72               virsh dumpxml {{ vm_name }} > /labfs/{{ vm_name }}.xml
     73 
     74         - name: "creation | create cluster resource"
     75           ansible.builtin.shell: |
     76             pcs resource create {{ vm_name }} VirtualDomain \
     77             config=/labfs/{{ vm_name }}.xml \
     78             migration_transport=ssh \
     79             meta \
     80             allow-migrate=true
     81 
     82         - name: "creation | rename tfstate file"
     83           ansible.builtin.copy:
     84             src: '{{ tofu_project }}/terraform.tfstate'
     85             dest: '{{ tofu_project }}/{{ vm_name }}.tfstate'
     86           delegate_to: localhost
     87 
     88       rescue:
     89         - name: "rescue | stop cluster resource"
     90           ansible.builtin.shell: |
     91             pcs resource disable {{ vm_name }} --wait
     92           failed_when: false
     93 
     94         - name: "rescue | remove cluster resource"
     95           ansible.builtin.shell: |
     96               pcs resource delete {{ vm_name }}
     97           failed_when: false
     98 
     99         - name: "rescue | destroy vm"
    100           community.general.terraform:
    101             project_path: '{{ tofu_project }}'
    102             state: absent
    103             state_file: '{{ tofu_project }}/{{ vm_name }}.tfstate'
    104           delegate_to: localhost
    105           failed_when: false
    106 
    107         - name: "removal | tidy up vm files"
    108           ansible.builtin.file:
    109             path: "/labfs/{{ vm_name }}.xml"
    110             state: absent
    111           failed_when: false
    112       tags:
    113         - create
    114 
    115     - name: "removal"
    116       block:
    117         - name: "removal | stop cluster resource"
    118           ansible.builtin.shell: |
    119             pcs resource disable {{ vm_name }} --wait
    120 
    121         - name: "removal | remove cluster resource"
    122           ansible.builtin.shell: |
    123               pcs resource delete {{ vm_name }}
    124 
    125         - name: "removal | destroy vm"
    126           community.general.terraform:
    127             project_path: '{{ tofu_project }}'
    128             state: absent
    129             state_file: '{{ tofu_project }}/{{ vm_name }}.tfstate'
    130           delegate_to: localhost
    131 
    132         - name: "removal | tidy up vm files"
    133           ansible.builtin.file:
    134             path: "/labfs/{{ vm_name }}.xml"
    135             state: absent
    136       tags:
    137         - never
    138         - remove
    139         - destroy
    140 
    141   handlers:
    142     - name: "restart libvirtd"
    143       ansible.builtin.service:
    144         name: libvirtd
    145         state: restarted