infra/roles/pve/tasks/create_vm.yml

127 lines
3.8 KiB
YAML
Raw Normal View History

2018-10-02 09:12:23 +03:00
---
- set_fact: node_name="{{tmp_node}}"
- set_fact: vm_name="{{node_name}}.{{stack.name}}"
- set_fact: other_nics_to_vlan=false
2019-11-20 16:09:23 +03:00
- set_fact:
kvm_timeout: 500
2018-10-02 09:12:23 +03:00
- name: "[{{vm_name}}] clone VM"
proxmox_kvm:
node: pve
2018-10-02 09:12:23 +03:00
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
pool: "{{env_name}}"
clone: "{{stack.nodes[node_name].template}}"
full: no
2022-02-26 21:22:19 +03:00
timeout: 120
state: present
2018-10-02 09:12:23 +03:00
- name: "[{{vm_name}}] get VM state"
proxmox_kvm:
node: pve
2018-10-02 09:12:23 +03:00
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
state: current
register: vm_status
until: vm_status is succeeded
retries: 30
delay: 1
ignore_errors: yes
2022-02-26 21:22:19 +03:00
- name: "[{{vm_name}}] enable QEMU guest agent"
proxmox_kvm:
node: pve
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
agent: yes
update: yes
timeout: 120
retries: 30
delay: 1
ignore_errors: yes
- name: "[{{vm_name}}] get VM`s ip addresses"
proxmox_qemu_agent:
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
command: "network-get-interfaces"
2022-03-29 03:12:05 +03:00
register: vm_ip
until: vm_ip.results | json_query('[] | [?name!=`lo`]."ip-addresses" | [] | [?"ip-address-type"==`ipv4`] | []."ip-address"') | length > 0
retries: 3
delay: 1
ignore_errors: yes
2022-03-29 03:12:05 +03:00
when: vm_status.status == "running"
- set_fact: other_nics_to_vlan="{{env.pve.empty_vlan}}"
2022-03-29 03:12:05 +03:00
when: vm_status.status == "stopped" or vm_ip.failed
- debug: msg="{{hostvars['localhost']['other_nics_to_vlan']}}"
2018-10-02 09:12:23 +03:00
- name: "[{{vm_name}}] configure VM"
proxmox_kvm:
node: pve
2018-10-02 09:12:23 +03:00
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
agent: yes
cpu: host
kvm: yes
cores: "{{stack.nodes[node_name].cores}}"
memory: "{{stack.nodes[node_name].mem}}"
net: "{{stack.nodes[node_name].net | to_proxmox_net(other_nics_to_vlan)}}"
2018-10-02 09:12:23 +03:00
update: yes
2019-06-26 15:20:56 +03:00
ipconfig0: "ip={{stack.nodes[node_name].net.eth0.ipv4[0]}},gw={{stack.nodes[node_name].net.eth0.default}}"
2019-06-26 16:54:02 +03:00
nameserver: "{{stack.nodes[node_name].net.eth0.nameservers | default(omit) | first}}"
searchdomain: "{{stack.nodes[node_name].net.eth0.search | default(omit) | first}}"
2018-10-02 09:12:23 +03:00
register: vm_status
until: vm_status is succeeded
retries: 30
delay: 1
ignore_errors: yes
- name: "[{{vm_name}}] start VM"
proxmox_kvm:
node: pve
2018-10-02 09:12:23 +03:00
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
state: started
2019-11-20 16:09:23 +03:00
timeout: "{{ kvm_timeout }}"
2018-10-02 09:12:23 +03:00
- name: "[{{vm_name}}] wait for qemu-agent return addresses list"
proxmox_qemu_agent:
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
command: "network-get-interfaces"
register: res
until: res.results | json_query('[] | [?name!=`lo`]."ip-addresses" | [] | [?"ip-address-type"==`ipv4`] | []."ip-address"') != None
retries: 30
delay: 1
- name: "[{{vm_name}}] wait for any IP on any VM's interface"
proxmox_qemu_agent:
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
command: "network-get-interfaces"
register: res
until: res.results | json_query('[] | [?name!=`lo`]."ip-addresses" | [] | [?"ip-address-type"==`ipv4`] | []."ip-address"') | length > 0
2022-03-09 03:53:23 +03:00
retries: 90
2018-10-02 09:12:23 +03:00
delay: 1
- name: "[{{vm_name}}] register VM's interfaces info"
set_fact: stack="{{stack|dict_inject("/nodes/"+node_name+"/nics", res.results|to_nics_dict)}}"