Skip to content

Instantly share code, notes, and snippets.

@github-somerandomguy-xyz
Created April 14, 2020 07:42
Show Gist options
  • Save github-somerandomguy-xyz/d9c92df1f15c4d309345f3ad3242e055 to your computer and use it in GitHub Desktop.
Save github-somerandomguy-xyz/d9c92df1f15c4d309345f3ad3242e055 to your computer and use it in GitHub Desktop.
complete kvm role
---
- name: install kvm packages on Debian
package:
name:
- bridge-utils
- genisoimage
- libguestfs-tools
- libnss-libvirt
- libosinfo-bin
- libvirt-clients
- libvirt-daemon-system
- libvirt-dev
- python-lxml
- python3-libvirt
- qemu-kvm
- qemu-utils
- virt-top
- virtinst
state: latest
update_cache: yes
cache_valid_time: 5400
allow_unauthenticated: yes
become: true
tags:
- packages
- kvm
- name: Enable service libvirt, and start if not started
service:
name: libvirtd
enabled: yes
state: started
- name: adding existing user ansible to group libvirt/-qemu
user:
name: "{{ansible_user}}"
groups: libvirt,libvirt-qemu
# if append is not set to yes, the user is removed from all other groups and gets added only to this one!
append: yes
# Facts will be available as 'ansible_libvirt_pools'
- name: gather facts on existing virsh pool
virt_pool:
command: facts
uri: "{{ libvirt_uri }}"
- name: add a storage pool for storing libvirt images if Pool doesn't already exists
virt_pool:
command: define
# looks like setting name here is a redundant, the name is anyways taken from the template xml file, but should set it to make virt_pool module happy.
name: "{{ image_pool_name }}"
xml: '{{ lookup("template", "pool/dir.xml.j2") }}'
uri: "{{ libvirt_uri }}"
when: image_pool_name not in ansible_libvirt_pools
- name: create a image_pool_dir
file:
path: "{{ image_pool_dir }}"
owner: "{{ ansible_user }}"
group: libvirt
recurse: yes
mode: '0755'
become: true
# We should gather facts again so that the newly created pool is updated to 'ansible_libvirt_pools'
- name: gather facts on existing virsh pool
virt_pool:
command: facts
uri: "{{ libvirt_uri }}"
- block:
- name: build the storage pool.
virt_pool:
command: build
name: "{{ image_pool_name }}"
uri: "{{ libvirt_uri }}"
- name: start the storage pool.
virt_pool:
command: create
name: "{{ image_pool_name }}"
uri: "{{ libvirt_uri }}"
when: ansible_libvirt_pools["{{image_pool_name}}"].state != "active"
- name: autostart the storage pool
virt_pool:
autostart: yes
name: "{{ image_pool_name }}"
uri: "{{ libvirt_uri }}"
- name: ensure that the pool is active
virt_pool:
state: active
name: "{{ image_pool_name }}"
uri: "{{ libvirt_uri }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment