1

我可以分开做,但不能将它们组合在一起,因为我不知道磁盘设备名称。

我的配置:

- name: Create Virtual Machine
  azure_rm_virtualmachine:
  resource_group: "{{ resource_group }}"
  name: "{{ item }}"
  vm_size: "{{ flavor }}"
  managed_disk_type: "{{ disks.disk_type }}"
  network_interface_names: "NIC-{{ item }}"
  ssh_password_enabled: false
  admin_username: "{{ cloud_config.admin_username }}"
  image:
    offer:  "{{ image.offer }}"
    publisher: "{{ image.publisher }}"
    sku: "{{ image.sku }}"
    version: "{{ image.version }}"
  tags: 
    Node: "{{ tags.Node }}"
  ssh_public_keys:
    - path: "/home/{{ cloud_config.admin_username }}/.ssh/authorized_keys"
      key_data: "{{ cloud_config.ssh.publickey }}"
  data_disks:
    - lun: 0
      disk_size_gb: "{{ disks.disk_size }}"
      caching: "{{ disks.caching }}"
      managed_disk_type: "{{ disks.disk_type }}"

格式化和挂载磁盘的其他部分

- name: partition new disk
  shell: 'echo -e "n\np\n1\n\n\nw" | fdisk /dev/sdc'
  args:
    executable: /bin/bash

- name: Makes file system on block device
  filesystem:
    fstype: xfs
    dev: /dev/sdc1

- name: new dir to mount
  file: path=/hadoop state=directory

- name: mount the dir
  mount:
    path: /hadoop
    src: /dev/sdc1
    fstype: xfs
    state: mounted

我的问题:无法配置设备名称。它可以是 /dev/sdc 或 /dev/sdb。对于 AWS ec2,我可以设置volumes[device_name],但是我在 Azure 中没有找到这样的字段。我该如何解决?

4

3 回答 3

1

/dev/sdb默认用于临时磁盘,但有时它被我的数据磁盘使用。我找到了一种在格式化之前检查设备名称的解决方法。我知道这不是一个聪明的方法。

- name: check device name which should be parted
  shell: parted -l
  register: device_name

- name: Show middle device name 
  debug:
    msg: "{{ device_name.stderr.split(':')[1] }}"
  register: mid_device

- name: Display real device name 
  debug: 
    msg: "{{ mid_device.msg.split()[0] }}"
  register: real_device

- name: partition new disk
  shell: 'echo -e "n\np\n1\n\n\nw" | fdisk {{ real_device.msg }}'
  args:
    executable: /bin/bash

- name: Makes file system on block device
  filesystem:
    fstype: xfs
    dev: "{{ real_device.msg }}1"

- name: new dir to mount
  file: path=/hadoop state=directory

- name: mount the dir
  mount:
    path: /hadoop
    src: "{{ real_device.msg }}1"
    fstype: xfs
    state: mounted
于 2018-03-23T08:32:34.480 回答
1

我们可以使用软链接而不是 /dev/sdb 来格式化数据盘,链接位于/dev/disk/azure

您可以运行命令“tree /dev/disk/azure”来了解详细结构。

下面是我格式化一个数据盘的例子,如果有更多的磁盘,你可以将软链接更改为 /dev/disk/azure/scsi1/lun1, /dev/disk/azure/scsi1/lun2, /dev/disk /天蓝色/scsi1/lun3...

- name: use parted to make label
  shell: "parted /dev/disk/azure/scsi1/lun0 mklabel msdos"
  args:
    executable: /bin/bash

- name: partition new disk
  shell: "parted /dev/disk/azure/scsi1/lun0 mkpart primary 1 100%"
  args:
    executable: /bin/bash

- name: inform the OS of partition table changes (partprobe)
  command: partprobe

- name: Makes file system on block device with xfs file system
  filesystem:
    fstype: xfs
    dev: /dev/disk/azure/scsi1/lun0-part1

- name: create data dir for mounting
  file: path=/data state=directory

- name: Get UUID of the new filesystem
  shell: |
    blkid -s UUID -o value $(readlink -f /dev/disk/azure/scsi1/lun0-part1)
  register: uuid

- name: show real uuid
  debug:
    msg: "{{ uuid.stdout }}"

- name: mount the dir
  mount:
    path: /data
    src: "UUID={{ uuid.stdout }}"
    fstype: xfs
    state: mounted

- name: check disk status
  shell: df -h | grep /dev/sd
  register: df2_status

- debug: var=df2_status.stdout_lines
于 2019-09-25T08:19:19.903 回答
0

也许可以尝试该azure_rm_managed_disk模块,然后将其附加到 VM。然后你就拥有了磁盘的所有属性。

于 2018-04-16T23:05:56.257 回答