how do i create a zfs filesystem/zpool using ansible using zfs-linux
Asked Answered
S

2

5

I want the equivalent of the following to be generated using the zfs module in ansible, the following works using the command line, but fails on second run as the filesystem already exists.

{{ part_postgres }} is set to /dev/sdb in this instance.

zpool create -O compression=gzip postgres {{ part_postgres }} -O secondarycache=all

Currently in ansible I have:

- name: Create postgres zpool
    zfs: name=postgres{{ part_postgres }}
         compression=gzip
         state=present
         secondarycache=all
         mountpoint=/postgres
         atime=off
Suspend answered 12/11, 2013 at 19:43 Comment(2)
Looks like there is no support for zpool in ansible and a new module would need to be written for it. github.com/ansible/ansible/issues/4880Suspend
Came here looking for an ansible module capable of creating zpools. Turns out, in 2024 there is github.com/mrlesmithjr/ansible-zfs looking very promising, including iscsi support. Very handy. Yeah, late answer, sure enough. HTHHelbonnas
S
7

Ok - the zfs module won't do it, would need to write a new model for zpool. That said, its easy enough to check for zpool existing using the 'creates' annotation for the command module in ansible:

  - name: Create postgres zpool
    command: zpool create -O compression=gzip postgres /dev/sdb -o ashift=12 -O    secondarycache=all
             creates=/postgres

This will check if /postgres exists, and only run the command if it doesn't.

Suspend answered 14/11, 2013 at 10:21 Comment(0)
F
7

Here is another example:

- hosts: all
  vars:
    zfs_pool_name: data
    zfs_pool_mountpoint: /mnt/data
    zfs_pool_mode: mirror
    zfs_pool_devices:
      - sda
      - sdb
    zfs_pool_state: present
    zfs_pool_options:
      - "ashift=12"
  tasks:
    - name: check ZFS pool existance
      command: zpool list -Ho name {{ zfs_pool_name }}
      register: result_pool_list
      ignore_errors: yes
      changed_when: false

    - name: create ZFS pool
      command: >-
        zpool create
        {{ '-o' if zfs_pool_options else '' }} {{ zfs_pool_options | join(' -o ') }}
        {{ '-m ' + zfs_pool_mountpoint if zfs_pool_mountpoint else '' }}
        {{ zfs_pool_name }}
        {{ zfs_pool_mode if zfs_pool_mode else '' }}
        {{ zfs_pool_devices | join(' ') }}
      when:
        - zfs_pool_state | default('present') == 'present'
        - result_pool_list.rc == 1

    - name: destroy ZFS pool
      command: zpool destroy {{ zfs_pool_name }}
      when:
        - zfs_pool_state | default('present') == 'absent'
        - result_pool_list.rc == 0
Fraze answered 6/12, 2019 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.