# How to create with\_items, on a submember?

**URL:** <https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036>\
**Category:** Ansible Project\
**Created:** [March 22, 2017, 5:44pm UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036 "2017-03-22T17:44:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 22, 2017, 5:44pm UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/1 "2017-03-22T17:44:10Z")

</div>

The output (i.e. module: ec2\_snapshot) of a registered variable (i.e. ec2\_snap) returns multiple members.

Playbook is:

— # Take Snapshot of EC2s

- name: Take snapshot of EC2s  
hosts: local  
become: no  
connection: local  
gather\_facts: yes  
vars:  
region: “us-east-1”  
awsec2id: [“i-037b596065189XXXXX”,“i-04452f010e4cXXXXX”]  
devname: “/dev/sda1”  
date: “{{ lookup(‘pipe’, ‘date +%Y/%m/%d-%H:%M’) }}”  
tasks:

- name: Create Root snapshots  
ec2\_snapshot:  
region: ‘{{ region }}’  
instance\_id: ‘{{ item }}’  
device\_name: ‘{{ devname }}’  
description: ‘snapshot of /dev/sda1 from {{ item }} taken {{ date }}’  
state: present  
snapshot\_tags:  
frequency: daily  
source: ‘{{ devname }}’  
register: ec2\_snap  
with\_items: ‘{{ awsec2id }}’

- debug: var=ec2\_snap

- debug: var=ec2\_snap.results[0].invocation.module\_args.device\_name

- debug: var=ec2\_snap.results[0].item

- debug: var=ec2\_snap.results[0].volume\_id

- debug: var=ec2\_snap.results[0].snapshot\_id

- name: “Task 2: Create files for each instance”  
command: touch /home/ansible/playbooks/common/variables/‘{{ item }}.yml’  
with\_items: “{{ awsec2id }}”

- name: “Task 3: Add string instance\_id to file for each instance”  
shell: ‘printf “{{ item.volume\_id }}” \> /home/ansible/playbooks/common/variables/“{{ item.item }}”’  
with\_items: “{{ ec2\_snap.results }}”  
…

**Error making code:**

- name: “Task 3: Add string instance\_id to file for each instance”  
shell: ‘printf “{{ item.volume\_id }}” \> /home/ansible/playbooks/common/variables/“{{ item.item }}”’  
with\_items: “{{ ec2\_snap.results }}”

ec2\_snap registered variable is having details of each of two snapshots in a relevant array element. How to create loop on them?

**Short Output is as follow:**

{  
“changed”:false,  
“ec2\_snap”:{  
“changed”:true,  
“msg”:“All items completed”,  
“results”:[  
{ },  
{ }  
]  
}  
}

**The extended output is as follow:**

ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap”: {  
“changed”: true,  
“msg”: “All items completed”,  
“results”: [  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: true,  
“invocation”: {  
“module\_args”: {  
“aws\_access\_key”: null,  
“aws\_secret\_key”: null,  
“description”: “snapshot of /dev/sda1 from i-037b5960651XXXXX taken 2017/03/22-17:19”,  
“device\_name”: “/dev/sda1”,  
“ec2\_url”: null,  
“instance\_id”: “i-037b59606518XXXXX”,  
“last\_snapshot\_min\_age”: 0,  
“profile”: null,  
“region”: “us-east-1”,  
“security\_token”: null,  
“snapshot\_id”: null,  
“snapshot\_tags”: {  
“frequency”: “daily”,  
“source”: “/dev/sda1”  
},  
“state”: “present”,  
“validate\_certs”: true,  
“volume\_id”: null,  
“wait”: true,  
“wait\_timeout”: 0  
}  
},  
“item”: “i-037b59606518XXXXX”,  
“snapshot\_id”: “snap-08d7aeda75dbXXXXX”,  
“tags”: {  
“frequency”: “daily”,  
“source”: “/dev/sda1”  
},  
“volume\_id”: “vol-09aec429dfcfXXXXX”,  
“volume\_size”: 8  
},  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: true,  
“invocation”: {  
“module\_args”: {  
“aws\_access\_key”: null,  
“aws\_secret\_key”: null,  
“description”: “snapshot of /dev/sda1 from i-04452f010e4XXXXX taken 2017/03/22-17:20”,  
“device\_name”: “/dev/sda1”,  
“ec2\_url”: null,  
“instance\_id”: “i-04452f010e4cXXXXX”,  
“last\_snapshot\_min\_age”: 0,  
“profile”: null,  
“region”: “us-east-1”,  
“security\_token”: null,  
“snapshot\_id”: null,  
“snapshot\_tags”: {  
“frequency”: “daily”,  
“source”: “/dev/sda1”  
},  
“state”: “present”,  
“validate\_certs”: true,  
“volume\_id”: null,  
“wait”: true,  
“wait\_timeout”: 0  
}  
},  
“item”: “i-04452f010e4caXXXXX”,  
“snapshot\_id”: “snap-0f8cb9b9d7b4XXXXX”,  
“tags”: {  
“frequency”: “daily”,  
“source”: “/dev/sda1”  
},  
“volume\_id”: “vol-0e0516ea9623XXXXX”,  
“volume\_size”: 15  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 22, 2017, 6:00pm UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/2 "2017-03-22T18:00:10Z")

</div>

- Can it be resolved with with\_submemebrs?
- Can someone please help me to understand?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 22, 2017, 7:29pm UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/3 "2017-03-22T19:29:54Z")

</div>

\<snip\>

I this output of ec2\_snap is correct there is nothing wrong with your loop code.

Since you haven't provided any output of the error it's impossible to help.

---

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 23, 2017, 2:55am UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/4 "2017-03-23T02:55:32Z")

</div>

Piece of code is as follow:

**# Manual accessing sub-elements**

- debug: var=ec2\_snap
- debug: var=ec2\_snap.results[0].invocation.module\_args.device\_name
- debug: var=ec2\_snap.results[0].item
- debug: var=ec2\_snap.results[0].volume\_id
- debug: var=ec2\_snap.results[0].snapshot\_id
- debug: var=ec2\_snap.results[1].invocation.module\_args.device\_name
- debug: var=ec2\_snap.results[1].item
- debug: var=ec2\_snap.results[1].volume\_id
- debug: var=ec2\_snap.results[1].snapshot\_id

**# Works fine, Output for above code is as follow:**

ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap.results[0].invocation.module\_args.device\_name”: “/dev/sda1”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansible/playbooks/rol1.yml:27  
ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap.results[0].item”: “i-037b59606518XXXXX”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansible/playbooks/rol1.yml:28  
ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap.results[0].volume\_id”: “vol-09aec429dfcXXXXX”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansible/playbooks/rol1.yml:29  
ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap.results[0].snapshot\_id”: “snap-0fe95f5d794XXXXX”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansible/playbooks/rol1.yml:30  
ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap.results[1].invocation.module\_args.device\_name”: “/dev/sda1”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansible/playbooks/rol1.yml:31  
ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap.results[1].item”: “i-04452f010e4cXXXXX”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansible/playbooks/rol1.yml:32  
ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap.results[1].volume\_id”: “vol-0e0516ea962XXXXX”  
}

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/ansible/playbooks/rol1.yml:33  
ok: [localhost] =\> {  
“changed”: false,  
“ec2\_snap.results[1].snapshot\_id”: “snap-0fa1ec99d379XXXXX”  
}

**When trying to access with\_items, with following code:**

- name: “Task 3: Add string instance\_id to file for each instance”  
shell: ‘printf “{{ item.volume\_id }}”’  
#shell: ‘printf “{{ item.volume\_id }}” \> /home/ansible/playbooks/common/variables/“{{ item.item }}”’  
with\_items:
- “{{ ec2\_snap.results }}”

**Rather than giving proper output, as appeared in above manual access, it gives garbrage as follow:**

changed: [localhost] =\> (item={‘\_ansible\_parsed’: True, u’changed’: True, ‘\_ansible\_no\_log’: False, ‘item’: u’i-04452f010e4cXXXXX’, ‘\_ansible\_item\_result’: True, u’volume\_size’: 15, u’snapshot\_id’: u’snap-0fa1ec99d379XXXXX’, u’volume\_id’: u’vol-0e0516ea9623XXXXX’, u’invocation’: {u’module\_args’: {u’aws\_secret\_key’: None, u’profile’: None, u’aws\_access\_key’: None, u’description’: u’snapshot of /dev/sda1 from i-04452f010e4cXXXXX taken 2017/03/22-19:58’, u’security\_token’: None, u’snapshot\_tags’: {u’source’: u’/dev/sda1’, u’frequency’: u’daily’}, u’region’: u’us-east-1’, u’snapshot\_id’: None, u’device\_name’: u’/dev/sda1’, u’instance\_id’: u’i-04452f010e4cXXXXX’, u’state’: u’present’, u’wait\_timeout’: 0, u’ec2\_url’: None, u’last\_snapshot\_min\_age’: 0, u’validate\_certs’: True, u’volume\_id’: None, u’wait’: True}}, u’tags’: {u’source’: u’/dev/sda1’, u’frequency’: u’daily’}}) =\> {  
“changed”: true,  
“cmd”: “printf "vol-0e0516ea9623XXXXX"”,  
“delta”: “0:00:00.002131”,  
“end”: “2017-03-22 19:58:13.225366”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “printf "vol-0e0516ea962XXXXX"”,  
“\_uses\_shell”: true,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“warn”: true  
}  
},  
“item”: {  
“changed”: true,  
“invocation”: {  
“module\_args”: {  
“aws\_access\_key”: null,  
“aws\_secret\_key”: null,  
“description”: “snapshot of /dev/sda1 from i-04452f010e4cacb0b taken 2017/03/22-19:58”,  
“device\_name”: “/dev/sda1”,  
“ec2\_url”: null,  
“instance\_id”: “i-04452f010e4cXXXXX”,  
“last\_snapshot\_min\_age”: 0,  
“profile”: null,  
“region”: “us-east-1”,  
“security\_token”: null,  
“snapshot\_id”: null,  
“snapshot\_tags”: {  
“frequency”: “daily”,  
“source”: “/dev/sda1”  
},  
“state”: “present”,  
“validate\_certs”: true,  
“volume\_id”: null,  
“wait”: true,  
“wait\_timeout”: 0  
}  
},  
“item”: “i-04452f010e4XXXXX”,  
“snapshot\_id”: “snap-0fa1ec99d379XXXXX”,  
“tags”: {  
“frequency”: “daily”,  
“source”: “/dev/sda1”  
},  
“volume\_id”: “vol-0e0516ea9623XXXXX”,  
“volume\_size”: 15  
},  
“rc”: 0,  
“start”: “2017-03-22 19:58:13.223235”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “vol-0e0516ea962XXXXX”,  
“stdout\_lines”: [  
“vol-0e0516ea9623XXXXX”  
]  
}

---

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 23, 2017, 4:17am UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/5 "2017-03-23T04:17:14Z")

</div>

**Same way another looping issue is as follow (actual code):**

- name: “Task 2: Fetch EC2 details”  
ec2\_remote\_facts:  
aws\_access\_key: ‘{{ AwsAccesskey }}’  
aws\_secret\_key: ‘{{ AwsSecretkey }}’  
region: ‘{{ region }}’  
filters:  
instance\_id: ‘{{ item }}’  
register: ec2\_hosts  
with\_items: “{{ awsec2id }}”
- debug: var=ec2\_hosts
- debug: var=ec2\_hosts.results[0].instances[0].private\_ip\_address
- debug: var=ec2\_hosts.results[1].instances[0].private\_ip\_address

**with\_items: Looping for the above:**

- name: looping  
shell: ‘printf “{{ item.instance[0].private\_ip\_address }}”’  
with\_items: “{{ ec2\_hosts.results }}”

**Error:**  
TASK [awsAtachEBSvolume : looping] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘dict object’ has no attribute ‘instance’\n\nThe error appears to have been in ‘/home/ansible/playbooks/roles/awsAtachEBSvolume/tasks/main.yml’: line 30, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - debug: var=awsec2id\n - name: looping\n ^ here\n”}  
to retry, use: --limit @/home/ansible/playbooks/rol.retry

**Output registered variable ec2\_hosts, follow:**  
ok: [localhost] =\> {  
“changed”: false,  
“ec2\_hosts”: {  
“changed”: false,  
“msg”: “All items completed”,  
“results”: [  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“instances”: [  
{  
“ami\_launch\_index”: “0”,  
“architecture”: “x86\_64”,  
“block\_device\_mapping”: [  
{  
“attach\_time”: “2017-03-17T08:02:13.000Z”,  
“delete\_on\_termination”: false,  
“device\_name”: “/dev/xvda”,  
“status”: “attached”,  
“volume\_id”: “vol-0573516d4320XXXXX”  
},  
{  
“attach\_time”: “2017-03-21T08:32:27.000Z”,  
“delete\_on\_termination”: false,  
“device\_name”: “/dev/xvdl”,  
“status”: “attached”,  
“volume\_id”: “vol-07556df02e90XXXXX”  
}  
],  
“client\_token”: “IQNIH14887952XXXXX”,  
“ebs\_optimized”: false,  
“groups”: [  
{  
“id”: “sg-dbcXXXXX”,  
“name”: “launch-wizard-14”  
}  
],  
“hypervisor”: “xen”,  
“id”: “i-0003df8d26bXXXXX”,  
“image\_id”: “ami-0b33XXXXX”,  
“instance\_profile”: null,  
“interfaces”: [  
{  
“id”: “eni-8fdfbXXXXX”,  
“mac\_address”: “0e:df:73:4c:67:e6”  
}  
],  
“kernel”: null,  
“key\_name”: “XXXXX”,  
“launch\_time”: “2017-03-21T04:02:10.000Z”,  
“monitoring\_state”: “disabled”,  
“persistent”: false,  
“placement”: {  
“tenancy”: “default”,  
“zone”: “us-east-1c”  
},  
“private\_dns\_name”: “ip-XXXXX.ec2.internal”,  
“private\_ip\_address”: “172.XXXXX”,  
“public\_dns\_name”: “[ec2-XXXXX-156.compute-1.amazonaws.com](http://ec2-XXXXX-156.compute-1.amazonaws.com)”,  
“public\_ip\_address”: “54.89.XXXXX”,  
“ramdisk”: null,  
“region”: “us-east-1”,  
“requester\_id”: null,  
“root\_device\_type”: “ebs”,  
“source\_destination\_check”: “true”,  
“spot\_instance\_request\_id”: null,  
“state”: “running”,  
“tags”: {  
“Name”: “XXXXX”  
},  
“virtualization\_type”: “hvm”,  
“vpc\_id”: “vpc-76XXXXX”  
}  
],  
“invocation”: {  
“module\_args”: {  
“aws\_access\_key”: “AKIAIGKMK7EXXXXX”,  
“aws\_secret\_key”: “VALUE\_SPECIFIED\_IN\_NOXXXXX”,  
“ec2\_url”: null,  
“filters”: {  
“instance\_id”: “i-0003df8d26XXXXX”  
},  
“profile”: null,  
“region”: “us-east-1”,  
“security\_token”: null,  
“validate\_certs”: true  
}  
},  
“item”: “i-0003df8d26b5XXXXX”  
},  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“instances”: [  
{  
“ami\_launch\_index”: “0”,  
“architecture”: “x86\_64”,  
“block\_device\_mapping”: [  
{  
“attach\_time”: “2017-03-21T08:32:38.000Z”,  
“delete\_on\_termination”: false,  
“device\_name”: “/dev/xvdl”,  
“status”: “attached”,  
“volume\_id”: “vol-0118435137XXXXX”  
},  
{  
“attach\_time”: “2017-03-22T08:28:47.000Z”,  
“delete\_on\_termination”: false,  
“device\_name”: “/dev/sda1”,  
“status”: “attached”,  
“volume\_id”: “vol-0e0516ea96XXXXX”  
}  
],  
“client\_token”: “CrBZB1489737XXXXX”,  
“ebs\_optimized”: false,  
“groups”: [  
{  
“id”: “sg-13d8076c”,  
“name”: “launch-wizard-19”  
}  
],  
“hypervisor”: “xen”,  
“id”: “i-04452f010e4cXXXXX”,  
“image\_id”: “ami-2051XXXXX”,  
“instance\_profile”: null,  
“interfaces”: [  
{  
“id”: “eni-4c0efXXXXX”,  
“mac\_address”: “0e:d9:48:XXXXX”  
}  
],  
“kernel”: null,  
“key\_name”: “bparmaXXXXX”,  
“launch\_time”: “2017-03-22T10:23:26.000Z”,  
“monitoring\_state”: “disabled”,  
“persistent”: false,  
“placement”: {  
“tenancy”: “default”,  
“zone”: “us-east-1c”  
},  
“private\_dns\_name”: “ip-172-31-XXXXX.ec2.internal”,  
“private\_ip\_address”: “172.31XXXXX”,  
“public\_dns\_name”: “[ec2-34-207-XXXXX.compute-1.amazonaws.com](http://ec2-34-207-XXXXX.compute-1.amazonaws.com)”,  
“public\_ip\_address”: “34.207.XXXXX”,  
“ramdisk”: null,  
“region”: “us-east-1”,  
“requester\_id”: null,  
“root\_device\_type”: “ebs”,  
“source\_destination\_check”: “true”,  
“spot\_instance\_request\_id”: null,  
“state”: “running”,  
“tags”: {  
“Name”: “XXXXX”  
},  
“virtualization\_type”: “hvm”,  
“vpc\_id”: “vpc-76b6XXXXX”  
}  
],  
“invocation”: {  
“module\_args”: {  
“aws\_access\_key”: “AKIAIGKMK7E3O2RXXXXX”,  
“aws\_secret\_key”: “VALUE\_SPECIFIED\_IN\_NO\_XXXXX”,  
“ec2\_url”: null,  
“filters”: {  
“instance\_id”: “i-04452f010XXXXX”  
},  
“profile”: null,  
“region”: “us-east-1”,  
“security\_token”: null,  
“validate\_certs”: true  
}  
},  
“item”: “i-04452f010e4cXXXXX”  
}  
]  
}  
}

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2017, 9:33am UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/6 "2017-03-23T09:33:21Z")

</div>

> \*When trying to access with\_items, with following code:\*
> 
> &nbsp;&nbsp;&nbsp;- name: "Task 3: Add string instance\_id to file for each instance"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: 'printf "{{ item.volume\_id }}"'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#shell: 'printf "{{ item.volume\_id }}" \>  
> /home/ansible/playbooks/common/variables/"{{ item.item }}"'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ ec2\_snap.results }}"
> 
> \*Rather than giving proper output, as appeared in above manual access, it  
> gives garbrage as follow:\*

This is not garbage, its a verbose output of what Ansible is doing.  
I have commented the interesting part below.

> changed: [localhost] =\> (item={'\_ansible\_parsed': True, u'changed': True,  
> '\_ansible\_no\_log': False, 'item': u'i-04452f010e4cXXXXX',  
> '\_ansible\_item\_result': True, u'volume\_size': 15, u'snapshot\_id':  
> u'snap-0fa1ec99d379XXXXX', u'volume\_id': u'vol-0e0516ea9623XXXXX',  
> u'invocation': {u'module\_args': {u'aws\_secret\_key': None, u'profile': None,  
> u'aws\_access\_key': None, u'description': u'snapshot of /dev/sda1 from  
> i-04452f010e4cXXXXX taken 2017/03/22-19:58', u'security\_token': None,  
> u'snapshot\_tags': {u'source': u'/dev/sda1', u'frequency': u'daily'},  
> u'region': u'us-east-1', u'snapshot\_id': None, u'device\_name':  
> u'/dev/sda1', u'instance\_id': u'i-04452f010e4cXXXXX', u'state': u'present',  
> u'wait\_timeout': 0, u'ec2\_url': None, u'last\_snapshot\_min\_age': 0,  
> u'validate\_certs': True, u'volume\_id': None, u'wait': True}}, u'tags':  
> {u'source': u'/dev/sda1', u'frequency': u'daily'}}) =\> {  
> &nbsp;&nbsp;&nbsp;&nbsp;"changed": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;"cmd": "printf \"vol-0e0516ea9623XXXXX\"",

Here you see that ansible is running  
printf "vol-0e0516ea9623XXXXX"  
as you wanted with you loop.

> &nbsp;&nbsp;&nbsp;&nbsp;"delta": "0:00:00.002131",  
> &nbsp;&nbsp;&nbsp;&nbsp;"end": "2017-03-22 19:58:13.225366",  
> &nbsp;&nbsp;&nbsp;&nbsp;"invocation": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_args": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_raw\_params": "printf \"vol-0e0516ea962XXXXX\"",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"\_uses\_shell": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"chdir": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"creates": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"executable": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"removes": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"warn": true  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;"item": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"changed": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"invocation": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"module\_args": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"aws\_access\_key": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"aws\_secret\_key": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"description": "snapshot of /dev/sda1 from  
> i-04452f010e4cacb0b taken 2017/03/22-19:58",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"device\_name": "/dev/sda1",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"ec2\_url": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"instance\_id": "i-04452f010e4cXXXXX",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"last\_snapshot\_min\_age": 0,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"profile": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"region": "us-east-1",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"security\_token": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"snapshot\_id": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"snapshot\_tags": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"frequency": "daily",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"source": "/dev/sda1"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"state": "present",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"validate\_certs": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"volume\_id": null,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"wait": true,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"wait\_timeout": 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"item": "i-04452f010e4XXXXX",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"snapshot\_id": "snap-0fa1ec99d379XXXXX",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"tags": {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"frequency": "daily",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"source": "/dev/sda1"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"volume\_id": "vol-0e0516ea9623XXXXX",  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"volume\_size": 15  
> &nbsp;&nbsp;&nbsp;&nbsp;},  
> &nbsp;&nbsp;&nbsp;&nbsp;"rc": 0,  
> &nbsp;&nbsp;&nbsp;&nbsp;"start": "2017-03-22 19:58:13.223235",  
> &nbsp;&nbsp;&nbsp;&nbsp;"stderr": "",  
> &nbsp;&nbsp;&nbsp;&nbsp;"stderr\_lines": ,  
> &nbsp;&nbsp;&nbsp;&nbsp;"stdout": "vol-0e0516ea962XXXXX",  
> &nbsp;&nbsp;&nbsp;&nbsp;"stdout\_lines": [  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"vol-0e0516ea9623XXXXX"

The output of the command above is in stdout and stdout\_lines.  
vol-0e0516ea962XXXXX is the output of the printf command.

This is working as it should, what were you expecting?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2017, 9:44am UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/7 "2017-03-23T09:44:59Z")

</div>

> \*Same way another looping issue is as follow (actual code):\*

It's no issue here, it's working as designed.  
To understand, we need to now what you would expect.

> &nbsp;&nbsp;- name: "Task 2: Fetch EC2 details"  
> &nbsp;&nbsp;&nbsp;&nbsp;ec2\_remote\_facts:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aws\_access\_key: '{{ AwsAccesskey }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aws\_secret\_key: '{{ AwsSecretkey }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;region: '{{ region }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filters:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_id: '{{ item }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;register: ec2\_hosts  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ awsec2id }}"

The output of is registered in variable ec2\_hosts

> &nbsp;&nbsp;- debug: var=ec2\_hosts

And the output is a list, one for each item, in ec2\_hosts.results.  
So the output below is as expected.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2017, 9:48am UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/8 "2017-03-23T09:48:27Z")

</div>

The error messages says "dict object' has no attribute 'instance'"  
If you check the output of ec2\_hosts you will see it's called  
instances and not instance (missing a "s" at the end)

---

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 23, 2017, 12:44pm UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/9 "2017-03-23T12:44:49Z")

</div>

Hey Kai,

Thanks for your guidance. I am just looking for vol-0e0516ea962XXXXX as output. Same way I have to grab various parts from output, to convert them in key : value pair, to store in YAML file.

Please, can you suggest how to only get the particular part? Like we can get with following manual way?

- debug: var=ec2\_snap.results[0].invocation.module\_args.device\_name

- debug: var=ec2\_snap.results[0].item

- debug: var=ec2\_snap.results[0].volume\_id

- debug: var=ec2\_snap.results[0].snapshot\_id

- debug: var=ec2\_snap.results[1].invocation.module\_args.device\_name

- debug: var=ec2\_snap.results[1].item

- debug: var=ec2\_snap.results[1].volume\_id

- debug: var=ec2\_snap.results[1].snapshot\_id

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [March 23, 2017, 3:55pm UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/10 "2017-03-23T15:55:57Z")

</div>

I'm not sure what you mean, because your loop did that.  
The output on the screen will always contain more than just the value, but the variable itself contains just the value.

- name: Print out variables in a loop with debug module  
&nbsp;&nbsp;&nbsp;debug: msg="### The output ###: {{ item.invocation.module\_args.device\_name }} - {{ item.item }} - {{ item.volume\_id }} - {{ item.snapshot\_id }}"  
&nbsp;&nbsp;&nbsp;with\_items: "{{ ec2\_snap.results }}"

This will print the values on one line divided by dash.  
And it would be easy to identify since the line start with "### The output ###:"

---

<div class="post-metadata">

**Author:** ![Bhavin\_Parmar](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bhavin\_Parmar](https://forum.ansible.com/u/Bhavin_Parmar)\
**Post date:** [March 24, 2017, 1:41pm UTC](https://forum.ansible.com/t/how-to-create-with-items-on-a-submember/24036/11 "2017-03-24T13:41:14Z")

</div>

Kai,

Thanks for your all support. 🙂

You were right it was just displaying access details on the screen. When I started putting them in a YAML file, I was just getting what I needed.

I have created three Ansible Roles and uploaded them to [https://github.com/Bhavin-P/Ansible](https://github.com/Bhavin-P/Ansible)  
Role 1. To add, format and mount extra EBS volume to the given list of EC2 IDs.  
Role 2. To take a snapshot of root volumes, it can be scheduled cron.  
Role 3. To restore root volume, from the snapshot taken by Role 2.

Thanks to you and community as well, for providing the platform.

Thanks and regards,  
Bhavin Parmar.
