This avoids having to accept the key manually and allows playbooks making new instances to complete without human intervention. (If it works as desired)
65 lines
2.1 KiB
YAML
65 lines
2.1 KiB
YAML
#
|
|
# This task is the thing that creates a vm for later use
|
|
#
|
|
|
|
- name: get vm list
|
|
delegate_to: "{{ vmhost }}"
|
|
virt: command=list_vms
|
|
register: result
|
|
always_run: yes
|
|
|
|
- name: ensure the lv for the guest is made
|
|
lvol: lv={{ inventory_hostname }} vg={{ volgroup }} size={{ lvm_size }} state=present
|
|
delegate_to: "{{ vmhost }}"
|
|
when: inventory_hostname not in result.list_vms
|
|
|
|
- name: run the virt-install
|
|
shell: "{{ virt_install_command }}"
|
|
delegate_to: "{{ vmhost }}"
|
|
when: inventory_hostname not in result.list_vms
|
|
|
|
- name: wait for the install to finish
|
|
virt: command=status name={{ inventory_hostname }}
|
|
register: vmstatus
|
|
until: vmstatus.status == 'shutdown'
|
|
delegate_to: "{{ vmhost }}"
|
|
retries: 1500
|
|
delay: 10
|
|
when: inventory_hostname not in result.list_vms
|
|
|
|
- name: start the vm up
|
|
action: virt state=running name={{ inventory_hostname }}
|
|
delegate_to: "{{ vmhost }}"
|
|
when: inventory_hostname not in result.list_vms
|
|
|
|
- name: set it to autostart
|
|
action: virt command=autostart name={{ inventory_hostname }}
|
|
delegate_to: "{{ vmhost }}"
|
|
when: inventory_hostname not in result.list_vms
|
|
|
|
- name: make sure there is no old ssh host key for the host still around
|
|
local_action: known_hosts path={{item}} host={{ inventory_hostname }} state=absent
|
|
ignore_errors: True
|
|
with_items:
|
|
- /root/.ssh/known_hosts
|
|
- /etc/ssh/ssh_known_hosts
|
|
when: inventory_hostname not in result.list_vms
|
|
|
|
- name: wait for ssh on the vm to start back
|
|
local_action: wait_for delay=10 host={{ inventory_hostname }} port=22 state=started timeout=1200
|
|
when: inventory_hostname not in result.list_vms
|
|
|
|
- name: gather ssh host key from new instance
|
|
local_action: ssh-keyscan -t rsa {{ inventory_hostname }}
|
|
ignore_errors: True
|
|
register: hostkey
|
|
when: inventory_hostname not in result.list_vms
|
|
|
|
- name: add new ssh host key (you still need to add it to official ssh_host_keys later)
|
|
local_action: known_hosts path={{item}} key={{ hostkey.stdout }} host={{ inventory_hostname }} state=present
|
|
ignore_errors: True
|
|
with_items:
|
|
- /root/.ssh/known_hosts
|
|
- /etc/ssh/ssh_known_hosts
|
|
when: inventory_hostname not in result.list_vms
|
|
|