summaryrefslogtreecommitdiff
path: root/roles/storage/zfs/base/tasks/main.yml
blob: d08548806d7489a1b01a4f38e824d8774e41d8c5 (plain) (blame)
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
---
- name: configure arc size
  when: zfs_arc_size is defined
  copy:
    content: |
      options zfs zfs_arc_min={{ zfs_arc_size.min | human_to_bytes }}
      options zfs zfs_arc_max={{ zfs_arc_size.max | human_to_bytes }}
    dest: /etc/modprobe.d/zfs.conf

- name: install zfs modules
  include_tasks: "{{ ansible_distribution }}.yml"

- name: load zfs kernel module
  modprobe:
    name: zfs
    state: present

- name: install zed and zfs file system utilities
  apt:
    name:
    - zfsutils-linux
    - zfs-zed
    state: present

- name: enable systemd mount-generator
  when: zfs_use_systemd_mount_generator
  import_tasks: enable-systemd-mount-generator.yml

- name: gather zpool facts
  zpool_facts:

- name: generate list of existing pools
  set_fact:
    zfs_existing_pools: "{{ ansible_zfs_pools | map(attribute='name') | list }}"

- name: try to import existing pools
  loop: "{{ zfs_pools | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  when: item.key not in zfs_existing_pools
  command: "zpool import{% for dev in (item.value.create_vdevs.split(' ') | select('match', '^/dev/') ) %} -d {{ dev }}{% endfor %} -f {{ item.key }}"
  register: zfs_import_result
  failed_when:
  - zfs_import_result.rc == 0
  - (zfs_import_result.stderr_lines | reject("regex", " no such pool available$") | list | length) > 0
  changed_when:
  - (zfs_import_result.stderr_lines | length) == 0

- name: gather zpool facts (again)
  zpool_facts:

- name: generate list of existing pools (again)
  set_fact:
    zfs_existing_pools: "{{ ansible_zfs_pools | map(attribute='name') | list }}"

- name: create pools
  loop: "{{ zfs_pools | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  when: item.key not in zfs_existing_pools
  command: "zpool create -m {{ item.value.mountpoint }} {% for prop, value in (item.value.properties | default(zfs_pool_default_properties)).items() %}-o {{ prop }}={{ value }} {% endfor %}{{ item.key }} {{ item.value.create_vdevs }}"

- name: create zfs volumes
  loop: "{{ zfs_volumes | zfs_volume_list }}"
  loop_control:
    label: "{{ item.pool }}/{{ item.name }}"
  vars:
    zfs_volume: "{{ item }}"
  include_role:
    name: storage/zfs/volume

- name: create zfs devices
  loop: "{{ zfs_devices }}"
  loop_control:
    label: "{{ item.pool }}/{{ item.name }}"
  vars:
    zfs_device: "{{ item }}"
  include_role:
    name: storage/zfs/device