Network IP addresses with jq bash command from docker inspect - json

I want to process docker inspect <container1> <container2> out to retrieve via jq command exposed ports and IPAddresses assigned to defined networks. So far I've got jq '{(.[].Name): getpath(path(.[].NetworkSettings.Networks)) | .[].IPAddress}' which is clearly far from enough.
p.s. I know its easy to write a script that would give such output but I'm curious how to do that using jq
Expected output
{
# .[].Name
"/prometheus_prometheus_1": [
[
# .[].Config.ExposedPorts
"9090/tcp"
],
{
# key = .[].NetworkSettings.Networks.<index>
# value = .[].NetworkSettings.Networks[].IPAddress
"prometheus_internal": "172.21.0.4",
"system-information": "172.28.0.2"
}
]
}
Input:
[
{
"Id": "c2f86bce68441f50f973ae89a835d052e74c083e35b5438f3cec2497bcac79fa",
"Created": "2018-06-12T05:33:19.811448302Z",
"Path": "/bin/prometheus",
"Args": [
"--config.file=/etc/prometheus/prometheus.yml",
"--storage.tsdb.path=/prometheus",
"--web.console.libraries=/usr/share/prometheus/console_libraries",
"--web.console.templates=/usr/share/prometheus/consoles"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 13217,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-06-12T05:33:20.264845701Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:44a4c1c270dcb5e8caf51e3205fa23343cc903f3823eab53ae204f304d16f62a",
"ResolvConfPath": "/var/lib/docker/containers/c2f86bce68441f50f973ae89a835d052e74c083e35b5438f3cec2497bcac79fa/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/c2f86bce68441f50f973ae89a835d052e74c083e35b5438f3cec2497bcac79fa/hostname",
"HostsPath": "/var/lib/docker/containers/c2f86bce68441f50f973ae89a835d052e74c083e35b5438f3cec2497bcac79fa/hosts",
"LogPath": "/var/lib/docker/containers/c2f86bce68441f50f973ae89a835d052e74c083e35b5438f3cec2497bcac79fa/c2f86bce68441f50f973ae89a835d052e74c083e35b5438f3cec2497bcac79fa-json.log",
"Name": "/prometheus_prometheus_1",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"prometheus_prometheus-data:/prometheus:rw",
"/home/sebastian/projects/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:rw"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "prometheus_internal",
"PortBindings": {},
"RestartPolicy": {
"Name": "always",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": [],
"CapAdd": null,
"CapDrop": null,
"Dns": null,
"DnsOptions": null,
"DnsSearch": null,
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "shareable",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": null,
"DeviceCgroupRules": null,
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/73c16b0735eb15494741e5b821a9df3be8cebb716219ac206d9ee0042e4166ac-init/diff:/var/lib/docker/overlay2/03c66042305488fea2fe693246e90374743fbb7836ab31e4ee5cdbfbb1c97785/diff:/var/lib/docker/overlay2/80821a00b79334493729e671dfb66cc878aec19fe2d0dfbaf4cde187522b06e8/diff:/var/lib/docker/overlay2/f5071cce5513fb3c465ad883297157271ef48c6f59cd88768068224d850c6903/diff:/var/lib/docker/overlay2/c9a0cc617abb45fbd4572abb41c7281c96bc9d59c109270c7f1ec7a99f5b055b/diff:/var/lib/docker/overlay2/5b425f8a440139a8fdcdd7f7fee8189565e9e00304e96f4cfee86ff6b861e068/diff:/var/lib/docker/overlay2/e85740d997591d2f60bfa61ea0c82a58636bccaaba970e55f12cf0ff24565c70/diff:/var/lib/docker/overlay2/e9c362d971c460b02111122b241888392d77c5b8545ed520c3d74660b16db9b2/diff:/var/lib/docker/overlay2/5001184fc8ddd25204626f419c1530b970cb37f936a196fb1173f2774310085e/diff:/var/lib/docker/overlay2/ed2e676f09a4583f2194e752083a7c95be80131f055891dc39f9fed5560f5a73/diff",
"MergedDir": "/var/lib/docker/overlay2/73c16b0735eb15494741e5b821a9df3be8cebb716219ac206d9ee0042e4166ac/merged",
"UpperDir": "/var/lib/docker/overlay2/73c16b0735eb15494741e5b821a9df3be8cebb716219ac206d9ee0042e4166ac/diff",
"WorkDir": "/var/lib/docker/overlay2/73c16b0735eb15494741e5b821a9df3be8cebb716219ac206d9ee0042e4166ac/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "prometheus_prometheus-data",
"Source": "/var/lib/docker/volumes/prometheus_prometheus-data/_data",
"Destination": "/prometheus",
"Driver": "local",
"Mode": "rw",
"RW": true,
"Propagation": ""
},
{
"Type": "bind",
"Source": "/home/sebastian/projects/prometheus/prometheus.yml",
"Destination": "/etc/prometheus/prometheus.yml",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "c2f86bce6844",
"Domainname": "",
"User": "nobody",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"9090/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"--config.file=/etc/prometheus/prometheus.yml",
"--storage.tsdb.path=/prometheus",
"--web.console.libraries=/usr/share/prometheus/console_libraries",
"--web.console.templates=/usr/share/prometheus/consoles"
],
"Image": "prom/prometheus:v2.3.0",
"Volumes": {
"/etc/prometheus/prometheus.yml": {},
"/prometheus": {}
},
"WorkingDir": "/prometheus",
"Entrypoint": [
"/bin/prometheus"
],
"OnBuild": null,
"Labels": {
"com.docker.compose.config-hash": "b8ff39fd239739ae66051752c1b49b0119862b8f21678fb42400c907d25b1c57",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "prometheus",
"com.docker.compose.service": "prometheus",
"com.docker.compose.version": "1.16.1",
"maintainer": "The Prometheus Authors <prometheus-developers#googlegroups.com>"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "7be77954d0e6990f565a8526b157236140238cabf0954629598ce3bb9aaae885",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"9090/tcp": null
},
"SandboxKey": "/var/run/docker/netns/7be77954d0e6",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"prometheus_internal": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"prometheus",
"c2f86bce6844"
],
"NetworkID": "145c59986c16003f9a3a30e6b671c34b56225d90243fba88fc295c49b4dcfff6",
"EndpointID": "8e3ac8bd4719ea85358c20a8141c37377dd327265916d41ee5d1db6b7496898c",
"Gateway": "172.21.0.1",
"IPAddress": "172.21.0.4",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:15:00:04",
"DriverOpts": null
},
"system-information": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"prometheus",
"c2f86bce6844"
],
"NetworkID": "f6e67721cb73da7e8d4eacdb709de240014af9af35e66216e6186300c72de330",
"EndpointID": "2c31369f8175f2b38a5bc966a8306c8f0f11ac4c264e6ce735d00c87b8a6f44f",
"Gateway": "172.28.0.1",
"IPAddress": "172.28.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:1c:00:02",
"DriverOpts": null
}
}
}
}
]

