Ansibleでファイルシステム作成・拡張メモ



```/etc/ansible/002_filesystem.yml
---
- hosts: target
  remote_user: root
  become: no
  vars:
    device_list:
    - device: "xvdf"
      num: "1"
      pvname: "xvdf1"
      vgname: "basevg"
      lvname: "maillv"
      lvsize: "1g"
      fstype: "xfs"
      mount_point: "/maildir"
      label: "maildir"
    - device: "xvdf"
      num: "1"
      pvname: "xvdf1"
      vgname: "basevg"
      lvname: "kdumplv"
      lvsize: "3g"
      fstype: "xfs"
      mount_point: "/var/crash"
      label: "varcrash"
  roles:
  - role: 002_filesystem/001_filesystem
    tags: filesystem
```

```/etc/ansible/roles/002_filesystem/001_filesystem/tasks/main.yml
---
- debug:
    var: device_list
    verbosity: 2


#===============================================================================
# パーティション拡張
#===============================================================================
- name: "parted"
  parted:
    device: "/dev/{{ item.device }}"
    unit: GiB
    number: "{{ item.num }}"
    state: present
    label: gpt
  register: result
  with_items: "{{ device_list }}"
  when:
    - item.device is defined
    - item.num is defined
- debug:
    var: result
    verbosity: 2


#===============================================================================
# pv/vg作成・拡張
#===============================================================================
- name: "volume group"
  lvg:
    vg: "{{ item.vgname }}"
    pvs: "/dev/{{ item.pvname }}"
  with_items: "{{ device_list }}"
  when:
    - item.vgname is defined
    - item.pvname is defined


#===============================================================================
# lv作成・拡張
#===============================================================================
- name: "logical volume"
  lvol:
    vg: "{{ item.vgname }}"
    lv: "{{ item.lvname }}"
    size: "{{ item.lvsize }}"
    shrink: no
  with_items: "{{ device_list }}"
  when:
    - item.vgname is defined
    - item.lvname is defined
    - item.lvsize is defined


#===============================================================================
# ファイルシステム作成・拡張
#===============================================================================
#-------------------------------------------------------------------------------
# 存在確認
#-------------------------------------------------------------------------------
# filesystem作成・拡張
- name: Check if the device exists
  ansible.builtin.stat:
    path: "/dev/{{ item.vgname }}/{{ item.lvname }}"
  register: device_stat
  with_items: "{{ device_list }}"

#-------------------------------------------------------------------------------
# ファイルシステム作成
#-------------------------------------------------------------------------------
- name: "filesystem"
  filesystem:
    fstype: "{{ item.0.fstype }}"
    opts: "-L {{ item.0.label }}"
    dev: "/dev/{{ item.0.vgname }}/{{ item.0.lvname }}"
    resizefs: true
  with_together:
  - "{{ device_list }}"
  - "{{ device_stat.results }}"
  when:
    - item.0.vgname is defined
    - item.0.lvname is defined
    - item.0.label is defined
    - item.0.fstype is defined
    - item.1.stat.exists


#===============================================================================
# mount実行 / fstab追記
# state: mounted(fstab: 更新, mount: yes)
#        present(fstab: 更新, mount: no)
#===============================================================================
- name: "mount disk"
  mount:
    path: "{{ item.mount_point }}"
    src: "LABEL={{ item.label }}"
    fstype: "{{ item.fstype }}"
    state: mounted
  with_items: "{{ device_list }}"
  when:
    - item.mount_point is defined
    - item.label is defined
    - item.fstype is defined
```