When I run this on the command line it works fine:
echo -e "n\np\n1\n\n\nw" | sudo fdisk /dev/sdb
But in Ansible it does not want to run in shell:
- name: partition new disk
shell: echo -e "n\np\n1\n\n\nw" | sudo fdisk /dev/sdb
It does not come back with an error, but it does not create the partition either.
I checked that Ansible and LVM will not do what I need.
Any advice?
With Ansible 2.3 and above, you can use parted
module to create partitions from a block device.
For example:
- parted:
device: /dev/sdb
number: 1
flags: [ lvm ]
state: present
To format the partition just use filesystem
module as shown below:
- filesystem:
fstype: ext2
dev: /dev/sdb1
To mount the partition to, let's say, /work
folder just use mount
module as shown below:
- mount:
fstype: ext2
src: /dev/sdb1
path: /work
state: mounted
By default, Ansible executes /bin/sh
shell.
For example, if /bin/sh
is linked to dash
, it's built echo
is different to the one in bash
or GNU echo
; so you end up with -e
characters fed into fdisk.
Try:
- name: partition new disk
shell: echo -e "n\np\n1\n\n\nw" | sudo fdisk /dev/sdb
args:
executable: /bin/bash
Or:
- name: partition new disk
shell: /bin/echo -e "n\np\n1\n\n\nw" | sudo fdisk /dev/sdb
How to allocate all free space to a new partition and add it to LVM in Ansible
If you're using LVM, look into this!
If you want to use all the free space of the device, look into this!
Starting with a device /dev/sda
and an existing partition on /dev/sda1
in {{ volumeGroup }}
.
Use the following approach to create a partition /dev/sda2
in the free space of /dev/sda
and to subsequently add the new partition to the existing {{ volumeGroup }}
- name: "Create partitions on devices"
block:
- name: install parted
package:
name: parted
state: present
- name: "Read device information /dev/sda"
parted:
device: "/dev/sda"
unit: MiB
register: device_info
- name: "Add new partition /dev/sda2"
parted:
device: "/dev/sda"
number: "2"
part_type: primary
flags: [ lvm ]
state: present
part_end: "100%"
part_start: "{{ device_info.partitions[0].end + 1}}MiB"
- name: "Add device to exising volume group {{ volumeGroup }}."
lvg:
vg: "{{ volumeGroup }}"
pvs: "/dev/sda1,/dev/sda2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With