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
}
]
}
}