Ansible 对硬盘自动分区并挂载

Ansible 对硬盘自动分区并挂载

将/dev/vdc先分区为lvm,再挂载到/data目录,/etc/fstab会自动添加

# ansible-playbook disk.yaml -e server_group=192.168.56.70 -e diskname=vdc

# cat disk.yaml 
- hosts: "{{ server_group }}"
  user: root
  gather_facts: true
  tasks:
    - name: install package parted lvm2
      yum: name={{ item }} state=present
      with_items:
       - parted
       - lvm2

    - name: Create a new primary partition
      parted:
        device: /dev/{{ diskname }}
        number: 1
        flags: [ lvm ]
        state: present

    - name: Create datavg vg
      lvg:
        vg: datavg
        pvs: /dev/{{ diskname }}1

    - name: Create data lvs
      lvol:
        vg: datavg
        lv: data
        size: 100%FREE
 
    - name: Format File System as xfs
      filesystem:
        fstype: xfs
        dev: /dev/datavg/data

    - name: Create /data Dir
      ansible.builtin.file:  path=/data state=directory mode=0755

    - name: Mount Partition to /data
      mount:
        name: /data
        src: /dev/datavg/data
        fstype: xfs
        state: mounted

 分区后的样子

# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 976M     0  976M   0% /dev
tmpfs                   1000M     0 1000M   0% /dev/shm
tmpfs                   1000M  8.6M  991M   1% /run
tmpfs                   1000M     0 1000M   0% /sys/fs/cgroup
/dev/vda1                 30G  1.5G   29G   5% /
tmpfs                    200M     0  200M   0% /run/user/0
/dev/mapper/datavg-data   20G   33M   20G   1% /data

# tail -n 1 /etc/fstab 
/dev/datavg/data /data xfs defaults 0 0

 

posted @ 2022-06-01 10:41  林中龙虾  阅读(896)  评论(0编辑  收藏  举报