548a49f4732d508cd3a4c92419a65536976b3998
[noc.git] / ansible / vm-update.yml
1 ---
2 - name: preperations and sanity checks
3   hosts: "{{ hostname }}"
4   gather_facts: no
5   tasks:
6   - name: setup variables
7     set_fact:
8       network_cooked: "{{ network }}"
9       install_cooked: "{{ install }}"
10
11   - name: create temporary host group for vm host
12     changed_when: no
13     add_host:
14       name: "{{ install_cooked.host }}"
15       inventory_dir: "{{inventory_dir}}"
16       group: _vmhost_
17   # TODO: add some sanity checks
18
19 - name: Update network configuration & shutdown VM
20   hosts: "{{ hostname }}"
21   gather_facts: no
22   roles:
23     - vm/network
24
25   tasks:
26     - meta: flush_handlers
27
28     - name: Shutdown VM
29       async: 1
30       poll: 0
31       ignore_errors: true
32       shell: sleep 2 && poweroff
33
34 - name: Update VM definition
35   hosts: _vmhost_
36   vars:
37     run_installer: no
38
39   tasks:
40     - name: Wait for VM to shut down
41       wait_for_virt:
42         name: "{{ hostname }}"
43         states: shutdown
44         timeout: 30
45  
46     - name: Undefine VM
47       virt:
48         name: "{{ hostname }}"
49         command: undefine
50
51     - name: Redefine VM
52       virt:
53         name: "{{ hostname }}"
54         command: define
55         xml: "{{ lookup('template', 'roles/vm/install/templates/libvirt-domain.xml.j2') }}"
56
57     - name: Restart VM
58       virt:
59         name: "{{ hostname }}"
60         state: running
61
62     - name: mark vm as autostarted
63       when: hostvars[hostname].install_cooked.autostart is defined
64       virt:
65         name: "{{ hostname }}"
66         autostart: "{{ hostvars[hostname].install_cooked.autostart }}"
67         command: info ## virt module needs either command or state
68
69 - name: Apply the base vm/guest role
70   hosts: "{{ hostname }}"
71   pre_tasks:
72     - name: Wait for the VM to be up and running
73       wait_for_connection:
74         timeout: 120
75
76   roles:
77     - vm/guest