Extract and concatinate json nodes in ansible - jinja2

I have Ansible task, where output get registered.
It is response of elastisearch request called by curl/uri module.
An example:
{
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"content_length": "747",
"content_type": "application/json; charset=UTF-8",
"invocation": {...},
"item": "content",
"json": {
"snapshots": [
{
"duration_in_millis": 2863,
"end_time": "2016-09-25T16:00:05.357Z",
"end_time_in_millis": 1474819205357,
"failures": [],
"indices": [
"content_h_20160817_1155",
"content_s_20160817_1159",
"content_p_20160817_1158",
"content_t_20160909_0451",
"content_v_20160909_0454",
"content_h_20160909_0431",
"content_v_20160817_1201",
"content_m_20160817_1157",
"content_m_20160909_0438",
"content_p_20160909_0443",
"content_t_20160817_1200",
"content_i_20160817_1156",
"content_i_20160909_0434",
"content_s_20160909_0447"
],
"shards": {
"failed": 0,
"successful": 28,
"total": 28
},
"snapshot": "snapshot_content_20160925_1600",
"start_time": "2016-09-25T16:00:02.494Z",
"start_time_in_millis": 1474819202494,
"state": "SUCCESS",
"version": "2.3.3",
"version_id": 2030399
}
]
},
"msg": "OK (747 bytes)",
"redirected": false,
"status": 200,
"url": "url"
},
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"changed": false,
"content_length": "526",
"content_type": "application/json; charset=UTF-8",
"invocation": {...},
"item": "log",
"json": {
"snapshots": [
{
"duration_in_millis": 5002,
"end_time": "2016-09-25T16:01:24.174Z",
"end_time_in_millis": 1474819284174,
"failures": [],
"indices": [
"log_i_20160804_0749",
"log_p_20160804_0811",
"log_v_20160804_0847",
"log_m_20160804_0758",
"log_h_20160804_0746",
"log_t_20160804_0829",
"log_s_20160804_0825"
],
"shards": {
"failed": 0,
"successful": 14,
"total": 14
},
"snapshot": "snapshot_log_20160925_1600",
"start_time": "2016-09-25T16:01:19.172Z",
"start_time_in_millis": 1474819279172,
"state": "SUCCESS",
"version": "2.3.3",
"version_id": 2030399
}
]
},
"msg": "OK (526 bytes)",
"redirected": false,
"status": 200,
"url": "url"
}
]
}
Next, I need to get only indices[] from each element in result.
How can I extract indices[] array from all result[] nodes into one variable, in order to be able to use with_items later?
Thanks.
PS. I omitted invocation elements, they are too long.

You may want to read about map and sum.
Answer to your question:
curl_res.results | map(attribute='json.snapshots') | sum(start=[]) | map(attribute='indices') | sum(start=[])
You need to flatten lists with sum twice, because snapshots is a list too.

Related

How to select fields with specific value with jq

