I need to automate the partition creation in several hundred server where a file system needs to be added.
the idea is have ansible get the number of the next partition and the start based on the output of parted info.
---
- hosts: all
become: true
tasks:
- name: get partition info
parted:
device: /dev/sda
unit: GiB
register: sda_info
- name: create new partition
parted:
device: /dev/sda
number: ???
part_start: ???
part_end: 3GiB
state: present
loop: '{{ sda_info.partitions }}
this is the output of sda_info
"sda_info": {
"changed": false,
"disk": {
"dev": "/dev/sda",
"logical_block": 512,
"model": "VMware Virtual disk",
"physical_block": 512,
"size": 49.0,
"table": "msdos",
"unit": "gib"
},
"failed": false,
"partitions": [
{
"begin": 0.0,
"end": 0.49,
"flags": [
"boot"
],
"fstype": "ext4",
"name": "",
"num": 1,
"size": 0.49,
"unit": "gib"
},
{
"begin": 0.49,
"end": 40.0,
"flags": [
"lvm"
],
"fstype": "",
"name": "",
"num": 2,
"size": 39.5,
"unit": "gib"
},
{
"begin": 40.0,
"end": 46.0,
"flags": [],
"fstype": "",
"name": "",
"num": 3,
"size": 6.0,
"unit": "gib"
}
],
"script": "unit 'GiB' print"
how can I calculate the number and the start based on the output of the sda_info.
In the example in hand the parted should be:
item.num +1 = 4
and part_start = 46
where the last partition on the disk ended.
every server has different disk structure so I need to get this dynamically.
i am thinking of using last for the number eg:
'{{ (sda_info.partitions|last).num +1 }}'
'{{ (sda_info.partitions|last).end }}'
but how to make sure that the sda_info.partitions is output in order and last
is actually the highest number and not just the last item in dictionary and end up destroy a bad partition?
sda_info.partition
a dictionary and dictionary are not ordered in python.
Here is simple playbook I've used with
unit: s
(sector as int) to avoid some errors because of floats:It creates a new partition at disk end using all free space.