ChatGPTでPython Programming

IDEのDISKをJsonのVM specから削除


パスワードの暗号化と複合化



Pythonでsubprocessモジュールを利用して、ssh接続を行い、サーバ上のシェルスクリプトを実行



Pythonで以下のJsonファイルから、vm_disk_info.is_cdrom==true, かつvm_disk_info.disk_address.device_bus=="ide" の要素を削除

{
    "allow_live_migrate": true,
    "gpus_assigned": false,
    "boot": {
        "uefi_boot": false,
        "secure_boot": false,
        "hardware_virtualization": false
    },
    "ha_priority": 0,
    "memory_mb": 4096,
    "name": "hm-Win2016-template",
    "num_cores_per_vcpu": 1,
    "num_vcpus": 2,
    "power_state": "off",
    "timezone": "Asia/Tokyo",
    "uuid": "da4ea00f-a48a-416f-a43a-4cc9b999c4d7",
    "vm_disk_info": [
        {
            "disk_address": {
                "device_bus": "ide",
                "device_index": 0,
                "disk_label": "ide.0",
                "device_uuid": "84b3f154-542b-4265-851f-7c2bbcfa579b"
            },
            "is_cdrom": true,
            "is_empty": true,
            "flash_mode_enabled": false,
            "is_scsi_passthrough": true,
            "is_hot_remove_enabled": false,
            "is_thin_provisioned": false,
            "shared": false
        },
        {
            "disk_address": {
                "device_bus": "ide",
                "device_index": 1,
                "disk_label": "ide.1",
                "ndfs_filepath": "/default-container-162244/.acropolis/vmdisk/d1c65a4e-42fc-40cc-91f7-003b80e512b6",
                "vmdisk_uuid": "d1c65a4e-42fc-40cc-91f7-003b80e512b6",
                "device_uuid": "dc8c4ef5-af5c-4000-aa35-5a5c6288c093"
            },
            "is_cdrom": true,
            "is_empty": false,
            "flash_mode_enabled": false,
            "is_scsi_passthrough": true,
            "is_hot_remove_enabled": false,
            "is_thin_provisioned": false,
            "shared": false,
            "source_disk_address": {
                "ndfs_filepath": "/default-container-162244/.snapshot/66/162241-1634562656131278-1152766/.acropolis/vmdisk/a92f0db3-6da3-401e-a660-bc527c64ead0"
            },
            "storage_container_uuid": "67d1ef9c-9d47-4b71-83a8-55fe37a72529",
            "size": 1277752832
        },
        {
            "disk_address": {
                "device_bus": "scsi",
                "device_index": 0,
                "disk_label": "scsi.0",
                "ndfs_filepath": "/default-container-162244/.acropolis/vmdisk/71bd3efb-879d-4c25-838d-0df9b478eb56",
                "vmdisk_uuid": "71bd3efb-879d-4c25-838d-0df9b478eb56",
                "device_uuid": "28536504-8c09-4499-9c58-e2b600a07c8a"
            },
            "is_cdrom": false,
            "is_empty": false,
            "flash_mode_enabled": false,
            "is_scsi_passthrough": true,
            "is_hot_remove_enabled": true,
            "is_thin_provisioned": false,
            "shared": false,
            "source_disk_address": {
                "ndfs_filepath": "/default-container-162244/.snapshot/66/162241-1634562656131278-1152766/.acropolis/vmdisk/29502fa9-a7e3-4c41-9aaf-c2aa193ffa16"
            },
            "storage_container_uuid": "67d1ef9c-9d47-4b71-83a8-55fe37a72529",
            "size": 53687091200
        }
    ],
    "vm_features": {
        "VGA_CONSOLE": true,
        "AGENT_VM": false
    },
    "vm_logical_timestamp": 1,
    "vm_nics": [],
    "machine_type": "pc"
}



# Loadデータ

with open("vm.json", "r") as f:

  data = json.load(f)

# vm_disk_infoキーの値(リスト型データ)を取り出す
vm_disk_info = data['vm_disk_info']

# リストから、is_cdrom==trueかつdisk_address.device_bus=='ide'、device_index==0の要素を削除
vm_disk_info = [x for x in vm_disk_info if not (x['is_cdrom'] and x['disk_address']['device_bus'] == 'ide' and x['disk_address']['device_index'] == 0)]

# change data

data['vm_disk_info'] = vm_disk_info

# write back to the file

with open("vm1_modified.json", "w") as f:
  json.dump(data, f, indent=2)

この記事が気に入ったらサポートをしてみませんか?