Initial version of playbook which creates virtual machines.
This commit is contained in:
commit
b010f9db84
4 changed files with 191 additions and 0 deletions
111
playbooks/include/vm-create.yml
Normal file
111
playbooks/include/vm-create.yml
Normal file
|
@ -0,0 +1,111 @@
|
|||
---
|
||||
- name: Create new virtual instance
|
||||
hosts: "{{myhosts}}"
|
||||
gather_facts: false
|
||||
|
||||
vars_files:
|
||||
- "../../vars/{{ os_type }}.yml"
|
||||
|
||||
vars:
|
||||
vmhost_uri: "qemu+ssh://root@{{ vmhost }}/system"
|
||||
root_pubkey: "{{ lookup('file', '/srv/ansible-private/ssh/id_rsa.pub') }}"
|
||||
|
||||
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: generate root password
|
||||
shell: "/srv/ansible/scripts/genpasswd {{ inventory_hostname }}"
|
||||
register: root_password
|
||||
delegate_to: localhost
|
||||
when: inventory_hostname not in result.list_vms
|
||||
|
||||
- name: create inject file
|
||||
copy:
|
||||
content: |
|
||||
rootpw --iscrypted {{ root_password.stdout }}
|
||||
%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
|
||||
shell: >
|
||||
virt-install --connect {{ vmhost_uri }} \
|
||||
--name {{ inventory_hostname }} \
|
||||
--graphics none --boot useserial=on --serial pty --noautoconsole \
|
||||
--controller usb,model=none --sound none --memory {{ mem_size }} \
|
||||
--vcpus {{ num_cpus }} --cpu host-passthrough \
|
||||
--disk /srv/libvirt/os/{{ inventory_hostname }}.a.img,cache=none,format=raw,size={{ dsk_size }} \
|
||||
--network bridge=br20,mac={{ mac_address }},model=virtio \
|
||||
--initrd-inject {{ tmpdir.path }}/include.ks \
|
||||
{{ 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
|
||||
|
||||
- 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
|
||||
|
||||
- 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
|
Loading…
Add table
Add a link
Reference in a new issue