How to create a new partition with Ansible
Asked Answered
C

5

21

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?

Carmacarmack answered 20/2, 2017 at 15:14 Comment(0)
S
60

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
Supportable answered 1/12, 2017 at 4:56 Comment(0)
C
12

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
Creepy answered 20/2, 2017 at 16:26 Comment(0)
U
12

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"
 
Updo answered 23/4, 2020 at 10:11 Comment(0)
N
3

With Ansible 2.12.5, tested and worked

---
# tasks file for formatting /dev/sdb for Longhorn
- name: Add disk to longhorn
  hosts: longhorn_disk
  become: true
  tasks:
    - parted:
        device: /dev/sdb
        number: 1
        state: present
       
    - filesystem:
        fstype: ext4
        dev: /dev/sdb1
       
    - mount:
        fstype: ext4
        src: /dev/sdb1
        path: /mnt/sdb1
        state: mounted
Nolen answered 28/8, 2022 at 7:28 Comment(0)
P
1

On my system helps double "n"

echo -e "\nn\np\n1\n\n\nw" | fdisk /dev/sdb

Pantheas answered 25/5, 2020 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.