如何使用数据磁盘创建Azure vm,然后通过ansible对其进行格式化

时间:2018-03-22 09:31:00

标签: azure ansible azure-managed-disk

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

我的配置:

- 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,我可以设置卷[device_name],但我在Azure中找不到这样的字段。我怎么能解决它?

3 个答案:

答案 0 :(得分:0)

/ 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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

这里是我的示例,用于格式化一个数据磁盘,如果有更多磁盘,则可以将软链接更改为/ dev / disk / azure / scsi1 / lun1,/ dev / disk / azure / scsi1 / lun2,/ dev / disk / azure / 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