Skip to content

Commit 2169624

Browse files
committed
feat: add zfs ansible scripts
1 parent 8bf3358 commit 2169624

File tree

4 files changed

+140
-3
lines changed

4 files changed

+140
-3
lines changed

ansible-inventory.example.ini

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
# if you wish, you can specify an alias for a node, or you can just specify
1919
# the ip address as shown below:
2020

21-
# you need to always define the ansible_ssh_host and ansible_ssh_private_key because they will be changed automatically
21+
# you need to always define the ansible_ssh_host and ansible_ssh_private_key_file because they will be changed automatically
2222
# you maybe to need to define the external interface (that will be given to the router, alongside the whole PCI device).
2323

2424

25-
#node1 ansible_ssh_host=10.0.0.2 ansible_ssh_private_key=/path/to/private_key
26-
#node1 ansible_ssh_host=10.0.0.2 ansible_ssh_private_key=/path/to/private_key external_interface=enp1f0
25+
#node1 ansible_ssh_host=10.0.0.2 ansible_ssh_private_key_file=/path/to/private_key
26+
#node1 ansible_ssh_host=10.0.0.2 ansible_ssh_private_key_file=/path/to/private_key external_interface=enp1f0
27+
28+
29+
#any node can be used to setup zfs pools
30+
#node2 ansible_ssh_host=192.168.121.184 ansible_user=ni ansible_ssh_private_key_file=node/bootstrap_key storage=true
2731

2832

2933
[workers]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
3+
- name: Update all nodes
4+
hosts: nodes
5+
become: true
6+
tasks:
7+
- name: Update nodes using DNF # noqa: package-latest
8+
ansible.builtin.dnf:
9+
name: "*"
10+
update_cache: true
11+
update_only: true
12+
state: latest
13+
14+
- name: Update all routers
15+
hosts: routers
16+
become: true
17+
tasks:
18+
- name: Update nodes using APT
19+
ansible.builtin.apt:
20+
name: "*"
21+
update_cache: true
22+
only_upgrade: true
23+
state: latest

node/add-zfs-to-storage-node.yaml

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
- name: Add zfs to storage nodes
3+
hosts: all
4+
become: true
5+
vars:
6+
zfs_pool_name: "niployments-hard-storage"
7+
zfs_pool_mountpoint: "/mnt/hard-storage"
8+
zfs_pool_state: present
9+
zfs_pool_mode: "raidz"
10+
11+
tasks:
12+
- name: Only run play if storage is not enabled
13+
when: hostvars[inventory_hostname]['storage'] is defined and hostvars[inventory_hostname]['storage'] == "true"
14+
block:
15+
- name: Get stoarge definitions
16+
ansible.builtin.include_vars:
17+
file: storage-defs.json
18+
name: storage_definitions
19+
- name: Use storage definition for node
20+
ansible.builtin.set_fact:
21+
zfs_pool_devices: "{{ storage_definitions[inventory_hostname] }}"
22+
- name: Enable EPEL repository
23+
ansible.builtin.yum:
24+
name: epel-release
25+
state: present
26+
- name: Add ZFS GPG key
27+
ansible.builtin.rpm_key:
28+
state: present
29+
key: https://raw.githubusercontent.com/zfsonlinux/zfsonlinux.github.com/master/zfs-release/RPM-GPG-KEY-openzfs-key2
30+
- name: Install DKMS manually
31+
ansible.builtin.yum:
32+
name: dkms
33+
state: present
34+
- name: Install zfs repo
35+
ansible.builtin.yum:
36+
name: "https://zfsonlinux.org/epel/zfs-release-2-3.el9.noarch.rpm"
37+
state: present
38+
- name: Install ZFS
39+
ansible.builtin.dnf:
40+
name:
41+
- zfs
42+
- zfs-dkms
43+
- kernel-devel
44+
state: present
45+
- name: Install dkms modules
46+
ansible.builtin.command: dkms autoinstall
47+
changed_when: true
48+
- name: Load ZFS kernel module
49+
ansible.builtin.command: modprobe zfs
50+
changed_when: true
51+
ignore_errors: yes
52+
- name: Check ZFS pool existance
53+
command: zpool list -Ho name {{ zfs_pool_name }}
54+
register: result_pool_list
55+
ignore_errors: yes
56+
changed_when: false
57+
58+
- name: Create ZFS pool
59+
command: >-
60+
zpool create
61+
{{ '-m ' + zfs_pool_mountpoint if zfs_pool_mountpoint else '' }}
62+
{{ zfs_pool_name }}
63+
{{ zfs_pool_mode if zfs_pool_mode else '' }}
64+
{{ zfs_pool_devices | join(' ') }}
65+
when:
66+
- zfs_pool_state | default('present') == 'present'
67+
- result_pool_list.rc == 1
68+
- name: Install NFS utilities
69+
dnf:
70+
name: nfs-utils
71+
state: present
72+
73+
- name: Force nfs version 4
74+
ansible.builtin.lineinfile:
75+
path: /etc/nfsmount.conf
76+
regexp: "^# Nfsvers=4"
77+
line: "Nfsvers=4"
78+
- name: Enable and start the NFS server service
79+
ansible.builtin.systemd:
80+
name: nfs-server
81+
enabled: true
82+
state: started
83+
84+
- name: Create export directory
85+
ansible.builtin.file:
86+
path: "{{ zfs_pool_mountpoint }}/shared_dir"
87+
state: directory
88+
mode: '0755'
89+
90+
- name: Set SELinux context for the export directory
91+
ansible.builtin.command: chcon -t nfs_t {{ zfs_pool_mountpoint }}/shared_dir
92+
args:
93+
creates: /export/shared_dir
94+
95+
- name: Configure NFS exports
96+
ansible.builtin.copy:
97+
dest: /etc/exports
98+
content: "{{ zfs_pool_mountpoint }}/shared_dir *(rw,sync,no_root_squash,no_subtree_check)\n"
99+
mode: 644
100+
101+
- name: Export NFS shares
102+
ansible.builtin.command: exportfs -r
103+
changed_when: true

node/vars/storage-defs.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"cluster2": [
3+
"/dev/disk/by-id/ata-ST2000DM008-2UB102_WFL7F5R8",
4+
"/dev/disk/by-id/ata-ST2000DM008-2UB102_WFL7F74S",
5+
"/dev/disk/by-id/ata-ST2000DM008-2UB102_WFL7HRE3"
6+
]
7+
}

0 commit comments

Comments
 (0)