jq solution:
jq '.[] | {(.Name) : [(.Config.ExposedPorts | keys),
(.NetworkSettings.Networks | to_entries
| map({(.key): .value.IPAddress}) | add)
]
}' input
The output:
{
"/prometheus_prometheus_1": [
[
"9090/tcp"
],
{
"prometheus_internal": "172.21.0.4",
"system-information": "172.28.0.2"
}
]
}

Related

Azure Congnitive Service Skill not persisting custom skill value

I have create a custom skill for my Azure Cognitive Service. After creating the datasouce, index, indexer and running the indexer I can see that my function is being called and it is outputting the correct information, but when I search the index the field that is connected to the custom skill is empty.
This is my indexer definition:
{
"name": "idxdocs",
"description": null,
"dataSourceName": "dsdocs",
"skillsetName": "skillset-procuradores",
"targetIndexName": "customer-documents",
"disabled": null,
"schedule": null,
"parameters": {
"batchSize": null,
"maxFailedItems": -1,
"maxFailedItemsPerBatch": null,
"base64EncodeKeys": null,
"configuration": {
"indexedFileNameExtensions": ".pdf,.docx,.doc",
"dataToExtract": "contentAndMetadata",
"failOnUnprocessableDocument": false,
"failOnUnsupportedContentType": false,
"indexStorageMetadataOnlyForOversizedDocuments": true,
"allowSkillsetToReadFileData": true
}
},
"fieldMappings": [],
"outputFieldMappings": [
{
"sourceFieldName": "/document/content/procuradores",
"targetFieldName": "procuradores"
}
],
"cache": null,
"encryptionKey": null
}
This is my index:
{
"name": "customer-documents",
"fields": [
{
"name": "key",
"type": "Edm.String",
"facetable": true,
"filterable": true,
"key": true,
"retrievable": true,
"searchable": false,
"sortable": true,
"analyzer": null,
"indexAnalyzer": null,
"searchAnalyzer": null,
"synonymMaps": [],
"fields": []
},
{
"name": "content",
"type": "Edm.String",
"facetable": false,
"filterable": false,
"key": false,
"retrievable": true,
"searchable": true,
"sortable": false,
"analyzer": "es.lucene",
"indexAnalyzer": null,
"searchAnalyzer": null,
"synonymMaps": [],
"fields": []
},
{
"name": "procuradores",
"type": "Edm.String",
"facetable": false,
"filterable": true,
"key": false,
"retrievable": true,
"searchable": false,
"sortable": false,
"analyzer": null,
"indexAnalyzer": null,
"searchAnalyzer": null,
"synonymMaps": [],
"fields": []
}
],
"suggesters": [],
"scoringProfiles": [],
"defaultScoringProfile": null,
"corsOptions": null,
"analyzers": [],
"charFilters": [],
"tokenFilters": [],
"tokenizers": [],
"similarity": {
"#odata.type": "#Microsoft.Azure.Search.BM25Similarity",
"k1": null,
"b": null
},
"encryptionKey": null,
"#odata.etag": "\"0x8D98BC85E9F6996\""
}
My skill set definition:
{
"name": "skillset-procuradores",
"description": "",
"skills": [
{
"#odata.type": "#Microsoft.Skills.Custom.WebApiSkill",
"name": "procuradores",
"description": "",
"context": "/document",
"uri": "my url ommited for secure reasons",
"httpMethod": "POST",
"timeout": "PT3M50S",
"batchSize": 1,
"degreeOfParallelism": null,
"inputs": [
{
"name": "text",
"source": "/document/content"
}
],
"outputs": [
{
"name": "procuradores",
"targetName": "procuradores"
}
],
"httpHeaders": {}
}
],
"cognitiveServices": null,
"knowledgeStore": null,
"encryptionKey": null
}
And finally my function output:
{
"values": [
{
"recordId": "1",
"data": {
"procuradores": "people's names"
}
}
]
}
What am I missing?
Rafael, can you give debug sessions a try? I suspect there is something happening in your custom skill that means the data is not coming back. By using this, you can test the actual input and output.
https://learn.microsoft.com/en-us/azure/search/cognitive-search-debug-session
This is just a hunch, but in the indexer definition, try changing your output field mapping sourceFieldName from "/document/content/procuradores" to "/document/procuradores". The context that you are giving in the skill is just "/document" so it should append the output onto that.

