Create a partition using Ansible playbook and extend the same

Below is the playbook which will create a LVM partition. Here, 
It will create PV, VG and then LVM which will be mounted on
/dir.
## To create pv, vg:vg_test, lv:lv_test, filesystem, and mount it on /dir
- hosts: server
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes


  tasks: 
  - name: To create a directory.
    file:
     path: /dir
     state: directory
     mode: 0755

  - name: To create phsyical volume.
    parted:
     device: /dev/sdb
     number: 1
     state: present
    
  - name: To create volume group.
    lvg:
     vg: vg_test
     pvs: /dev/sdb1

  - name: To create logical volume.
    lvol:
     vg: vg_test
     lv: lv_test
     size: 512m

  - name: To ceate filesystem on logical volume.
    filesystem:
     fstype: ext4
     dev: /dev/vg_test/lv_test

  - name: To mount newly created filesystem.
    mount:
     path: /dir
     src: /dev/vg_test/lv_test
     fstype: ext4
     state: mounted
Now below playbook will extend the LVM partition. 
#### Extending the Logical Volume

- hosts: server
  remote_user: ansible
  become: yes
  become_method: sudo
  gather_facts: yes


  tasks:
  - name: To extend logical volume by 30M. 
    command: lvextend -L+512M /dev/mapper/vg_test-lv_test

  - name: To resize filesystem.
    command: resize2fs /dev/mapper/vg_test-lv_test

Leave a Reply

Your email address will not be published. Required fields are marked *