Jonny
Jonny

Reputation: 21

Create logical volume with ansible and if statement

I'm trying to create a playbook that does the following,

  1. Create a logical volume with specific size, if the volume group doesn't have enough size output a message and use another size.
  2. If volume group doesn't exist exit with output message
  3. If logical volume created format and mount to disk

I am trying to find the a way to do this, but I could not find anything on google that would help me to solve this.

I'm using ansible 2.7.8

Upvotes: 2

Views: 7110

Answers (2)

Steven com my
Steven com my

Reputation: 25

Lets say you have 100 nodes and 1 control nodes. 100 nodes is mixed of some that have a VG called databin and some don't and 100 nodes is mixed of various size of VG databin

  1. Create a logical volume with specific size, if the volume group doesn't have enough size output a message and use another size. ( LV name = dbdata and size 2.5G if VG databin have sufficient size or fall back to 1G )

  2. If volume group doesn't exist exit with output message ( if VG databin not there, we skip )

  3. If logical volume created format and mount to disk ( if dbdata LV created, format with ext4 and mount to /db )

--sample Playbook using Block and Fail ( error handling )--

---
 - name: Setup LVM
   hosts: all
   tasks:
     - name: Check for databin VG
       fail:
         msg: VG databin does not exist
       when:
         - ansible_lvm['vgs']['databin'] is not defined

     - name: Create LVM and Filesystem and Mount
       block:
         - name: Check for databin VG Size of 2500MiB
           fail:
             msg: Could not create LV with 2500MiB size
           when:
             - ansible_lvm['vgs']['databin']['size_g'] < "2.50"
         - name: Create 2500MiB LVM on databin VG
           lvol:
              lv: dbdata
              vg: databin
              size: "2532"
       rescue:
         - name: Fall back and Create 1024MiB LVM on databin VG
           lvol:
             size: "1024"
             vg: databin
             lv: dbdata
       always:
         - name: Set Filesystem
           filesystem:
             dev: /dev/databin/dbdata
             fstype: ext4
         - name: Mount LV 
           mount:
             path: /db
             src: /dev/databin/dbdata
             fstype: ext4
             state: present

Upvotes: 2

Live_
Live_

Reputation: 71

I would look at "block" in ansible. Block, allows you to do one operation and capture any errors with "rescue" and then do something else.

For question 2, I would check out debug and how to use it with a "when" condition.

I.E: when: vgname not in ansible_lvm.vgs

Upvotes: 0

Related Questions