question about extracting variables from json

I have bellow json output:
I have to get "holders"("VG_ROOT-var", "VG_ROOT-root", "VG_ROOT-tmp")
from all sd* drives *
tries to use something like this in ansible:
"gather_host_facts_result.ansible_facts.ansible_devices.sd*"
unfortunately at this stage I already have a problem. Could someone help me?
how to specify "*" in the query to go to the rest of the structure?
{
"msg": [
{
"dm-0": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-VG_ROOT-root",
"dm-uuid-LVM-YMwN1WVoYYOwj8ZRJ5ZBdpMSlPTiiOAOgCughz5IwAC91ILXxeQEkJU4NnhDgfGf"
],
"labels": [],
"masters": [],
"uuids": [
"97c2a8ff-e21f-4869-9b0f-bbba523dc487"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "123723776",
"sectorsize": "512",
"size": "59.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-1": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-VG_DATA-data",
"dm-uuid-LVM-htvEwSfXRtyxN1j7V4A2BSdeKJGBTLqiPBERZVCP8ZOr4uMWSmopoedAQvSZ4gXJ"
],
"labels": [],
"masters": [],
"uuids": [
"4c4c89ab-df3f-4647-aa37-c04c9175b804"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "62906368",
"sectorsize": "512",
"size": "30.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-2": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-VG_SWAP-swap",
"dm-uuid-LVM-SHCF191xu9CcjovlxavjqUXI1yBmVdLTxbd24kHS8fkcAlDkbMcXlAlsY7m0soMF"
],
"labels": [],
"masters": [],
"uuids": [
"97999edf-ccb5-45c8-87c6-23d2a5771146"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "20963328",
"sectorsize": "512",
"size": "10.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-3": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-VG_ROOT-tmp",
"dm-uuid-LVM-YMwN1WVoYYOwj8ZRJ5ZBdpMSlPTiiOAOqId09qrO6vwn9z7306ZkWhRyiXS4d1jy"
],
"labels": [],
"masters": [],
"uuids": [
"325d6523-4e1f-4352-ab39-1e7259b613c5"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "20971520",
"sectorsize": "512",
"size": "10.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"dm-4": {
"holders": [],
"host": "",
"links": {
"ids": [
"dm-name-VG_ROOT-var",
"dm-uuid-LVM-YMwN1WVoYYOwj8ZRJ5ZBdpMSlPTiiOAOTz6mFIEVsHJN4B0hOAV0RlVdmm293Z0L"
],
"labels": [],
"masters": [],
"uuids": [
"41f5cd6a-fa50-4ac3-a14d-693d23a3df87"
]
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "",
"sectors": "20971520",
"sectorsize": "512",
"size": "10.00 GB",
"support_discard": "0",
"vendor": null,
"virtual": 1
},
"sda": {
"holders": [],
"host": "Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 02)",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": "Virtual disk",
"partitions": {
"sda1": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": [
"3C14-A798"
]
},
"sectors": "2097152",
"sectorsize": 512,
"size": "1.00 GB",
"start": "2048",
"uuid": "3C14-A798"
},
"sda2": {
"holders": [
"VG_ROOT-var",
"VG_ROOT-root",
"VG_ROOT-tmp"
],
"links": {
"ids": [
"lvm-pv-uuid-9Mm0hj-z0vg-WEhd-7MyB-xcp1-mEI3-XZENN8"
],
"labels": [],
"masters": [
"dm-0",
"dm-3",
"dm-4"
],
"uuids": []
},
"sectors": "165672927",
"sectorsize": 512,
"size": "79.00 GB",
"start": "2099200",
"uuid": null
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "bfq",
"sectors": "167772160",
"sectorsize": "512",
"size": "80.00 GB",
"support_discard": "0",
"vendor": "VMware",
"virtual": 1
},
"sdb": {
"holders": [
"VG_SWAP-swap"
],
"host": "Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 02)",
"links": {
"ids": [
"lvm-pv-uuid-m2YPQe-ofBp-AjUa-qAfQ-Bcy6-xTpj-WyNFU1"
],
"labels": [],
"masters": [
"dm-2"
],
"uuids": []
},
"model": "Virtual disk",
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "bfq",
"sectors": "20971520",
"sectorsize": "512",
"size": "10.00 GB",
"support_discard": "0",
"vendor": "VMware",
"virtual": 1
},
"sdc": {
"holders": [
"VG_DATA-data"
],
"host": "Serial Attached SCSI controller: VMware PVSCSI SCSI Controller (rev 02)",
"links": {
"ids": [
"lvm-pv-uuid-X47VTO-03iG-WVw4-ww8p-PH0v-hlGj-wU1t7F"
],
"labels": [],
"masters": [
"dm-1"
],
"uuids": []
},
"model": "Virtual disk",
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "bfq",
"sectors": "62914560",
"sectorsize": "512",
"size": "30.00 GB",
"support_discard": "0",
"vendor": "VMware",
"virtual": 1
},
"sr0": {
"holders": [],
"host": "IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)",
"links": {
"ids": [
"ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001"
],
"labels": [],
"masters": [],
"uuids": []
},
"model": "VMware IDE CDR10",
"partitions": {},
"removable": "1",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "bfq",
"sectors": "2097151",
"sectorsize": "512",
"size": "1024.00 MB",
"support_discard": "0",
"vendor": "NECVMWar",
"virtual": 1
}
}
]
}
I created something like this:
- name: disks
hosts: localhost
become: false
gather_facts: false
tasks:
- name: hardware
setup:
gather_subset:
- hardware
register: gather_host_facts_result
- name: The output
debug:
msg:
- "{{ gather_host_facts_result.devices | dict2items | selectattr('key', 'match', '^sd.*') | map(attribute='value.holders') | flatten }}"
But this dosen't work.
I'd like to pull all holders f.e VG ROOT-var, VG _ROOT-rootVG _ROOT-tmp from all sd * disks.
The output should look like this:
sda2:
"VG_ROOT-var",
"VG_ROOT-root",
"VG_ROOT-tmp"
sdc
"VG_DATA-data"
sdb
"VG_SWAP-swap"

