Ansible role to provision a zone on OpenIndiana
Olaf Bohlen
2020-08-25 a9c6b8a0d39219fe12790c3e33ca8646b865e676
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
---
# tasks file for oi-zone
- name: creating zone for you
  block:
    # jump into rescue if we want to uninstall
    - name: check for uninstall
      fail:
        msg: "uninstall is set to true, deleting resources"
      when:
        - oizone['uninstall'] is defined
        - oizone['uninstall']
        
    - name: create VNICs with VLAN
      dladm_vnic:
        name: "{{ item['logical'] }}"
        link: "{{ item['physical'] }}"
        vlan: "{{ item['vlan'] }}"
      loop: "{{ oizone['nics'] }}"
      when: item['vlan'] is defined
 
    - name: create VNICs without VLAN
      dladm_vnic:
        name: "{{ item['logical'] }}"
        link: "{{ item['physical'] }}"
      loop: "{{ oizone['nics'] }}"
      when: item['vlan'] is not defined
 
    - name: create filesystems
      zfs:
        name: "{{ item['path'] }}"
        state: present
        extra_zfs_properties: "{{ item['extra_zfs_properties'] }}"
      loop: "{{ oizone['filesystems'] }}"
      when: item['zfscreate']
 
    - name: is the zone already there?
      shell: zoneadm -z {{ oizone['name'] }} list
      register: zoneout
      changed_when: false
      ignore_errors: true
      
    - name: set up VM zones for master
      solaris_zone:
        name: "{{ oizone['name'] }}"
        state: installed
        path: "{{ oizone['zoneroot'] }}/{{ oizone['name'] }}"
        install_options: "-e pkg:/security/sudo -e runtime/python-27"
        config: >
          set brand={{ oizone['brand'] }};
          set autoboot={{ oizone['autoboot'] }};
          {% if oizone['bootargs'] is defined and oizone['bootargs'] | length %}
          set bootargs={{ oizone['bootargs'] }};
          {% endif %}
          set ip-type={{ oizone['iptype'] }};
          {% for nic in oizone['nics'] %}
          add net;
          set physical={{ nic['logical'] }};
          {% if oizone['iptype'] == "shared" %}
          set address={{ nic['address'] }};
          {% endif %}
          end;
          {% endfor %}
          {% if oizone['cpus'] is defined and oizone['cpus'] == "dedicated" %}
          add dedicated-cpu;
          set ncpus={{ oizone['ncpus'] }};
          end;
          {% endif %}
          {% if oizone['cpus'] is defined and oizone['cpus'] == "capped-cpu" %}
          add capped-cpu;
          set ncpus={{ oizone['ncpus'] }};
          end;
          {% endif %}
          {% if oizone['mem'] is defined and oizone['mem'] == "capped-memory" %}
          add capped-memory;
          set physical={{ oizone['ram'] }};
          set swap={{ oizone['swap'] }};
          set locked={{ oizone['locked'] }};
          end;
          {% endif %}
          {% for dataset in oizone['filesystems'] %}
          {% if dataset['type'] == "dataset" %}
          add dataset;
          set name={{ dataset['path'] }};
          end;
          {% endif %}
          {% if dataset['type'] == "lofs" %}
          add fs;
          set special={{ dataset['path'] }};
          set dir={{ dataset['mountpoint'] }};
          set type="lofs";
          {% for option in database['options'] %}
          add options {{ option }};
          {% endfor %}
          end;
          {% endif %}
          {% if dataset['type'] == "volume" %}
          add device;
          set match=/dev/zvol/rdsk/{{ dataset['path'] }};
          end;
          {% endif %}
          {% endfor %}
          {% if oizone['brand'] == "kvm" %}
          add attr;
          set name="bootorder";
          set type="string";
          set value="{{ oizone['kvm']['bootorder'] }}";
          add attr;
          set name="vnc";
          set type="string";
          set value="{{ oizone['kvm']['vnc'] }}";
          end;
          add attr;
          set name="vcpus";
          set type="string";
          set value="{{ oizone['ncpus'] }}";
          end;
          add attr;
          set name="ram";
          set type="string";
          set value="{{ oizone['ram'] }}";
          end;
          {% endif %}
      when: zoneout.rc == 1
      
    - name: create a sysding.conf
      template:
        dest: "{{ oizone['zoneroot'] }}/{{ oizone['name'] }}/root/etc/sysding.conf"
        src: sysding.j2
        mode: 0400
 
    - name: boot zone
      solaris_zone:
        name: "{{ oizone['name'] }}"
        state: running
        path: "{{ oizone['zoneroot'] }}/{{ oizone['name'] }}"
 
    - name: add zone to inventory
      local_action:
        module: lineinfile
        path: "{{ inventory_file }}"
        insertbefore: "BOF"
        line: "{{ oizone['name'] }}.{{ oizone['sysding']['ip']['dns']['domain'] }}"
      when:
        - oizone['updateinventory'] is defined
        - oizone['updateinventory']
      
  rescue:   # in case something wents wrong above, we do housekeeping here
    - name: ATTENTION
      debug:
        msg: "failed to install {{ oizone['name'] }}, rolling back"
    - name: delete zone
      solaris_zone:
        name: "{{ oizone['name'] }}"
        state: absent
        path: "{{ oizone['zoneroot'] }}/{{ oizone['name'] }}"
 
    - name: delete VNICs
      dladm_vnic:
        name: "{{ item['logical'] }}"
        link: "{{ item['physical'] }}"
        state: absent
      loop: "{{ oizone['nics'] }}"
        
    - name: delete filesystems
      zfs:
        name: "{{ item['path'] }}"
        state: absent
        extra_zfs_properties: "{{ item['extra_zfs_properties'] }}"
      loop: "{{ oizone['filesystems'] }}"
      when: item['zfscreate']