Search code examples
dictionaryansibleyamlansible-factsdisk-partitioning

I need to create a YAML code to partition and format disk for windows , the input will be a dynamic /json file ,with the details of disk name,label


the input file format which i am using the (json/dictionary) as in below format dictionary :

 {
     "disks": {
    "disk2": {
      "name": "S",
      "label": "Sample",
      "disknum": 2
    },
    "disk3": {
      "name": "T",
      "label": "Testing",
      "disksize": 10
    },
    "disk4": {
      "name": "K",
      "label": "Urban",
      "disknum": 4
    }

the code which I have used is below, but the disk number is hardcoded here in the input, which I need to replace with the values retrieved from ansible win_disk_facts,

starting disk number need to be from 2, as disk 0, disk 1 is already consumed, can we include the disk number from facts in the loop mentioned below?

 - name: Perform Partition of disks
    win_partition:
    drive_letter: "{{item.value.name}}"
    partition_size: -1
    disk_number: "{{item.value.disknum}}"
    loop: "{{ lookup('dict', disks) }}"

Solution

  • i have found out a way to resolve these 2 situations

    1.increase the index value (with indexed_items)

    • debug: msg: "disk number is {{item.0 +2}} " with_indexed_items: - "{{ lookup('dict', disks) }}"

    2.use indexing value in partitioning (loop_control)

    • name: Perform Partition of disks win_partition: drive_letter: "{{item.value.name}}" partition_size: -1 disk_number: "{{ 2 + my_idx|int }}" loop: "{{ lookup('dict', disks) }}" loop_control: index_var: my_idx