Question:
When I run this on the command line it works fine:
1 2 |
echo -e "n\np\n1\n\n\nw" | sudo fdisk /dev/sdb |
But in Ansible it does not want to run in shell:
1 2 3 |
- 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?
Answer:
With Ansible 2.3 and above, you can use parted
module to create partitions from a block device.
For example:
1 2 3 4 5 6 |
- parted: device: /dev/sdb number: 1 flags: [ lvm ] state: present |
To format the partition just use
filesystem
module as shown below:
1 2 3 4 |
- filesystem: fstype: ext2 dev: /dev/sdb1 |
To mount the partition to, let’s say,
/work
folder just use mount
module as shown below:
1 2 3 4 5 6 |
- mount: fstype: ext2 src: /dev/sdb1 path: /work state: mounted |