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 }}" 
3
  • what could be the issue? Commented May 10, 2021 at 15:45
  • It's not clear what do you want to achieve. The loop will override the facts on each iteration. This is not what you want I guess. edit your question and make it mre. See the correct syntax in the answer. Commented May 10, 2021 at 16:00
  • @VladimirBotka, could you look into the question that I have updated. Commented May 10, 2021 at 16:10

1 Answer 1

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' 
2
  • actually I am not defining the variable.. output is the registered variable which comes from register Commented May 10, 2021 at 16:11
  • I have tried this but I am getting "rc": 1. what could be the cause Commented May 10, 2021 at 16:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.