bash 如何使用 Ansible 创建新分区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42348098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:47:51  来源:igfitidea点击:

How to create a new partition with Ansible

bashansibledisk-partitioning

提问by warhansen

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:

但是在 Ansible 中它不想在 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.

我检查了 Ansible 和 LVM 不会做我需要的。

Any advice?

有什么建议吗?

采纳答案by Konstantin Suvorov

By default, Ansible executes /bin/shshell.
For example, if /bin/shis linked to dash, it's built echois different to the one in bashor GNU echo; so you end up with -echaracters fed into fdisk.

默认情况下,Ansible 执行/bin/shshell。
例如,如果/bin/sh链接到dash,则它的构建echobash或 GNU 中的不同echo;所以你最终将-e字符输入到 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

回答by Jerald Sabu M

With Ansible 2.3 and above, you can use partedmoduleto create partitions from a block device. For example:

使用 Ansible 2.3 及更高版本,您可以使用parted模块从块设备创建分区。例如:

 - parted:
     device: /dev/sdb
     number: 1
     flags: [ lvm ]
     state: present

To format the partition just use filesystemmoduleas shown below:

要格式化分区,只需使用filesystem模块,如下所示:

 - filesystem:
     fstype: ext2
     dev: /dev/sdb1

To mount the partition to, let's say, /workfolder just use mountmoduleas shown below:

要将分区挂载到,比方说,/work文件夹只需使用mount模块,如下所示:

- mount:
    fstype: ext2
    src: /dev/sdb1
    path: /work
    state: mounted

回答by jschnasse

How to allocate all free space to a new partition and add it to LVM in Ansible

如何将所有可用空间分配给新分区并将其添加到 Ansible 中的 LVM

  • If you're using LVM, look into this!

  • If you want to use all the free space of an device, look into this!

  • 如果您正在使用 LVM,请查看此内容!

  • 如果您想使用设备的所有可用空间,请查看此内容!

Starting with a device /dev/sdaand an existing partition on /dev/sda1in {{ volumeGroup }}.

与设备开始/dev/sda并在现有分区/dev/sda1{{ volumeGroup }}

Use the following approach to create a partition /dev/sda2in the free space of /dev/sdaand to subsequently add the new partition to the existing {{ volumeGroup }}

使用以下方法/dev/sda2在可用空间中创建一个分区,/dev/sda然后将新分区添加到现有{{ 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"

回答by Kirill V

On my system helps double "n"

在我的系统上帮助加倍“n”

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

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