如何使用数据磁盘创建Azure虚拟机,然后通过Ansible格式化它

i86rm4rw  于 2023-01-27  发布在  其他
关注(0)|答案(4)|浏览(137)

我可以单独执行,但不能将它们组合在一起,因为我不知道磁盘设备名称。
我的配置:

- 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中找不到此字段。如何修复它?

rjee0c15

rjee0c151#

***/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
brgchamk

brgchamk2#

我们可以使用软链接而不是/dev/sdb来格式化数据磁盘,该链接位于**/dev/disk/azure**中。
您可以运行命令“tree /dev/disk/azure”来了解详细结构。
下面是格式化一个数据磁盘的示例,如果有多个磁盘,您可以将软链接更改为/dev/disk/azure/scsi 1/lun 1、/dev/disk/azure/scsi 1/lun 2、/dev/disk/azure/scsi 1/lun 3...

- 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
drkbr07n

drkbr07n3#

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

tpgth1q7

tpgth1q74#

如果您需要LVM...

- name: Mount disks with logical volume management
  block:
    - name: Add disks to logical volume group
      community.general.lvg:
        vg: "{{ my_volume_group }}"
        pvs: "{{ my_physical_devices }}"

    - name: Manage logical volume
      community.general.lvol:
        vg: "{{ my_volume_group }}"
        lv: "{{ my_logical_volume }}"
        size: "{{ my_volume_size }}"

    - name: Manage mount point
      ansible.builtin.file:
        path: "{{ my_path }}"
        state: directory
        mode: 0755

    - name: Manage file system
      community.general.filesystem:
        dev: /dev/{{ my_volume_group }}/{{ my_logical_volume }}
        fstype: "{{ my_fstype }}"

    - name: Mount volume
      ansible.posix.mount:
        path: "{{ my_path }}"
        state: mounted
        src: /dev/{{ my_volume_group }}/{{ my_logical_volume }}
        fstype: "{{ my_fstype }}"
        opts: defaults,nodev

相关问题