Giter Site home page Giter Site logo

levshinovsky / ansible-role-virt_install_vm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stafwag/ansible-role-virt_install_vm

0.0 0.0 0.0 32 KB

An Ansible role to install a libvirt virtual machine with virt-install and cloud-init. It “designed” to be flexible.

License: MIT License

Jinja 100.00%

ansible-role-virt_install_vm's Introduction

Ansible Role: virt_install_vm

An Ansible role to install a libvirt virtual machine with virt-install and cloud-init. It is "designed" to be flexible.\

Example templates are provided to set up a Debian 11 and a Debian 12 systems. The cloud-init templates should work with other (GNU)/Linux systems that supports cloud-init.\

But there are (currently) only tested with Debian GNU/Linux.

Requirements

The role is a wrapper around the following roles:

Install the required roles with

$ ansible-galaxy install -r requirements.yml

this will install the latest default branch releases.

Or follow the installation instruction for each role on Ansible Galaxy.

https://galaxy.ansible.com/stafwag

Installation

Ansible galaxy

The role is available on Ansible Galaxy.

To install the role from Ansible Galaxy execute the command below.

$ ansible-galaxy install stafwag.virt_install_import

Source Code

If you want to use the source code directly.

Clone the role source code.

$ git clone https://github.com/stafwag/ansible-role-virt_install_vm

and put into the role search path

Supported GNU/Linux Distributions

It should work on most GNU/Linux distributions. cloud-localds is required. cloud-localds was available on Centos/RedHat 7 but not on Redhat 8. You'll need to install it manually to use the role on Centos/RedHat 8.

  • Archlinux
  • Debian
  • Centos 7
  • RedHat 7
  • Ubuntu

Role Variables and templates

Role Variables

See the documentation of the roles in the Requirements section.

  • virt_install_vm: "namespace"

    • skip_if_deployed: boolean default: false. \

      • When true:
        Skip role if the VM is already deployed. The role will exit successfully. \
      • When false:
        The role will exit with an error if the VM is already deployed.

Return values:

  • virt_install_vm_installed: Default: false. Set to true when the vm is installed by the role.

Templates

Templates files

  • templates/simple_debian: Example template to create a Debian virtual machine.
    This template use cloud_localds.cloudinfo to configure the cloud-init user-data.
    See the Usage section for an example.

Template variables

The template uses the following variables.

  • cloud_localds.cloud_config:
    • system_info:
      • default_user: The default user
        • name: Optional. Default: debian
        • passwd: Optional. Default: false. When set to false the user will be locked.
        • ssh_authorized_keys: An array with the authorized_key array.
    • disable_cloud_init: Optional. Default undefined. When set to true. cloud-init will be disabled on the VM.
    • network:
      • dns_nameservers: List with the nameservers.
      • dns_search: The dns search string.
      • interface: The default network interface to configure.
        • name: The interface name.
        • address: The ip address.
        • gateway: The gateway.
    • commands: Array with commands to execute on the VM.
    • poweroff: Poeroff the VM after the VM is installed.
    • reboot: Reboot the VM after the VM is installed.

Usage

Create a virtual machine template

This is a file with the role variables to set set up a virtual machine with all the common settings for the virtual machines. In this example vm.hostname and vm.ip_address can be configured for each virtual machine.

  • debian_vm_template.yml:
---
qemu_img:
  dest: "/var/lib/libvirt/images/{{ vm.hostname }}.qcow2"
  format: qcow2
  src: /Downloads/isos/debian/cloud/debian-10-generic-amd64.qcow2
  size: "50G"
  owner: root
  group: kvm
  mode: 660
cloud_localds:
  dest: "/var/lib/libvirt/images/{{ vm.hostname }}_cloudinit.iso"
  config_template: "templates/simple_debian/debian.j2"
  network_config_template: "templates/simple_debian/debian_netconfig.j2"
  cloud_config:
    system_info:
      default_user:
        name: ansible
        passwd: "{{ ansible_become_hash }}"
        ssh_authorized_keys:
          - "{{lookup('file', '~/.ssh/ansible_ssh_key.pub') }}"
    network:
      dns_nameservers:
        9.9.9.9
      dns_search:
        intern.local
      interface:
        name:
          enp1s0
        address:
          "{{ vm.ip_address }}"
        gateway:
          192.168.123.1
    disable_cloud_init: true
    reboot:
      true
virt_install_import:
  wait: 0
  name: "{{ vm.hostname }}"
  os_type: Linux
  os_variant: debian10
  network: network:default
  graphics: spice
  disks:
    - "/var/lib/libvirt/images/{{ vm.hostname }}.qcow2,device=disk"
    - "/var/lib/libvirt/images/{{ vm.hostname }}_cloudinit.iso,device=cdrom"

Playbook

Playbook to setup a virtual machine:

---
- name: Install tstdebian2
  hosts: kvmhost
  become: true
  vars:
    vm:
      hostname:
        tstdebian2
      ip_address:
        192.168.123.2/24
  pre_tasks:
    - name: Load the vm template
      include_vars: debian_vm_template.yml
    - name: display qemu_img
      debug:
        msg: 
          - "qemu_img: {{ qemu_img }}"
  roles:
    - stafwag.virt_install_vm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.