Ansible Parse JSON Array from Register

Related Post: ansible parse json array reply from api

I have an Ansible playlist which registers a return variable:

- name: Create Instance ec2_instance: aws_access_key: "{{access_key}}" aws_secret_key: "{{secret_key}}" key_name: *** instance_type: t2.micro security_group: *** image_id: ami-39f8215b region: *** register: details 

So the details is a JSON object like this:

{ "details": { "changed": false, "changes": [], "failed": false, "instance_ids": [ "i-1111abcde" ], ... } 

All I want to do is write a text file with each instance_id in there:

i-1111abcde 

I've tried all of the following, none working:

debug: var: item with_items: details['instance_ids'] debug: var: item.item with_items: details['instance_ids'] debug: var: details.instance_ids with_items: details # This works, but prints the entire JSON array... 

Solution

- name: Debug Info debug: var: item loop: "{{details.instance_ids}}" - name: Write Temp File lineinfile: path: /tmp/temp.txt line: "{{ item }}" loop: "{{ details.instance_ids }}" 

Note: loop is a more modern Ansible concept that with_items or with_*

1

1 Answer

Solution

- name: Debug Info debug: var: item loop: "{{details.instance_ids}}" - name: Write Temp File lineinfile: path: /tmp/temp.txt line: "{{ item }}" loop: "{{ details.instance_ids }}" 

Note: loop is a more modern Ansible concept that with_items or with_*

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like