ansibleでディスクの操作を行う方法はいくつかあるが、特に使いそうな2つをメモしておく
※ansibleのverによって名前が変わるmoduleがあるため注意
動作環境
ansible 2.7.10
##partedでパーティションを切る
パーティションの情報をwith_itemsに持たせる。
複数パーティションを切る場合はwith_itemsに追加してやれば良い
roles/set_disk/tasks/main.yml
- name: "disk settings"
include: disk.yml
with_items:
- device: /dev/sdb
num: 1
label: gpt
fstype: xfs
mount_point: /home/foo/bar
owner: hoge
loop_control:
loop_var: disk
roles/set_disk/tasks/disk.yml
- name: "parted {{ disk.device }}"
parted:
device: "{{ disk.device }}"
number: "{{ disk.num }}"
state: present
label: "{{ disk.label }}"
become: yes
become_user: root
- name: "format {{ disk.device }}{{ disk.num }}"
filesystem:
dev: "{{ disk.device }}{{ disk.num }}"
fstype: "{{ disk.fstype }}"
become: yes
become_user: root
- name: "get blkid for {{ disk.device }}{{ disk.num }}"
shell: "blkid -s UUID -o value {{ disk.device }}{{ disk.num }}"
register: uuid
become: yes
become_user: root
- name: "Mount up device by UUID for {{ disk.device }}{{ disk.num }}"
mount:
path: "{{ disk.mount_point }}"
src: "UUID={{ uuid.stdout }}"
fstype: "{{ disk.fstype }}"
state: mounted
become: yes
become_user: root
- name: "chown mount point for {{ disk.device }}{{ disk.num }}"
file:
path: "{{ disk.mount_point }}"
owner: "{{ disk.owner }}"
group: "{{ disk.owner }}"
state: directory
become: yes
become_user: root
##LVMでディスクを拡張する
vgとlgの名前は特になければなんでも良い。
マウントポイントはpathに指定する。
roles/set_disk/tasks/main.yml
- name: create volume group
lvg:
vg: hoge
pvs: "/dev/sdb"
become: yes
- name: create logical volume
lvol:
vg: hoge
lv: fuga
size: +100%FREE
become: yes
- name: make filesystem
filesystem:
fstype: xfs
dev: /dev/hoge/fuga
resizefs: yes
become: yes
- name: mount disk
mount:
path: /home/foo/bar
src: /dev/hoge/fuga
fstype: xfs
state: mounted
become: yes
- name: "change permission for mount point"
file:
path: "/home/foo/bar"
mode: "0755"
owner: "hoge"
group: "hoge"
state: directory
become: yes