Hello all,
I am giving a list of EC2 instance IDs and mounted the volume name to take a snapshot. It is taking snapshots perfectly, but not returning snapshot IDs.
My code is:
— # Take Snapshot of EC2s
- name: Take snapshot of EC2s
hosts: local
become: no
connection: local
gather_facts: no
vars:
region: “us-east-1”
awsec2id: [“i-037b596065XXXXXXX”,“i-04452f010eXXXXXXX”]
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:
msg: ‘{{ ec2_snap.snapshot_id }}’
…
My Error is:
TASK [debug] ***********************************************************************************************************************************************************
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 ‘snapshot_id’\n\nThe error appears to have been in ‘/home/ansible/playbooks/rol1.yml’: line 25, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n with_items: ‘{{ awsec2id }}’\n - debug:\n ^ here\n”}
Usual output of registered variable (ec2_snap) is as follow:
ok: [localhost] => {
“changed”: false,
“msg”: {
“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-037b5960651891XXX taken 2017/03/22-05:13”,
“device_name”: “/dev/sda1”,
“ec2_url”: null,
“instance_id”: “i-037b5960651891XXX”,
“last_snapshot_min_age”: 0,
“profile”: null,
“region”: “us-east-1”,
“security_token”: null,
“snapshot_id”: null,
“snapshot_tags”: {},
“state”: “present”,
“validate_certs”: true,
“volume_id”: null,
“wait”: true,
“wait_timeout”: 0
}
},
“item”: “i-037b5960651891XXX”,
“snapshot_id”: “snap-080407c817cce8XXX”,
“tags”: {},
“volume_id”: “vol-09aec429dfcf28XXX”,
“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-04452f010e4cacXXX taken 2017/03/22-05:13”,
“device_name”: “/dev/sda1”,
“ec2_url”: null,
“instance_id”: “i-04452f010e4cacXXX”,
“last_snapshot_min_age”: 0,
“profile”: null,
“region”: “us-east-1”,
“security_token”: null,
“snapshot_id”: null,
“snapshot_tags”: {},
“state”: “present”,
“validate_certs”: true,
“volume_id”: null,
“wait”: true,
“wait_timeout”: 0
}
},
“item”: “i-04452f010e4cacXXX”,
“snapshot_id”: “snap-0e0cd7819920fdXXX”,
“tags”: {},
“volume_id”: “vol-0aac75ea6d2434XXX”,
“volume_size”: 15
}
]
}
}
Looking for guidance/help to perform:
I would like to store them as a key value pair (i.e. : ) in another YAML file. So I can perform a lookup, based on instance id on newly created YAML file.