Ansible script shell command exemple pvresize idempotent

Voir :

M(community.general.lvg)

ansible/playbook.yml

#! /usr/bin/env ansible-playbook --- - name: exemple hosts: all become: True vars: lvmconfig: [ { lv: root, sz: '40g' }, { lv: var, sz: '10g' }, { lv: log, sz: '100g' }, { lv: opt, sz: '10g' } ] roles: - lvm-resize

ansible/roles/lvm-resize/tasks/main.yml

--- # tasks file for roles/lvm-resize - name: pvresize needed ? script: files/pvresize_check.sh /dev/xvdk changed_when: false check_mode: false failed_when: - pvresize_check.rc != 0 # OK - pvresize_check.rc != 101 # Change to do register: pvresize_check - name: pvresize command: pvresize /dev/xvdk when: pvresize_check.rc == 101 - name: resize volume group lvg: vg: vg_os pvs: /dev/xvdk - name: configure sizes for each LVM partition lvol: vg=vg_os lv={{ item.lv }} size={{ item.sz }} state=present shrink=no resizefs=yes with_items: "{{ lvmconfig }}"

ansible/roles/lvm-resize/files/pvresize_check.sh

#!/bin/bash set -e set -o nounset DEV=$(basename "$(readlink -f "$1")") isNonEmptyStr () { echo "$@" | grep -q -v "^$" } # SIZE IN GB BLKDEV_SIZE=$(($(cat /sys/class/block/"$DEV"/size) * 512 / 1024 / 1024 / 1024)) PV_SIZE=$(pvs --noheadings /dev/"$DEV" |awk '{print $5}' |sed 's/^[^0-9]\+//' |sed 's/[^0-9].*//g') isNonEmptyStr "$BLKDEV_SIZE" ||exit 3 isNonEmptyStr "$PV_SIZE" ||exit 4 if [ "$BLKDEV_SIZE" -eq "$PV_SIZE" ] then exit 0 else exit 101 fi

Notes

Voir également

parted -s /dev/xvdk print free
pvs --noheadings -o pv_size --units b --nosuffix /dev/sdb vgs --noheadings -o pv_size --units b --nosuffix vg_data