Skip to content

Instantly share code, notes, and snippets.

@waddles
Last active July 6, 2020 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waddles/bf8d88e611b8066036f40e5688ff6e76 to your computer and use it in GitHub Desktop.
Save waddles/bf8d88e611b8066036f40e5688ff6e76 to your computer and use it in GitHub Desktop.
Ansible play to expand disk and upgrade Ubuntu
- block:
- name: Upgrade OS to latest LTS release
shell: |
test $(lsb_release -si) = Ubuntu || exit 0
parted /dev/sda unit s print | awk '
/^Disk \// {
total = substr($3,0,length($3)-1)
}
$5 == "extended" {
xpnum = $1
xsectors = substr($3,0,length($3)-1)
}
$5 == "logical" {
lpnum = $1
lsectors = substr($3,0,length($3)-1)
}
END {
if (lsectors > 1 && lsectors < total - 1) {
printf "resizepart %s %ss\n", xpnum, total - 1
printf "resizepart %s %ss\n", lpnum, total - 1
}
}' | xargs parted --script /dev/sda
pvresize /dev/sda5
lvresize /dev/ubuntu-vg/root -l '+100%FREE'
resize2fs /dev/ubuntu-vg/root
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get upgrade -q -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" --with-new-pkgs
apt-get autoremove -y
do-release-upgrade -f DistUpgradeViewNonInteractive
systemctl reboot
vars:
ansible_host: "{{ vm_ipaddress }}"
become: True
- name: Wait for reboot after OS upgrade
wait_for:
port: "{{ ansible_port | default('22') }}"
host: "{{ vm_ipaddress }}"
connection: local
become: no
when:
- nutanix.created is defined and nutanix.created
- "'linux' in group_names"
- not ansible_check_mode
@waddles
Copy link
Author

waddles commented Jun 23, 2020

This play gets run straight after provisioning a VM in Nutanix*. Typically I would run the full Ansible playbook then go back to upgrade the OS interactively but this play does the upgrade for me automatically.

Nutanix provisions a VM from a disk image clone of a minimal Ubuntu installation but with an expanded size.

This play

  1. expands the MBR partition table's extended partition (usually sda2)
  2. expands the logical volume (sda5) within the extended volume
  3. resizes the LVM Physical Volume to cover the expanded logical volume
  4. expands the LVM Logical Volume for the root filesystem
  5. resizes the Ext4 root filesytem
  6. updates the Ubuntu package repos
  7. upgrades installed packages on current release
  8. upgrades Ubuntu to the latest LTS release

all before knowing anything about the VM except it's IP address.

Pros

  • gets run before Ansible has gathered any facts or modified any configs
  • ensures I'm left with a fully updated and configured VM

Cons

  • it’s very opinionated about how the disks are laid out and named - will only work on Ubuntu
  • it assumes you always want the latest LTS release and maybe not all roles can support that version yet
  • it adds about 15 minutes to creating a new linux VM

* (actually there is a step in between to set the vm_ipaddress fact)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment