ansible/playbooks/include/vm-create.yml

126 lines
3.9 KiB
YAML

---
- name: Create new virtual instance
hosts: "{{myhosts}}"
gather_facts: false
vars:
vmhost_uri: "qemu+ssh://root@{{ vmhost }}/system"
root_pubkey: "{{ lookup('file', '/srv/ansible-private/ssh/id_rsa.pub') }}"
letters: "{{ 'bcdefghijklmnopqrstuvwxyz'|list }}"
console_log: "/var/log/libvirt/qemu/{{ inventory_hostname }}.console.log"
os_disk_image: "/srv/libvirt/ssd/{{ inventory_hostname }}.a.img"
disk_opts: bus=virtio,cache=none,device=disk,format=raw
virt_install_disks: >-
{% if datadisks is defined %}
{% for num in range(datadisks|count) %}
--disk /srv/libvirt/hdd/{{ inventory_hostname }}.{{ letters[num] }}.img,{{ disk_opts }},size={{ datadisks[num] }}
{% endfor %}
{% endif %}
virt_install_network: >-
{% for item in interfaces %}
{% if item[1] is defined %}
--network bridge=br{{ item[0] }},mac={{ item[1] }},model=virtio
{% else %}
--network bridge=br{{ item[0] }},model=virtio
{% endif %}
{% endfor %}
tasks:
- name: get vm list
virt:
uri: "{{ vmhost_uri }}"
command: list_vms
delegate_to: localhost
register: result
check_mode: false
- name: create temp directory
tempfile:
state: directory
register: tmpdir
delegate_to: localhost
when: inventory_hostname not in result.list_vms
- name: create inject file
copy:
content: |
rootpw --lock
%post
umask 077
mkdir -p /root/.ssh
echo '{{ root_pubkey }}' > /root/.ssh/authorized_keys
%end
dest: "{{ tmpdir.path }}/include.ks"
delegate_to: localhost
when: inventory_hostname not in result.list_vms
- name: run virt-install
command: >
virt-install --connect {{ vmhost_uri }} \
--name {{ inventory_hostname }} \
--graphics none --boot useserial=on --noautoconsole \
--serial pty,log.file={{ console_log }} \
--controller usb,model=none --sound none \
--vcpus "sockets=1,cores={{ num_cpus }},threads=1,placement=auto" \
--memory {{ mem_size }} --cpu host-passthrough \
--disk {{ os_disk_image }},{{ disk_opts }},size={{ dsk_size }} \
{{ virt_install_disks }} --initrd-inject {{ tmpdir.path }}/include.ks \
{{ virt_install_network }} \
{{ virt_install_os_args }}
delegate_to: localhost
when: inventory_hostname not in result.list_vms
- name: wait for install to finish
virt:
uri: "{{ vmhost_uri }}"
name: "{{ inventory_hostname }}"
command: status
register: vmstatus
until: vmstatus.status == "shutdown"
retries: 1000
delay: 20
delegate_to: localhost
when: inventory_hostname not in result.list_vms
- name: clean tempdir
file:
path: "{{ tmpdir.path }}"
state: absent
delegate_to: localhost
when: tmpdir.path is defined
- name: start vm
virt:
uri: "{{ vmhost_uri }}"
name: "{{ inventory_hostname }}"
command: start
delegate_to: localhost
when: inventory_hostname not in result.list_vms
- name: wait for ssh to start
wait_for:
delay: 10
host: "{{ inventory_hostname }}"
port: 22
state: started
timeout: 1200
delegate_to: localhost
when: inventory_hostname not in result.list_vms
- name: get ssh keys from new host
local_action: command ssh-keyscan {{ inventory_hostname }}
register: hostkeys
when: inventory_hostname not in result.list_vms
- name: add new ssh host key to known_hosts
known_hosts:
path: /root/.ssh/known_hosts
key: "{{ item }}"
host: "{{ inventory_hostname }}"
with_items: "{{ hostkeys.stdout.splitlines() }}"
delegate_to: localhost
when: inventory_hostname not in result.list_vms