I want to select parent value where the field result is with a specific value
Here is the json i have :
{
"lde-poc-01": {
"file_|-linux-debian-login-uidmin_|-/etc/login.defs_|-replace": {
"name": "/etc/login.defs",
"changes": {},
"pchanges": {},
"result": true,
"comment": "No changes needed to be made",
"__sls__": "dev/linux/ubuntu/login",
"__run_num__": 0,
"start_time": "14:40:20.708675",
"duration": 17.447,
"__id__": "linux-debian-login-uidmin"
},
"file_|-linux-debian-login-uidmax_|-/etc/login.defs_|-replace": {
"name": "/etc/login.defs",
"changes": {},
"pchanges": {},
"result": true,
"comment": "No changes needed to be made",
"__sls__": "dev/linux/ubuntu/login",
"__run_num__": 1,
"start_time": "14:40:20.726336",
"duration": 10.614,
"__id__": "linux-debian-login-uidmax"
},
"file_|-linux-debian-login-sysuidmax_|-/etc/login.defs_|-replace": {
"name": "/etc/login.defs",
"changes": {},
"pchanges": {},
"result": true,
"comment": "No changes needed to be made",
"__sls__": "dev/linux/ubuntu/login",
"__run_num__": 2,
"start_time": "14:40:20.737165",
"duration": 10.515,
"__id__": "linux-debian-login-sysuidmax"
},
"file_|-linux-debian-login-gidmin_|-/etc/login.defs_|-replace": {
"name": "/etc/login.defs",
"changes": {},
"pchanges": {},
"result": true,
"comment": "No changes needed to be made",
"__sls__": "dev/linux/ubuntu/login",
"__run_num__": 3,
"start_time": "14:40:20.747892",
"duration": 10.201,
"__id__": "linux-debian-login-gidmin"
},
"file_|-linux-ubuntu-docker_|-/etc/apt/sources.list.d/docker.list_|-managed": {
"changes": {},
"pchanges": {},
"comment": "File /etc/apt/sources.list.d/docker.list is in the correct state",
"name": "/etc/apt/sources.list.d/docker.list",
"result": true,
"__sls__": "dev/linux/ubuntu/apt",
"__run_num__": 9,
"start_time": "14:40:20.873513",
"duration": 40.569,
"__id__": "linux-ubuntu-docker"
},
"cmd_|-linux-ubuntu-unattended-updrade-service_|-systemctl daemon-reload_|-run": {
"changes": {},
"result": false,
"duration": 0.008,
"start_time": "14:40:21.159608",
"comment": "State was not run because none of the onchanges reqs changed",
"__state_ran__": false,
"__run_num__": 20,
"__sls__": "dev/linux/ubuntu/apt"
},
"file_|-linux-ubuntu-upgrade-script-telegraf_|-/etc/telegraf/check-update.sh_|-managed": {
"changes": {},
"pchanges": {},
"comment": "File /etc/telegraf/check-update.sh is in the correct state",
"name": "/etc/telegraf/check-update.sh",
"result": true,
"__sls__": "dev/linux/ubuntu/apt",
"__run_num__": 21,
"start_time": "14:40:21.159685",
"duration": 42.313,
"__id__": "linux-ubuntu-upgrade-script-telegraf"
},
"file_|-linux-ubuntu-upgrade-telegraf_|-/etc/telegraf/telegraf.d/update.conf_|-managed": {
"changes": {},
"pchanges": {},
"comment": "File /etc/telegraf/telegraf.d/update.conf is in the correct state",
"name": "/etc/telegraf/telegraf.d/update.conf",
"result": true,
"__sls__": "dev/linux/ubuntu/apt",
"__run_num__": 22,
"start_time": "14:40:21.202254",
"duration": 51.238,
"__id__": "linux-ubuntu-upgrade-telegraf"
},
"file_|-mysoc-po-python-rm2_|-/var/tmp/mysoc-po_|-absent": {
"name": "/var/tmp/mysoc-po",
"changes": {},
"pchanges": {},
"result": false,
"comment": "File /var/tmp/mysoc-po is not present",
"__sls__": "dev/mysoc-po",
"__run_num__": 236,
"start_time": "14:40:38.161788",
"duration": 0.305,
"__id__": "mysoc-po-python-rm2"
}
}
}
I want to get the values of comment / id and the parent value like ("file_|-mysoc-po-python-rm2_|-/var/tmp/mysoc-po_|-absent") where the field result is false.
I tried to use the select function but with no success
Thanks for your help
You can use the following :
.[]
| to_entries
| map(select(.value.result == false))
| map({ name:.key, id:.value.__id__, comment:.value.comment})
map(select(criteria)) is used to discard the items of an array (or the fields of an item in this case) which do not satisfy a criteria.
to_entries is used to craft an array of key/values from an item's fields.

Extract some value from JSON using JsonPath with != condition in Jmeter

