LoginSignup
6
7

More than 5 years have passed since last update.

CentOS6追加ディスクのマウントをAnsibleで自動化 (for IDCF Cloud)

Posted at

IDCFクラウドの仮想マシンはデフォルトのルートディスクが15GB固定であり、追加ディスクのマウントは手動で行う必要があるようです。これをAnsibleで自動化してみます。

たぶんMicrosoft Azure Virtual Machinesとかでもだいたい同じだと思います。

前提

  • クライアント側
    • ansible 2.1.1.0
  • サーバ側
    • IDCF Cloud VirtualMachine light.S1
    • CentOS 6.8
    • 構築時に1GBの追加データディスクをアタッチ済

手順

下記のような手順をAnsible化します

手順のイメージ
$ mkfs -t xfs /dev/sdb
$ mkdir /mnt/tmp_device
$ mount -t xfs -o defaults /dev/sdb /mnt/tmp_device
$ mv /var/* /mnt/tmp_device/.
$ sed -i -e "/^UUID/a UUID=` blkid /dev/sdb | awk -F\\" '{ print $2 }'` /var xfs defaults 0 0" /etc/fstab

Ansibleにはマウントとfstabへの追記を同時に行ってくれるmountモジュールがあり、これを活用します。

Ansible role

mount.yml
- hosts: all
  user: root
  vars:
    file_system: xfs
    device: /dev/sdb
    mount_point: /var
    reboot: true

  tasks:
    - name: yum install xfsprogs
      yum:
        name: xfsprogs
      when: file_system == "xfs"

    - name: mkfs
      filesystem:
        fstype: "{{ file_system }}"
        dev: "{{ device }}"

    - name: mkdir /mnt/tmp_device
      file:
        path: /mnt/tmp_device
        state: directory

    - name: mount /mnt/tmp_device
      mount:
        name: /mnt/tmp_device
        src: "{{ device }}"
        fstype: "{{ file_system }}"
        opts: defaults
        state: mounted

    - name: mv to /mnt/tmp_device/.
      shell: mv {{ mount_point }}/* /mnt/tmp_device/.

    - name: mkdir mount_point
      file:
        path: "{{ mount_point }}"
        state: directory

    - name: get blkid
      shell: 'blkid {{ device }} | awk -F\" ''{ print $2 }'' '
      register: uuid

    - name: unmount /mnt/tmp_device
      mount:
        name: /mnt/tmp_device
        src: "{{ device }}"
        fstype: "{{ file_system }}"
        opts: defaults
        state: absent

    - name: mount
      mount:
        name: "{{ mount_point }}"
        src: "UUID={{ uuid.stdout }}"
        fstype: "{{ file_system }}"
        opts: defaults
        state: mounted

    - name: reboot
      shell: shutdown -r now
      when: reboot == true
hosts
[app]
xxx.xxx.xxx.xxx

実行

$ ansible-playbook mount.yml -i hosts

結果

実行前

$ df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda1      ext4    15G  1.1G   13G   8% /
tmpfs          tmpfs  495M     0  495M   0% /dev/shm

実行後

$ df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda1      ext4    15G 1002M   13G   8% /
tmpfs          tmpfs  495M     0  495M   0% /dev/shm
/dev/sdb       xfs   1014M  119M  896M  12% /var

参考

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7