Delete Partition Table

359 views
Skip to first unread message

dma...@istreamplanet.com

unread,
Aug 20, 2018, 12:59:08 PM8/20/18
to Ansible Project
Hi everyone!

I'm trying to clear a partition table.  That is, delete all partitions.  I don't see how to do that with the parted module.

Thanks!
-Dylab

Stephane Emilien

unread,
Aug 25, 2018, 8:21:20 AM8/25/18
to Ansible Project
Hi,

Please note that the current state fo the 'parted' module is mainly focused around partition management (mostly creation and deletion)
It supports clearing/recreating the partition table but will only work if there's an actual *change of state* on a partition.

You can use either method for your case:

Delete all partitions on the block device:
As the doc says - https://docs.ansible.com/ansible/2.5/modules/parted_module.html
# Read device information (always use unit when probing)
- parted: device=/dev/sdb unit=MiB
 
register: sdb_info

# Remove all partitions from disk
- parted:
    device
: /dev/sdb
    number
: "{{ item.num }}"
    state
: absent
  with_items
:
   
- "{{ sdb_info.partitions }}"


Re-create the partition table:
If you want to clear the partition table itself, you'll need to switch it to another type and back to the desired one (msdos,gpt,bsd,sun,etc...)
>>>
- parted:
    device
: /dev/sdb
    label
: "{{ item.label }}"
    number
: "{{ item.num }}"
    state
: "{{ item.state}}"
  with_items
:
   
- { num: 1, label: amiga, state: absent } # Remove the first partition and create a new 'amiga' partition table
   
- { num: 1, label: gpt, state: present }  # Necessary step, recreates the partition table with a partition
   
- { num: 1, label: gpt, state: absent }   # This removes the newly created partition

- SDE
Reply all
Reply to author
Forward
0 new messages