I have the following JSON and I need to get id values for instances which do not have type = Jenkins
{
"data": [
{
"id": "35002399-6fd7-40b7-b0d0-8be64e4ec09c",
"name": "94Jenkins",
"url": "http://127.0.0.1:8084",
"authProvider": false,
"siteId": "cce1b6e2-4b5d-4455-ac96-6b5d4c0d901d",
"status": {
"status": "ONLINE"
},
"instanceStateReady": true,
"instanceState": {
"#type": "InstanceStateDto",
"version": "2.60.3"
},
"adminUser": "admin1",
"hasDRConfig": false,
"managed": true,
"type": "JENKINS",
"siteName": "City",
"lastRefreshTime": "2018-04-24T09:43:01.694Z"
},
{
"id": "5cd3caf6-bac1-4f07-8793-5f124b90eaf5",
"name": "RJO",
"url": "http://test.com",
"authProvider": false,
"status": {
"status": "UNAUTHORIZED"
},
"instanceStateReady": true,
"instanceState": {
"#type": "numberOfArtifacts",
"version": "5.5.2-m002",
"licenses": {
"RJO : artrjo-m": {
"type": "ENTERPRISE",
"validThrough": "Jun 12, 2021",
"licensedTo": "Test",
"licenseHash": "asdadsdb612bda1aae745bd2a3",
"expired": false
},
"RJO : artrjo-s1": {
"type": "ENTERPRISE",
"validThrough": "Jun 12, 2021",
"licensedTo": "JFrog",
"licenseHash": "asaswca236350205a3798c0fa3",
"expired": false
}
}
},
"adminUser": "jfmc",
"hasDRConfig": false,
"managed": false,
"warnings": [
"Site is missing",
"Failed to connect to the service. Please verify that the service information provided is correct."
],
"type": "ARTIFACTORY"
},
{
"id": "0727a49a-6c95-433e-9fc5-7e5c760cc76f",
"name": "NinetyTwo",
"url": "http:127.0.0.1:8081",
"authProvider": true,
"siteId": "cce1b6e2-4b5d-4455-ac96-6b5d4c0d901d",
"status": {
"status": "ONLINE"
},
"instanceStateReady": true,
"instanceState": {
"#type": "numberOfArtifacts",
"version": "5.9.0",
"licenses": {
"NinetyTwo": {
"type": "ENTERPRISE",
"validThrough": "Dec 30, 2018",
"licensedTo": "Test",
"licenseHash": "qweqwed95f712dbabee98184da52443",
"expired": false
}
}
},
"adminUser": "admin",
"hasDRConfig": false,
"managed": true,
"type": "ARTIFACTORY",
"serviceId": "jfrt#01c7g4c7hq0dpd0qa71r8c09sj",
"siteName": "Test",
"lastRefreshTime": "2018-04-24T09:43:01.698Z"
}
]
}
And I use $..[?(#.type!='JENKINS')].id path to receive id-s which relate to the instances with type NOT JENKINS, however JSON Extractor returns me the Jenkins's id too. The question is how can I receive ids for non-jenkins instances only?
Replace your JSON path expression with the below and it should work:
$.data.[*][?(#.type != "JENKINS")].id

Returning metadata value from JSON API Response

I have an api request that returns JSON data which is then used to populate an html table. I am able to populate the table with item.name ... I would like to add the metadata value for player.field but I have not been able to access it. Here is the JSON response:
{
"list": [
{
"id": 55,
"name": "0046GS (RMS03241708)",
"description": "S 12-7 M-S 10-9",
"uuid": "6f4b5bfd-6d17-4095-9e48-7b9313f7f8c6",
"previewPlayer": false,
"enabled": true,
"mac": "00-00-00-00-00-00",
"type": "CHROMEBOX",
"distributionServer": {
"id": 2,
"name": "Main",
"driver": "IP_P2P"
},
"playergroups": [
{
"id": 2,
"name": "GameStop",
"numberOfPlayers": 454
}
],
"playerDisplays": [
{
"id": 55,
"name": "Display 1",
"channel": {
"id": 53,
"name": "GameStop TV"
},
"screenCounter": 1
}
],
"requestLogs": false,
"downloadThreads": 1,
"unusedFilesCache": 24,
"planDeliveryMethod": "CONTENT_MANAGER_DIRECT",
"pollingInterval": 1,
"pollingUnit": "MINUTES",
"logLevel": "normal",
"metadataValue": [
{
"id": 12512,
"value": "true",
"playerMetadata": {
"id": 34,
"name": "Player.field",
"datatype": "BOOLEAN",
"valueType": "ANY",
"order": 4
}
},
{
"id": 1085,
"value": "77056",
"playerMetadata": {
"id": 31,
"name": "Player.ZipCode",
"datatype": "STRING",
"valueType": "ANY",
"order": 30
}
},
{
"id": 1071,
"value": "22:15",
"playerMetadata": {
"id": 10,
"name": "Player.ScreenOff_Wednesday",
"datatype": "STRING",
"valueType": "ANY",
"order": 12
}
}
],
"usedPairingKey": "HBVULW",
"active": "ACTIVE",
"lastModified": "2017-04-03 20:12:43",
"timezoneOffset": "0"
}
],
"offset": 0,
"count": 68
}
Here is the ajax request:
$.ajax({
type: 'GET',
url: "https://sampleserver.com:8080/ContentManager/api/rest/players?limit=1&offset=0&sort=name&filters=%7BplayerStatus%20:%20%7Bvalues:%5B'ACTIVE'%5D,%20comparator%20:%20'eq'%7D%7D",
dataType: "json",
success: function(data) {
$.each(data.list, function(i, item) {
var tr = $('<tr><td>'+item.name+'</td><td>'+some.JSONResponse+'</td></tr>').appendTo('#scalaapi');
});
}
});
I am stuck on how to specifically display the value for player.field ... "value": "true",
It feels like it should be simple, but my attempts have all been met with undefined.
Attempted if statement...
success: function(data) {
$.each(data.list, function(i, item) {
var tr = $('<tr><td>'+item.name+'</td><td class="val">...</td></tr>').appendTo('#scalaapi');
var bool = function(i,item) {if (item.metadataValue.id = '12512');
tr.find('.val').text(bool);};
});
}
});

Can not filter results in vimeo api GET call with fields param

I am using Vimeo API, while making a get call to access a user's video I am doing
https://api.vimeo.com/users/61402929/videos?access_token=token
this is returning a json response
{
"total": 1,
"page": 1,
"per_page": 25,
"paging": {
"next": null,
"previous": null,
"first": "/users/61402929/videos?access_token=365879aad6244864dab70902890fc1a1&page=1",
"last": "/users/61402929/videos?access_token=365879aad6244864dab70902890fc1a1&page=1"
},
"data": [
{
"uri": "/videos/200383630",
"name": "Bhuvan bam _ Bb ki vines _ playing piano _ Saagar jaisi aankhon waali _ bhuvan bam live (360p_30fps_H264-96kbit_AAC)",
"description": "BB Ki Vines",
"link": "https://vimeo.com/200383630",
"duration": 59,
"width": 320,
"language": null,
"height": 320,
"embed": {
"uri": null,
"html": "<iframe src=\"https://player.vimeo.com/video/200383630?badge=0&autopause=0&player_id=0\" width=\"320\" height=\"320\" frameborder=\"0\" title=\"Bhuvan bam _ Bb ki vines _ playing piano _ Saagar jaisi aankhon waali _ bhuvan bam live (360p_30fps_H264-96kbit_AAC)\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>",
"buttons": {
"like": true,
"watchlater": true,
"share": true,
"embed": true,
"hd": false,
"fullscreen": true,
"scaling": true
},
"logos": {
"vimeo": true,
"custom": {
"active": false,
"link": null,
"sticky": false
}
},
"title": {
"name": "user",
"owner": "user",
"portrait": "user"
},
"playbar": true,
"volume": true,
"color": "00adef"
},
"created_time": "2017-01-20T17:57:04+00:00",
"modified_time": "2017-01-20T17:58:41+00:00",
"release_time": "2017-01-20T17:57:04+00:00",
"content_rating": [
"unrated"
],
"license": null,
"privacy": {
"view": "anybody",
"embed": "public",
"download": true,
"add": true,
"comments": "anybody"
},
"pictures": {
"uri": "/videos/200383630/pictures/613872508",
"active": true,
"type": "custom",
"sizes": [
{
"width": 100,
"height": 75,
"link": "https://i.vimeocdn.com/video/613872508_100x75.webp?r=pad",
"link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F613872508_100x75.webp&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
},
{
"width": 200,
"height": 150,
"link": "https://i.vimeocdn.com/video/613872508_200x150.webp?r=pad",
"link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F613872508_200x150.webp&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
},
{
"width": 295,
"height": 166,
"link": "https://i.vimeocdn.com/video/613872508_295x166.webp?r=pad",
"link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F613872508_295x166.webp&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
},
{
"width": 640,
"height": 640,
"link": "https://i.vimeocdn.com/video/613872508_640x640.webp?r=pad",
"link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F613872508_640x640.webp&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
},
{
"width": 960,
"height": 960,
"link": "https://i.vimeocdn.com/video/613872508_960x960.webp?r=pad",
"link_with_play_button": "https://i.vimeocdn.com/filter/overlay?src0=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F613872508_960x960.webp&src1=http%3A%2F%2Ff.vimeocdn.com%2Fp%2Fimages%2Fcrawler_play.png"
}
],
"resource_key": "fdb74e1e2dcaf7c929cfe14240765f45f2d2a302"
},
"tags": [],
"stats": {
"plays": 0
},
"metadata": {
"connections": {
"comments": {
"uri": "/videos/200383630/comments",
"options": [
"GET",
"POST"
],
"total": 0
},
"credits": {
"uri": "/videos/200383630/credits",
"options": [
"GET",
"POST"
],
"total": 1
},
"likes": {
"uri": "/videos/200383630/likes",
"options": [
"GET"
],
"total": 0
},
"pictures": {
"uri": "/videos/200383630/pictures",
"options": [
"GET",
"POST"
],
"total": 1
},
"texttracks": {
"uri": "/videos/200383630/texttracks",
"options": [
"GET",
"POST"
],
"total": 0
},
"related": null
},
"interactions": {
"watchlater": {
"added": false,
"added_time": null,
"uri": "/users/61402929/watchlater/200383630"
}
}
},
"user": {
"uri": "/users/61402929",
"name": "Rishabh Kumar",
"link": "https://vimeo.com/user61402929",
"location": null,
"bio": null,
"created_time": "2017-01-11T16:15:43+00:00",
"account": "basic",
"pictures": null,
"websites": [],
"metadata": {
"connections": {
"activities": {
"uri": "/users/61402929/activities",
"options": [
"GET"
]
},
"albums": {
"uri": "/users/61402929/albums",
"options": [
"GET"
],
"total": 0
},
"appearances": {
"uri": "/users/61402929/appearances",
"options": [
"GET"
],
"total": 0
},
"categories": {
"uri": "/users/61402929/categories",
"options": [
"GET"
],
"total": 0
},
"channels": {
"uri": "/users/61402929/channels",
"options": [
"GET"
],
"total": 0
},
"feed": {
"uri": "/users/61402929/feed",
"options": [
"GET"
]
},
"followers": {
"uri": "/users/61402929/followers",
"options": [
"GET"
],
"total": 0
},
"following": {
"uri": "/users/61402929/following",
"options": [
"GET"
],
"total": 0
},
"groups": {
"uri": "/users/61402929/groups",
"options": [
"GET"
],
"total": 0
},
"likes": {
"uri": "/users/61402929/likes",
"options": [
"GET"
],
"total": 0
},
"moderated_channels": {
"uri": "/users/61402929/channels?filter=moderated",
"options": [
"GET"
],
"total": 0
},
"portfolios": {
"uri": "/users/61402929/portfolios",
"options": [
"GET"
],
"total": 0
},
"videos": {
"uri": "/users/61402929/videos",
"options": [
"GET"
],
"total": 1
},
"watchlater": {
"uri": "/users/61402929/watchlater",
"options": [
"GET"
],
"total": 0
},
"shared": {
"uri": "/users/61402929/shared/videos",
"options": [
"GET"
],
"total": 0
},
"pictures": {
"uri": "/users/61402929/pictures",
"options": [
"GET",
"POST"
],
"total": 0
},
"watched_videos": {
"uri": "/me/watched/videos",
"options": [
"GET"
],
"total": 0
}
}
},
"preferences": {
"videos": {
"privacy": "anybody"
}
},
"content_filter": [
"language",
"drugs",
"violence",
"nudity",
"safe",
"unrated"
],
"resource_key": "6fe192b4cb782d1341fbf3fb3d0ba04a0295236d"
},
"app": null,
"status": "available",
"resource_key": "cad1f2b7d388491329363a4936f0219fa4dfd18b",
"embed_presets": null
}
]
}
however I am only interested in fields paging, total, and some fields of data array therefore i am using filter as below
https://api.vimeo.com/users/61402929/videos?access_token=token&fields=paging,data.name,data.description,data.link,data.pictures.sizes.link
but the response of above call is
{
"total": 1,
"page": 1,
"per_page": 25,
"paging": {
"next": null,
"previous": null,
"first": "/users/61402929/videos?access_token=365879********0902890fc1a1&fields=paging%2Cdata.name%2Cdata.description%2Cdata.link%2Cdata.pictures.sizes.link&page=1",
"last": "/users/61402929/videos?access_token=365879********0902890fc1a1&fields=paging%2Cdata.name%2Cdata.description%2Cdata.link%2Cdata.pictures.sizes.link&page=1"
},
"data": [
[]
]
}
Edit: Also the response for the call
https://api.vimeo.com/users/61402929/videos?access_token=token&fields=paging
is
{
"total": 1,
"page": 1,
"per_page": 25,
"paging": {
"next": null,
"previous": null,
"first": "/users/61402929/videos?access_token=365879*********70902890fc1a1&fields=paging&page=1",
"last": "/users/61402929/videos?access_token=365879*********70902890fc1a1&fields=paging&page=1"
},
"data": [
[]
]
}
I am unable to figure out why the request is not being processed correctly.
A couple of things: First, the access token should be passed in the auth header of the request, not in the request uri as you mentioned: https://developer.vimeo.com/api/authentication#making-requests
With regards to the fields filter, the filter only applies to the keys nested under data. The paging array is always returned for requests where multiple items are returned.
So your example request should look like this:
https://api.vimeo.com/users/61402929/videos?fields=name,description,link,pictures.sizes.link
More info here: https://developer.vimeo.com/api/spec#json-filter
Hope this helps!
This is not a direct answer to this question, but in case anyone else like me ends up finding this page through search results, I have wasted a couple of hours of my life with a similar problem, wondering why the results returned by Vimeo's API were seemingly completely erratic, when dialing up the API using Laravel with this package like so:
$response = Vimeo::request('/me/albums/xxxxxxx/videos?fields=name,uri,duration,width,height,link', `['per_page' => 100], 'GET');`
After a lot of trial and error I discovered that it always seemed to miss the last field listed in the query parameter.
Anyway, the solution was to always send the request with a trailing comma! i.e.
$response = Vimeo::request('/me/albums/xxxxxxx/videos?fields=name,uri,duration,width,height,link,', `['per_page' => 100], 'GET');`
Is this an absolutely face-palmingly ridiculous requirement? Why, yes. Yes it is. But there you have it.

Ansible: EC2 provisioning and Iterations

I am trying to start a bunch of EC2 instances, then install something on them based on the IP given by AWS. With only one EC2, I can add the host and proceed without any issue,but when I chain them using with_dict, I can't achieve it anymore...
The following runs as I want, but I can't understand how to deal with the registered variable ec2_infos I got from the provisioning...
- name: Create Test EC2 instances
ec2:
group: default
image: ami-40d28157
instance_type: '{{item.value.type}}'
instance_tags:
Name: "{{ tag+'-'+item.value.name }}"
key_name: privatekey
region: us-west-1
vpc_subnet_id: subnet-REDACTD
wait: yes
with_dict: '{{ec2_stack}}'
register: ec2_infos
With a dictionary like
ec2_stack:
serv1:
type: t2.micro
name: server1
serv2:
type: t2.small
name: server2
ec2_infos is structures like:
"ec2_infos": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"instance_ids": [
"i-0fewq09812ddq6"
],
"instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"block_device_mapping": {
"/dev/sda1": {
"delete_on_termination": true,
"status": "attached",
"volume_id": "vol-0987654"
}
},
"dns_name": "",
"ebs_optimized": false,
"groups": {
"sg-qdwdww": "default"
},
"hypervisor": "xen",
"id": "i-083665656521dwq6",
"image_id": "ami-40d28157",
"launch_time": "2016-11-24T20:38:53.000Z",
"placement": "us-west-1d",
"private_ip": "x.x.x.x",
"public_dns_name": "",
"public_ip": null,
"ramdisk": null,
"region": "us-east-1",
"root_device_name": "/dev/sda1",
"root_device_type": "ebs",
"state": "running",
"state_code": 16,
"tags": {
"Name": "server1",
"Team": "blah"
},
"tenancy": "default","tenancy": "default",
"virtualization_type": "hvm"
}
],
"invocation": {
"module_args": {
"assign_public_ip": false,
"exact_count": null,
"group": [
"default"
],
"group_id": null,
"id": null,
"image": "ami-40d28157",
"instance_ids": null,
"instance_initiated_shutdown_behavior": null,
"instance_profile_name": null,
"instance_tags": {
"Name": "server1",
"Team": "blah"
},
"instance_type": "t2.micro",
"kernel": null,
"volumes": null,
"vpc_subnet_id": "subnet-abcdfed",
"wait": true,
"wait_timeout": "300",
"zone": null
},
"module_name": "ec2"
},
"item": {
"key": "serv1",
"value": {
"name": "server1",
"type": "t2.micro"
}
},
"tagged_instances": []
},
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"instance_ids": [
"i-0971278624334fd"
],
"instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"block_device_mapping": {
"/dev/sda1": {
"delete_on_termination": true,
"status": "attached",
"volume_id": "vol-9999999"
}
},
"dns_name": "",
"ebs_optimized": false,
"groups": {
"sg-redactd": "default"
},
"launch_time": "2016-11-24T20:39:21.000Z",
"private_ip": "y.y.y.y",
"public_dns_name": "",
"public_ip": null,
"ramdisk": null,
"state": "running",
"state_code": 16,
"tags": {
"Name": "serv2",
"Team": "blah"
},
"tenancy": "default",
"virtualization_type": "hvm"
}
],
"invocation": {
"module_args": {
"assign_public_ip": false,
"wait_timeout": "300",
"zone": null
},
"module_name": "ec2"
},
"item": {
"key": "server2",
"value": {
"name": "serv2",
"type": "t2.small"
}
},
"tagged_instances": []
}
]
}
I tried with_items and with_subelements in different ways, but I can't manage to get every IPs of the new EC2. I don't even need to sort them just extract them from the instances part and feed them to add_host so I can proceed.
Anybody knows a clean way to do so, or would be kind enough to explain to me how to deal with a registered variable after a loop properly ?
Answer from the comments:
ec2_infos.results | map(attribute='instances') | sum(start=[]) | map(attribute='private_ip') | list