871b3c780b98c1cc6c75c197034604ec9f90aa53
[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   post_tasks:
26     - name: Shutdown VM
27       async: 1
28       poll: 0
29       ignore_errors: true
30       shell: sleep 2 && poweroff
31
32 - name: Update VM definition
33   hosts: _vmhost_
34   tasks:
35     - name: Wait for VM to shut down
36       wait_for_virt:
37         name: "{{ hostname }}"
38         states: shutdown
39         timeout: 30
40  
41     - name: Undefine VM
42       virt:
43         name: "{{ hostname }}"
44         command: undefine
45
46     - name: Redefine VM
47       virt:
48         name: "{{ hostname }}"
49         command: define
50         xml: "{{ lookup('template', 'roles/vm/install/templates/libvirt-domain.xml.j2') }}"
51
52     - name: Restart VM
53       virt:
54         name: "{{ hostname }}"
55         state: running
56
57     - name: mark vm as autostarted
58       when: hostvars[hostname].install_cooked.autostart is defined
59       virt:
60         name: "{{ hostname }}"
61         autostart: "{{ hostvars[hostname].install_cooked.autostart }}"
62         command: info ## virt module needs either command or state
63
64 - name: Apply the base vm/guest role
65   hosts: "{{ hostname }}"
66   pre_tasks:
67     - name: Wait for the VM to be up and running
68       wait_for_connection:
69         timeout: 120
70
71   roles:
72     - vm/guest