0

Here is my debug output from an ansible playbook:

    "msg": {
        "changed": false, 
        "failed": false, 
        "instances": [
            {
                .........
                "private_dns_name": "ip-10-XXX-XXX-XX.ec2.internal", 
                "private_ip_address": "10.XXX.XXX.XX", 
                "product_codes": [], 
                "public_dns_name": "", 
                "root_device_name": "/dev/sda1", 
                "root_device_type": "ebs", 
                "security_groups": ["...."], 
                "source_dest_check": true, 
                ......
                "private_dns_name": "ip-10.XXX.XXX.XX.ec2.internal", 
                "private_ip_address": "10.XXX.XXX.XX", 
                "product_codes": [], 
                "public_dns_name": "", 
                "root_device_name": "/dev/sda1", 
                "root_device_type": "ebs", 
                "security_groups": ["...."], 
                "source_dest_check": true, 
            }
        ]
    }
}

I want to do partition of disks from the created EC2 instances. output variable consists of IP addresses in JSON format where I want to loop through each IP address and get the disk information of the server, then I do partition using parted module as per below.

- name: Collect Disk Information 
      setup:
        gather_subset:
          - hardware
      delegate_to: "{{ output.instances[0].private_ip_address }}"
    
    - name: Print disks Details
      debug:
        var: hostvars[inventory_hostname].ansible_devices.keys()|list
     
    - name: Create Partition
      parted:
        device: "/dev/{{ item }}"
        number: 1
        part_type: 'primary'
        state: present
      delegate_to: "{{ output.instances[0].private_ip_address }}"
      with_items: "{{ hostvars[inventory_hostname].ansible_devices.keys() }}"

This task can only do partition on the instances[0] array, I want to loop through all the private_ip_address in the output variable. How to achieve this?

I tried with below as well

Try 1:

      delegate_to: "{{ item.private_ip_address }}"
      loop: "{{ output.instances }}"

1 Answers1

0

For example

- hosts: localhost
  vars:
    output:
      instances: [
         {"name": "ip-10", "ip": "10.1.0.10"},
         {"name": "ip-20", "ip": "10.1.0.20"}]
  tasks:
    - debug:
        msg: "delegate_to: {{ item.ip }}"
      loop: "{{ output.instances }}"

gives

  msg: 'delegate_to: 10.1.0.10'
  msg: 'delegate_to: 10.1.0.20'
Vladimir Botka
  • 5,138
  • 8
  • 20