How to create a new partition with Ansible

。_饼干妹妹 提交于 2020-01-01 04:27:11

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/42348098/how-to-create-a-new-partition-with-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!