infra/roles/pve/tasks/create_vm.yml
2022-03-29 04:12:05 +04:00

127 lines
3.8 KiB
YAML

---
- set_fact: node_name="{{tmp_node}}"
- set_fact: vm_name="{{node_name}}.{{stack.name}}"
- set_fact: other_nics_to_vlan=false
- set_fact:
kvm_timeout: 500
- name: "[{{vm_name}}] clone VM"
proxmox_kvm:
node: pve
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
timeout: 120
state: present
- name: "[{{vm_name}}] get VM state"
proxmox_kvm:
node: pve
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
- 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"
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
when: vm_status.status == "running"
- set_fact: other_nics_to_vlan="{{env.pve.empty_vlan}}"
when: vm_status.status == "stopped" or vm_ip.failed
- debug: msg="{{hostvars['localhost']['other_nics_to_vlan']}}"
- name: "[{{vm_name}}] configure VM"
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
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)}}"
update: yes
ipconfig0: "ip={{stack.nodes[node_name].net.eth0.ipv4[0]}},gw={{stack.nodes[node_name].net.eth0.default}}"
nameserver: "{{stack.nodes[node_name].net.eth0.nameservers | default(omit) | first}}"
searchdomain: "{{stack.nodes[node_name].net.eth0.search | default(omit) | first}}"
register: vm_status
until: vm_status is succeeded
retries: 30
delay: 1
ignore_errors: yes
- name: "[{{vm_name}}] start VM"
proxmox_kvm:
node: pve
api_user: "{{env.pve.username}}"
api_password: "{{env.pve.password}}"
api_host: "{{env.pve.api_url}}"
name: "{{vm_name}}"
state: started
timeout: "{{ kvm_timeout }}"
- 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
retries: 90
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)}}"