Hi all, I need to get the id of all available volumes in amazon aws, to make it I have created the following
You can use json_query to create a list of just the volume IDs.
When you say “aws_volume.volumes[0]”, you’re explicitly looking at only the first volume, so your statement “… and should show me all” is incorrect; it should show you only the first volume.
Try something like this (untested):
- name: “show”
debug:
msg: “{{ item.id }}”
loop: “{{ aws_volume.volumes }}”
I understand, I could make it work, but not by using the ec2 snapshot module.
I try to save a key as a variable but the result is empty
ok: [localhost] => {
“ansible_facts”: {
“snap_id”: “”
},
“changed”: false
}
You need to first look at the registered out snapshot_info to see what its dictionary looks like. After you snapshot the EC2, insert a debug: var=snapshot_info to see what you are being given back. With that you can to then construct a proper reference to the snapshot_id.
Walter
According to the ansible docs you get a single snapshot_id.
snapshot_info.snapshot_id should get you the reference you need. It isn’t clear from the docs whether that is a list if the EC2 has multiple EBS volumes attached.
Walter
Hello! I can’t solve the problem.
This is the output
changed: [localhost] => (item=vol-xxxxxxxxxxxxxxxx) => {
“ansible_loop_var”: “item”,
“changed”: true,
“invocation”: {
“module_args”: {
“aws_access_key”: null,
“aws_ca_bundle”: null,
“aws_config”: null,
“aws_secret_key”: null,
“debug_botocore_endpoint_logs”: false,
“description”: null,
“device_name”: null,
“ec2_url”: null,
“instance_id”: null,
“last_snapshot_min_age”: 0,
“profile”: null,
“region”: null,
“security_token”: null,
“snapshot_id”: null,
“snapshot_tags”: {
“MarkedForDeletion”: true
},
“state”: “present”,
“validate_certs”: true,
“volume_id”: “vol-xxxxxxxxxxxxxxxx”,
“wait”: true,
“wait_timeout”: 900
}
},
“item”: “vol-xxxxxxxxxxxxxxxx”,
“snapshot_id”: “snap-xxxxxxxxxxxxxxxx”,
“snapshots”: [
{
“description”: “”,
“encrypted”: false,
“owner_id”: “xxxxxxxxxxxxxxxxxxx”,
“progress”: “”,
“response_metadata”: {
“http_headers”: {
“cache-control”: “no-cache, no-store”,
“content-length”: “674”,
“content-type”: “text/xml;charset=UTF-8”,
“date”: “Thu, 05 Jan 2023 14:17:16 GMT”,
“server”: “AmazonEC2”,
“strict-transport-security”: “max-age=31536000; includeSubDomains”,
“x-amzn-requestid”: “xxxxxxxxxxxxxxxxxxxx”
},
“http_status_code”: 200,
“request_id”: “xxxxxxxxxxxxxxxxxxxxxxxxx”,
“retry_attempts”: 0
},
“snapshot_id”: “snap-xxxxxxxxxxx”,
“start_time”: “2023-01-05T14:17:16.785000+00:00”,
“state”: “pending”,
“tags”: {
“MarkedForDeletion”: “True”
},
“volume_id”: “vol-xxxxxxxxxx”,
“volume_size”: 400
}
],
“tags”: {
“MarkedForDeletion”: “True”
},
“volume_id”: “vol-xxxxxxxxxxxxx”,
“volume_size”: 400
}
i see this error
TASK [Debug] **********************************************************************************************************************************************************
task path: /etc/ansible/ec2/create_ec2_instance/playbook/test.yaml:19
fatal: [localhost]: FAILED! => {
“msg”: “The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘snapshot_id’\n\nThe error appears to be in ‘/etc/ans
ible/ec2/create_ec2_instance/playbook/test.yaml’: line 19, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line app
ears to be:\n\n\n - name: "Debug"\n ^ here\n”
}
PLAY RECAP ************************************************************************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
Here my playbook:
Are you missing a parameter “snapshot_id” in your definition of “ec2_snapshot”?
Please change this:
- name: “Debug”
debug:
msg: “{{ item.snapshot_id }}”
with_items: “{{ snapshotid }}”
to this:
- name: “Debug”
debug: var=snapshotid
So you can see the entire registered result returned from the prior task.
Walter
ok: [localhost] => {
“msg”: “var=snapshotid"”
}
i think the “register” not work
I don’t know what I’m doing wrong, I’m desperate.
any help?
The register worked fine, but there’s a typo in your debug task, which you have not shown us.
i Try this tasks:
- name: “mostrando”
debug:
msg: var=show_id_snapshot
should be
- name: “mostrando”
debug: var=show_id_snapshot
or
- name: “mostrando”
debug:
var: show_id_snapshot
or
- name: “mostrando”
debug:
msg: “{{ show_id_snapshot }}”
Walter
Ok, the output shows me what I have been showing in previous answers.
Here my playbook
Change this:
- name: “mostrando”
debug: var=show_id_snapshot.snapshot_id
to
- name: “mostrando”
debug: var=show_id_snapshot
so we can see the entirety of what is registered.
Walter
registered output here https://pastebin.pl/view/b43d2333
Looks like the reference to snapshot_id should be:
set_fact:
snap_id: “{{ show_id_snapshot.results[0].snapshot_id }}”
or to get a list if there are multiples:
set_fact:
snap_id: {{ show_id_snapshot.results | json_query(‘.snapshot_id’) | list }}
Walter
Thanks. I’m ashamed I hope you can forgive me for bothering so much.
We all learn from experience and the help of others. Nothing to be ashamed about.
Walter