ShawnC
ShawnC

Reputation: 709

parted directive in Ansible start with first free location on disk when creating partition

I'm using Ansible to configure a RHEL VM on Azure, which is being created from a Marketplace image. I'm creating the VM with a larger OS disk (128GB vs. what I believe is the default of 64GB for the image), and want to create a new partition from the unused part of the disk. When using the parted directive in Ansible, I can use '100%' as the part_end parameter to tell it to make the new partition extend to the end, but is there a way to tell it to start the new partition at the next unallocated "spot" on the disk (or do I need to figure out what that is on my own and pass it in)?

Upvotes: 0

Views: 1211

Answers (2)

ShawnC
ShawnC

Reputation: 709

I had forgotten that I had posted this. I haven't tried the suggested approach above, but this is what I ended up doing:

- name: Get the device name for the root disk.
  command: /usr/bin/realpath /dev/disk/cloud/azure_root
  register: rootdev

- name: Update the GPT for the larger disk. 
  command: /usr/sbin/sgdisk -e {{ rootdev.stdout }} && /usr/sbin/partprobe
  become: yes
  become_user: root

- name: Create additional partition on extended OS disk for Kong use.
  parted:
    device: "{{ rootdev.stdout }}"
    number: "3"
    label: gpt
    state: present
    part_start: 69GB
    part_end: 100%
    part_type: primary

Upvotes: 0

Florian
Florian

Reputation: 36

I found this solution:

- name: "Get partition start size of /dev/{{ item.0.vg_dev }}"
  community.general.parted:
    device: "/dev/{{ item.0.vg_dev }}"
    unit: "MiB"
  register: "device_info"
- name: "Create Partition /dev/{{ item.0.vg_dev }}"
  community.general.parted:
    device: "/dev/{{ item.0.vg_dev }}"
    number: "{{ item.0.vg_part_num }}"
    part_start: "{% if device_info.partitions is defined and device_info.partitions|length > 0 %}{{ device_info.partitions[-1].end + 1 }}MiB{% else %}0%{% endif %}"
    part_end: "100%"
    label: "gpt"
    flags: [ lvm ]
    unit: "MiB"
    align: "optimal"
    state: "present"

Upvotes: 1

Related Questions