4

我希望使用 ansible 中的 zfs 模块生成以下等效项,以下使用命令行工作,但由于文件系统已经存在而在第二次运行时失败。

{{ part_postgres }} 在此实例中设置为 /dev/sdb。

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

目前在ansible我有:

- name: Create postgres zpool
    zfs: name=postgres{{ part_postgres }}
         compression=gzip
         state=present
         secondarycache=all
         mountpoint=/postgres
         atime=off
4

2 回答 2

4

好的 - zfs 模块不会这样做,需要为 zpool 编写一个新模型。也就是说,使用 ansible 中命令模块的“creates”注释来检查 zpool 是否存在很容易:

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

这将检查 /postgres 是否存在,如果不存在则只运行该命令。

于 2013-11-14T10:21:26.687 回答
3

这是另一个例子:

- 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
于 2019-12-06T17:58:44.077 回答