Filtering output to needed values in json

I have below output generated as part of gathering all attributes about openstack VM. I am trying to work a json filter, which would let me select just values of interest. (tenant name, tenant ID or cloud name and so on).
I am new to filters, so all the help is appreciated. I tried different ways but not close to what i want.
N_ID": true,
"NAME_ATTR": "name",
"OS-DCF:diskConfig": "AUTO",
"OS-EXT-AZ:availability_zone": "cloud-b",
"OS-EXT-STS:power_state": 1,
"OS-EXT-STS:task_state": null,
"OS-EXT-STS:vm_state": "active",
"OS-SRV-USG:launched_at": "2018-06-05T22:45:50.000000",
"OS-SRV-USG:terminated_at": null,
"accessIPv4": "192.168.0.1",
"accessIPv6": "",
"addresses": {
"tenant-internal-direct-net": [
{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:73:c6:9e",
"OS-EXT-IPS:type": "fixed",
"addr": "192.168.0.1",
"version": 4
}
]
},
"az": "cloud-1-b",
"cloud": "cloud-1",
"config_drive": "",
"created": "2018-06-05T22:45:37Z",
"flavor": {
"id": "15",
"name": "2vCPUx8GB"
},
"hostId": "f12acb7a6a4050672a7710da4ccb5987916ac46648fc667496c4a169",
"human_id": "myhost-ost1",
"id": "ae12d6cd-c9b8-4908-a0f9-6af614f01197",
"image": {
"id": ""
},
"interface_ip": "192.168.0.1",
"key_name": "pz-rhel7",
"metadata": {},
"name": "myhost-ost1",
"networks": {
"tenant-internal-direct-net": [
"192.168.0.1"
]
},
"os-extended-volumes:volumes_attached": [
{
"id": "179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1"
}
],
"private_v4": "",
"progress": 0,
"public_v4": "192.168.0.1",
"public_v6": "",
"region": "cloud-1",
"request_ids": [],
"security_groups": [
{
"description": "",
"id": "55991c66-beb5-4750-b8f6-06025a1ce7d4",
"name": "SSHping",
"security_group_rules": [
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "53fa8fca-d54c-4462-9f5e-a748fed86885",
"port_range_max": null,
"port_range_min": null,
"protocol": "icmp",
"remote_ip_prefix": "0.0.0.0/0",
"security_group_id": "55991c66-beb5-4750-b8f6-06025a1ce7d4"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "841446f5-a7bf-46e9-91c2-f505c5842bc6",
"port_range_max": 22,
"port_range_min": 22,
"protocol": "tcp",
"remote_ip_prefix": "0.0.0.0/0",
"security_group_id": "55991c66-beb5-4750-b8f6-06025a1ce7d4"
}
]
},
{
"description": "Default security group",
"id": "987efb61-d7f3-47d5-9362-42f66d1b104b",
"name": "default",
"security_group_rules": [
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "79da1cc2-2128-4937-95ca-d0fc757bbfb3",
"port_range_max": 22,
"port_range_min": 22,
"protocol": "tcp",
"remote_ip_prefix": "0.0.0.0/0",
"security_group_id": "987efb61-d7f3-47d5-9362-42f66d1b104b"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "901d230e-9cd5-486c-aa89-c279b359fac9",
"port_range_max": 1,
"port_range_min": 1,
"protocol": "tcp",
"remote_ip_prefix": "0.0.0.0/0",
"security_group_id": "987efb61-d7f3-47d5-9362-42f66d1b104b"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "950c6b8c-e129-4138-8dd5-92254c5bd6c8",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_ip_prefix": null,
"security_group_id": "987efb61-d7f3-47d5-9362-42f66d1b104b"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "cf20d16d-0b8a-4bd7-b6b4-cab6d5968a44",
"port_range_max": null,
"port_range_min": null,
"protocol": null,
"remote_ip_prefix": null,
"security_group_id": "987efb61-d7f3-47d5-9362-42f66d1b104b"
}
]
}
],
"status": "ACTIVE",
"tenant_id": "99f1f837a8004092926887d46188e726",
"updated": "2018-06-05T22:45:50Z",
"user_id": "35f6e9300037f9c6d23f08c5af8b324c4ab23688d2a146bd6aac601377ed72ef",
"volumes": [
{
"HUMAN_ID": false,
"NAME_ATTR": "name",
"attachments": [
{
"attached_at": "2018-06-05T22:45:43.000000",
"attachment_id": "4602ad6b-8652-4702-910b-e4e82a894a32",
"device": "/dev/vda",
"host_name": null,
"id": "179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1",
"server_id": "ae12d6cd-c9b8-4908-a0f9-6af614f01197",
"volume_id": "179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1"
}
],
"availability_zone": "nova",
"bootable": true,
"consistencygroup_id": null,
"created_at": "2018-06-05T22:45:39.000000",
"description": "",
"device": "/dev/vda",
"display_description": "",
"display_name": "",
"encrypted": false,
"human_id": null,
"id": "179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1",
"links": [
{
"href": "http://cloud-1.mycloud.com:8776/v2/99f1f837a8004092926887d46188e726/volumes/179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1",
"rel": "self"
},
{
"href": "http://cloud-1.mycloud.com:8776/99f1f837a8004092926887d46188e726/volumes/179749a7-ba9b-4cc1-aebd-dcc8dcb7f4e1",
"rel": "bookmark"
}
],
"metadata": {
"attached_mode": "rw",
"readonly": "False"
},
"multiattach": false,
"name": "",
"os-vol-tenant-attr:tenant_id": "99f1f837a8004092926887d46188e726",
"replication_status": "disabled",
"request_ids": [],
"size": 11,
"snapshot_id": null,
"source_volid": null,
"status": "in-use",
"updated_at": "2018-06-05T22:45:43.000000",
"user_id": "35f6e9300037f9c6d23f08c5af8b324c4ab23688d2a146bd6aac601377ed72ef",
"volume_image_metadata": {
"checksum": "2d3f40fa841b1c35c10e74207d1221f8",
"container_format": "bare",
"disk_format": "raw",
"hw_disk_bus": "scsi",
"hw_qemu_guest_agent": "yes",
"hw_scsi_model": "virtio-scsi",
"hw_vif_multiqueue_enabled": "true",
"hw_watchdog_action": "reset",
"image_id": "fe463cc5-fecf-4aad-a77e-70987f421169",
"image_name": "RHEL-7_4-MG",
"min_disk": "0",
"min_ram": "0",
"size": "10737418240"
},
"volume_type": "ceph-standard",
"x_openstack_request_ids": []
}
],
"x_openstack_request_ids": []
}

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

