How to create a new partition with Ansible

前端 未结 4 1012
走了就别回头了
走了就别回头了 2021-02-05 09:53

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 ru

相关标签:
4条回答
  • 2021-02-05 10:19

    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"
     
    
    0 讨论(0)
  • 2021-02-05 10:31

    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
    
    0 讨论(0)
  • 2021-02-05 10:35

    On my system helps double "n"

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

    0 讨论(0)
  • 2021-02-05 10:40

    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
    
    0 讨论(0)
提交回复
热议问题