How to access a mysql database that is into a Docker continer?

I'm working in a windows environment and I have my MySQL database into a Docker container, but how I should access from an external application?
Right now I'm just want to use a regular Java Class to test the connection but what address should I use? I've tried with the boot2docker ip, but that doesn't work. I've also tried with the ip that I got with the docker inspect command but I haven't got any result.
[
{
"Id": "ee02d1e463056c8dcd878fa7d9746c39b4377e051d11a8853be274b206b73c16",
"Created": "2015-08-11T16:07:44.597618319Z",
"Path": "/entrypoint.sh",
"Args": [
"mysqld"
],
"State": {
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2167,
"ExitCode": 0,
"Error": "",
"StartedAt": "2015-08-11T16:38:38.256276665Z",
"FinishedAt": "2015-08-11T16:38:08.508576127Z"
},
"Image": "a128139aadf282a10ec937a60010f837fa1b91dd367e05c7ca6197a84b3115b3",
"NetworkSettings": {
"Bridge": "",
"EndpointID": "a6e0a25891c065dc47b9dccb19c89e0ab7ee358a2b7cf947f6385bb47b99bac4",
"Gateway": "172.17.42.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"HairpinMode": false,
"IPAddress": "172.17.0.5",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:05",
"NetworkID": "5c8e21999516c552412027ab316d3a8d8d813952fb207c1de9ec38040895fc50",
"PortMapping": null,
"Ports": {
"3306/tcp": null
},
"SandboxKey": "/var/run/docker/netns/ee02d1e46305",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null
},
"ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/ee02d1e463056c8dcd878fa7d9746c39b4377e051d11a8853be274b206b73c16/resolv.conf",
"HostnamePath": "/mnt/sda1/var/lib/docker/containers/ee02d1e463056c8dcd878fa7d9746c39b4377e051d11a8853be274b206b73c16/hostname",
"HostsPath": "/mnt/sda1/var/lib/docker/containers/ee02d1e463056c8dcd878fa7d9746c39b4377e051d11a8853be274b206b73c16/hosts",
"LogPath": "/mnt/sda1/var/lib/docker/containers/ee02d1e463056c8dcd878fa7d9746c39b4377e051d11a8853be274b206b73c16/ee02d1e463056c8dcd878fa7d9746c39b4377e051d11a8853be274b206b73c16-json.log",
"Name": "/fluxtream",
"RestartCount": 0,
"Driver": "aufs",
"ExecDriver": "native-0.2",
"MountLabel": "",
"ProcessLabel": "",
"Volumes": {
"/var/lib/mysql": "/mnt/sda1/var/lib/docker/volumes/5a05900d9551d22235e51a7d03a7354156fce1bdea3e7378f52df8d33fee1f4f/_data"
},
"VolumesRW": {
"/var/lib/mysql": true
},
"AppArmorProfile": "",
"ExecIDs": [
"8fd70881d9cfed795dfb6850d513e54490432fd1e55e8bf8f6040f71cc1e7a4c",
"d3ca3e35de0222127c7e5da3e6ab45157cb06f5716c0c2bbfdc9a7096230786d",
"b75620585c02840a62169bc5adf0a5a80fa265194ac74136c54dc31cf9edd653",
"a61e680448ad796b1ad113776c5ba46f3b8a03f6601d843b005722748c9d5669"
],
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LxcConf": [],
"Memory": 0,
"MemorySwap": 0,
"CpuShares": 0,
"CpuPeriod": 0,
"CpusetCpus": "",
"CpusetMems": "",
"CpuQuota": 0,
"BlkioWeight": 0,
"OomKillDisable": false,
"Privileged": false,
"PortBindings": {},
"Links": null,
"PublishAllPorts": false,
"Dns": null,
"DnsSearch": null,
"ExtraHosts": null,
"VolumesFrom": null,
"Devices": [],
"NetworkMode": "bridge",
"IpcMode": "",
"PidMode": "",
"UTSMode": "",
"CapAdd": null,
"CapDrop": null,
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"SecurityOpt": null,
"ReadonlyRootfs": false,
"Ulimits": null,
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"CgroupParent": ""
},
"Config": {
"Hostname": "ee02d1e46305",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"PortSpecs": null,
"ExposedPorts": {
"3306/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"MYSQL_ROOT_PASSWORD=fluxtream",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"MYSQL_MAJOR=5.6",
"MYSQL_VERSION=5.6.26"
],
"Cmd": [
"mysqld"
],
"Image": "mysql:5.6.26",
"Volumes": {
"/var/lib/mysql": {}
},
"VolumeDriver": "",
"WorkingDir": "",
"Entrypoint": [
"/entrypoint.sh"
],
"NetworkDisabled": false,
"MacAddress": "",
"OnBuild": null,
"Labels": {}
}
}
]
My dev enviroment:
Windows 7 Pro
Docker 1.7.0
Boot2Docker-cli version 1.7.0
MySQL container 5.6.26
What worked for my is mapping the port when I created the container.
docker run --name YOUR_CONTAINER -p 3306:3306 -e MYSQL_ROOT_PASSWORD=YOUR_PASS -d mysql:5.6.26