rethinkdb aggregation based on sequence items - rethinkdb

I'm currently going through the rethinkdb python tutorial.
Currently, I have 4 superheroes. In the example below, heroes is an alias for r.db("python_tutorial").table("heroes").
In[45]: list(heroes.run())
Out[44]:
[{u'appearances_count': 98,
u'hero': u'Wolverine',
u'id': u'28b6a53f-14c6-4a36-bb0b-45a6fb9c77c9',
u'magazine_titles': [u'Amazing Spider-Man vs. Wolverine',
u'Avengers',
u'X-MEN Unlimited',
u'Magneto War',
u'Prime'],
u'name': u"James 'Logan' Howlett"},
{u'aka': [u'Magnus', u'Erik Lehnsherr', u'Lehnsherr'],
u'appearances_count': 42,
u'hero': u'Magneto',
u'id': u'19274b39-f829-4daa-ba2b-24fd680e01c6',
u'magazine_titles': [u'Alpha Flight', u'Avengers', u'Avengers West Coast'],
u'name': u'Max Eisenhardt'},
{u'appearances_count': 72,
u'hero': u'Storm',
u'id': u'69848f10-2f5a-48f4-8d87-c310b88f9487',
u'magazine_titles': [u'Amazing Spider-Man vs. Wolverine',
u'Excalibur',
u'Fantastic Four',
u'Iron Fist'],
u'name': u'Ororo Monroe'},
{u'appearances_count': 72,
u'hero': u'Professor Xavier',
u'id': u'22dd3ab1-60d6-4679-9c39-2ad7da6e48d0',
u'magazine_titles': [u'Alpha Flight', u'Avengers', u'Bishop', u'Defenders'],
u'name': u'Charles Francis Xavier'}]
What I would like to do is group heroes in accordance with the magazine titles they were in. So, I'm trying to build a query, that would give something like the following:
u'Prime'
{ u'name': u"James 'Logan' Howlett"}
u'Fantastic Four'
{ u'name': u'Ororo Monroe'}
u'Excalibur'
{ u'name': u'Ororo Monroe'}
u'Defenders'
{ u'name': u'Charles Francis Xavier'}
u'Magneto War'
{ u'name': u"James 'Logan' Howlett"}
u'Bishop'
{ u'name': u'Charles Francis Xavier'}
u'Avengers West Coast'
{ u'name': u'Max Eisenhardt'}
u'Amazing Spider-Man vs. Wolverine'
{ u'name': u"James 'Logan' Howlett"}
{ u'name': u'Ororo Monroe'}
u'X-MEN Unlimited'
{ u'name': u"James 'Logan' Howlett"}
u'Alpha Flight'
{ u'name': u'Charles Francis Xavier'}
{ u'name': u'Max Eisenhardt'}
u'Avengers'
{ u'name': u"James 'Logan' Howlett"}
{ u'name': u'Charles Francis Xavier'}
{ u'name': u'Max Eisenhardt'}
u'Iron Fist'
{ u'name': u'Ororo Monroe'}
I have managed to do this through two separate queries. Here is essentially what I did:
In[46]: titles = list(heroes.concat_map(lambda hero: hero["magazine_titles"]).distinct().run())
In[47]: titles
Out[46]:
[u'Alpha Flight',
u'Amazing Spider-Man vs. Wolverine',
u'Avengers',
u'Avengers West Coast',
u'Bishop',
u'Defenders',
u'Excalibur',
u'Fantastic Four',
u'Iron Fist',
u'Magneto War',
u'Prime',
u'X-MEN Unlimited']
The above gives me a list of all the titles. Then, I merely search the database to see if the the titles are in a hero's magazine_titles. Like so:
In[48]: from collections import defaultdict
In[49]: title_data = defaultdict(list)
In[57]: for title in titles:
... title_data[title] = list(heroes.filter(lambda hero: hero["magazine_titles"].contains(title)).pluck("name").run())
In[59]: for title, heroes in title_data.items():
... pprint(title)
... pprint(heroes, indent=4)
However, I would like to do this in one query. And regarding said query, would it be more efficient than doing two separate queries like I did?

The group command with multi should do what you want:
heroes.group('magazine_titles', multi=True)['name']

Related

Removing array brackets in the ansible variable result

I have an issue about removing the array brackets in the output. I need the output without the array brackets so that i can pass the result in the rest api code. I have an example listed here below. I would like to group each set with the name and nicknames associated with that name. I was able to group the nicknames for each host but the result has brackets which I am trying to remove.
name: Set Global Vars
hosts: localhost
gather_facts: false
vars:
msg: [
{
"name": "abcdsto1",
"portnames": [
{
"nickname": "abcdsto1p1",
"port": "4353000e1ec2ec9a",
"type": "fc"
},
{
"nickname": "abcdsto1p2",
"port": "4353000e1ec2ec9b",
"type": "fc"
},
{
"nickname": "abcdsto1p3",
"port": "4353000e1ec2ec3a",
"type": "fc"
},
{
"nickname": "abcdsto1p4",
"port": "4353000e1ec2ec3b",
"type": "fc"
}
]
},
{
"name": "abcdsto2",
"portnames": [
{
"nickname": "abcdsto2p1",
"port": "4353000e1ec2ec06",
"type": "fc"
},
{
"nickname": "abcdsto2p2",
"port": "4353000e1ec2ec07",
"type": "fc"
},
{
"nickname": "abcdsto2p3",
"port": "4353000e1ec2ec64",
"type": "fc"
},
{
"nickname": "abcdsto2p4",
"port": "4353000e1ec2ec65",
"type": "fc"
}
]
},
{
"name": "abcdsto3",
"portnames": [
{
"nickname": "abcdsto3p1",
"port": "4353000e1ec43f20",
"type": "fc"
},
{
"nickname": "abcdsto3p2",
"port": "4353000e1ec43f21",
"type": "fc"
},
{
"nickname": "abcdsto3p3",
"port": "4353000e1ec43f30",
"type": "fc"
},
{
"nickname": "abcdsto3p4",
"port": "4353000e1ec43f31",
"type": "fc"
}
]
}
]
tasks:
- name: print json file
debug:
msg: "{{ item.portnames | community.general.json_query('[*].nickname') }}"
loop: "{{ msg }}"
TASK [print json file] ***************************************************************************************************************************************************************************
ok: [localhost] => (item={'name': 'abcdsto1', 'portnames': [{'nickname': 'abcdsto1p1', 'port': '4353000e1ec2ec9a', 'type': 'fc'}, {'nickname': 'abcdsto1p2', 'port': '4353000e1ec2ec9b', 'type': 'fc'}, {'nickname': 'abcdsto1p3', 'port': '4353000e1ec2ec3a', 'type': 'fc'}, {'nickname': 'abcdsto1p4', 'port': '4353000e1ec2ec3b', 'type': 'fc'}]}) => {
"msg": [
"abcdsto1p1",
"abcdsto1p2",
"abcdsto1p3",
"abcdsto1p4"
]
}
ok: [localhost] => (item={'name': 'abcdsto2', 'portnames': [{'nickname': 'abcdsto2p1', 'port': '4353000e1ec2ec06', 'type': 'fc'}, {'nickname': 'abcdsto2p2', 'port': '4353000e1ec2ec07', 'type': 'fc'}, {'nickname': 'abcdsto2p3', 'port': '4353000e1ec2ec64', 'type': 'fc'}, {'nickname': 'abcdsto2p4', 'port': '4353000e1ec2ec65', 'type': 'fc'}]}) => {
"msg": [
"abcdsto2p1",
"abcdsto2p2",
"abcdsto2p3",
"abcdsto2p4"
]
}
ok: [localhost] => (item={'name': 'abcdsto3', 'portnames': [{'nickname': 'abcdsto3p1', 'port': '4353000e1ec43f20', 'type': 'fc'}, {'nickname': 'abcdsto3p2', 'port': '4353000e1ec43f21', 'type': 'fc'}, {'nickname': 'abcdsto3p3', 'port': '4353000e1ec43f30', 'type': 'fc'}, {'nickname': 'abcdsto3p4', 'port': '4353000e1ec43f31', 'type': 'fc'}]}) => {
"msg": [
"abcdsto3p1",
"abcdsto3p2",
"abcdsto3p3",
"abcdsto3p4"
]
}
But I need the result to be
msg: abcdsto1p1,abcdsto1p2,abcdsto1p3,abcdsto1p4
msg: abcdsto2p1,abcdsto2p2,abcdsto2p3,abcdsto2p4
msg: abcdsto3p1,abcdsto3p2,abcdsto3p3,abcdsto3p4
Thanks for the help
I have faced this same issue. The join filter will work here to extract it from being pack into an array.
Before the join this is what I was using
{%- set _ = nodes.append(hostvars[server]['node_id']
this is what was being returned, even though it was coming form a valid list item.
- - 63dbc56c-8abb-a9e1-b604-71b4a7cf698e
If you then try to strip the extra hyphen using replace() filter, it will pack it into an array
| replace('-', '')
- '[''63dbc56c-8abb-a9e1-b604-71b4a7cf698e'']'
When filtering with join() it, it essentially gets casted to a string. If you have only one item in the array, then its just one string in your list.
{%- set _ = nodes.append(hostvars[server]['node_id'] | join(',') ) -%}\
The list data comes back cleaner, as expected
- 63dbc56c-8abb-a9e1-b604-71b4a7cf698e
just the same, the following will work on multiple elements inside array brackets
msg: "{{ item.portnames | community.general.json_query('[*].nickname') | join(',') }}"
will get you
msg: abcdsto1p1,abcdsto1p2,abcdsto1p3,abcdsto1p4
...
Thats a couple use cases where we want the value returned as a string only. We can filter with join()
Also thanks to #β.εηοιτ.βε for the tip.

Suppress Assert output in Ansible

I have an Ansible script that checks Linux file systems for capacity and issues a message if it is over a certain threshold.
- hosts: all
gather_facts: yes
tasks:
- name: Collect only facts about hardware
setup:
gather_subset:
- 'hardware'
- name: Test for available disk space
setup: filter=ansible_mounts
- name: Ensure that free space on the tested volume is greater than 15%
assert:
that:
- mount.size_available > mount.size_total|float * 0.05
fail_msg: Disk space on {{ item.mount }} has reached 95% threshold. Total size is {{ mount.size_total }} bytes and the available size is {{ mount.size_available }}
quiet: yes
vars:
mount: "{{ ansible_mounts | selectattr('mount','equalto',item.mount) | list | first }}"
with_items:
- "{{ ansible_mounts }}"
The problem is that even though I added quiet: yes I still get this as output on drives that are OK:
{
"changed": false,
"msg": "All assertions passed",
"_ansible_no_log": false,
"item": {
"mount": "/boot",
"device": "/dev/sda2",
"fstype": "ext4",
"options": "rw,nodev,relatime,data=ordered",
"size_total": 1023303680,
"size_available": 729440256,
"block_size": 4096,
"block_total": 249830,
"block_available": 178086,
"block_used": 71744,
"inode_total": 65536,
"inode_available": 65223,
"inode_used": 313,
"uuid": "75c887d9-e8a9-45a6-9bb8-8e7ffe4b9e68"
},
"ansible_loop_var": "item",
"_ansible_item_label": {
"mount": "/boot",
"device": "/dev/sda2",
"fstype": "ext4",
"options": "rw,nodev,relatime,data=ordered",
"size_total": 1023303680,
"size_available": 729440256,
"block_size": 4096,
"block_total": 249830,
"block_available": 178086,
"block_used": 71744,
"inode_total": 65536,
"inode_available": 65223,
"inode_used": 313,
"uuid": "75c887d9-e8a9-45a6-9bb8-8e7ffe4b9e68"
}
}
And this on the failed:
{
"evaluated_to": false,
"assertion": "mount.size_available > mount.size_total|float * 0.05",
"msg": "Disk space on /snap/core/8268 has reached 95% threshold. Size 0 Total size 93454336",
"_ansible_no_log": false,
"changed": false,
"item": {
"mount": "/snap/core/8268",
"device": "/dev/loop0",
"fstype": "squashfs",
"options": "ro,nodev,relatime",
"size_total": 93454336,
"size_available": 0,
"block_size": 131072,
"block_total": 713,
"block_available": 0,
"block_used": 713,
"inode_total": 12842,
"inode_available": 0,
"inode_used": 12842,
"uuid": "N/A"
},
"ansible_loop_var": "item",
"_ansible_item_label": {
"mount": "/snap/core/8268",
"device": "/dev/loop0",
"fstype": "squashfs",
"options": "ro,nodev,relatime",
"size_total": 93454336,
"size_available": 0,
"block_size": 131072,
"block_total": 713,
"block_available": 0,
"block_used": 713,
"inode_total": 12842,
"inode_available": 0,
"inode_used": 12842,
"uuid": "N/A"
}
}
I would like to suppress everything but the assert failed message.
The short answer is to set an explicit label in a loop_control directive, which will suppress the default behavior of outputting the entire loop variable for each loop iteration.
But you're also doing something odd in the vars section of your task.
If we fix both of those, we get:
- hosts: all
gather_facts: false
tasks:
- name: Collect only facts about hardware
setup:
gather_subset:
- hardware
- name: Test for available disk space
setup:
filter: ansible_mounts
- name: Ensure that free space on the tested volume is greater than 15%
assert:
that:
- item.size_available|float > item.size_total|float * 0.05
fail_msg: Disk space on {{ item.mount }} has reached 95% threshold. Total size is {{ item.size_total }} bytes and the available size is {{ item.size_available }}
quiet: yes
loop: "{{ ansible_mounts }}"
loop_control:
label: "{{ item.mount }}"
Which will output something like this for healthy filesystems:
ok: [host0] => (item=/)
And something like this for filesystems that fail your assert:
failed: [host1] (item=/vol/failed) => {"ansible_loop_var": "item", "assertion": "item.size_available|float > item.size_total|float * 0.05", "changed": false, "evaluated_to": false, "item": {"block_available": 0, "block_size": 131072, "block_total": 444, "block_used": 444, "device": "/dev/loop4", "fstype": "squashfs", "inode_available": 0, "inode_total": 10809, "inode_used": 10809, "mount": "/vol/failed", "options": "ro,nodev,relatime", "size_available": 0, "size_total": 58195968, "uuid": "N/A"}, "msg": "Disk space on /vol/failed has reached 95% threshold. Total size is 58195968 bytes and the available size is 0"}
Note that the contents of ansible_mounts may contain filesystems that legitimately have no free space (e.g., read-only squashfs or isofs mounts), or that don't have size_total and size_available keys. You might want to add a filter onto your task, for example:
- name: Ensure that free space on the tested volume is greater than 15%
assert:
that:
- item.size_available|float > item.size_total|float * 0.05
fail_msg: Disk space on {{ item.mount }} has reached 95% threshold. Total size is {{ item.size_total }} bytes and the available size is {{ item.size_available }}
quiet: yes
when: >
"size_total" in item and item.fstype in ['xfs', 'ext4']
loop: "{{ ansible_mounts }}"
loop_control:
label: "{{ item.mount }}"
With that filter in place, the output on my system is:
TASK [Ensure that free space on the tested volume is greater than 15%] **************************************
ok: [localhost] => (item=/)
skipping: [localhost] => (item=/var/lib/snapd/snap/mame/2287)
ok: [localhost] => (item=/boot)
skipping: [localhost] => (item=/var/lib/snapd/snap/gtk-common-themes/1514)
ok: [localhost] => (item=/home)
skipping: [localhost] => (item=/var/lib/snapd/snap/kde-frameworks-5-core18/32)
skipping: [localhost] => (item=/var/lib/snapd/snap/snapd/10707)
skipping: [localhost] => (item=/var/lib/snapd/snap/core18/1944)
skipping: [localhost] => (item=/var/lib/snapd/snap/mame/2235)
ok: [localhost] => (item=/var/lib/libvirt)
ok: [localhost] => (item=/var/lib/containers)
ok: [localhost] => (item=/var/lib/packages)
ok: [localhost] => (item=/var/lib/mock)
ok: [localhost] => (item=/vol/log)
ok: [localhost] => (item=/scratch)
ok: [localhost] => (item=/var/cache)
skipping: [localhost] => (item=/var/lib/containers/storage/overlay)

Ansible edit array of json objects in place or sort for different values when undefined

I have a list of json objects, that looks something like this
[
{
"apiVersion": "v1",
"count": 11,
"eventTime": null,
"firstTimestamp": "2020-10-20T16:17:08Z",
"lastTimestamp": "2020-10-20T16:30:38Z",
"involvedObject": {
"apiVersion": "v1"
},
"kind": "Event"
},
{
"apiVersion": "v1",
"count": 11,
"eventTime": "2020-10-20T16:17:10.182317Z"
"firstTimestamp": null,
"lastTimestamp": null,
"involvedObject": {
"apiVersion": "v1"
},
"kind": "Event"
}
]
I would like to be able to sort this array by lastTimestamp and if lastTimestamp was not defined by eventTime. Since this does not work with sort - to my knowledge at least, I was thinking about manipulating the array, setting lastTimestamp to eventTime when lastTimestamp is null.
Since I am new to Ansible, I am not sure how to manipulate a list in place in order to archive my goal. Or is there even a way to sort by two attributes?
To sumarize briefly, you don't edit in place in ansible. You manipulate the data to create a new data structure adapted to your need
In your specific case, I don't think there is any other way than using set_fact and looping over your original data to examine each item (i.e. I don't see a solution by simply applying a series of filters on the original data).
The new items in your new list will be obtained by combining the original dict with a dict containing the correct date or an empty one to keep the current date.
Here is a quick example playbook:
---
- name: Process date and sort
hosts: localhost
gather_facts: false
vars:
# Your original data as json on a single line to shorten display
api_events: [{"apiVersion": "v1", "count": 11, "eventTime": null, "firstTimestamp": "2020-10-20T16:17:08Z", "lastTimestamp": "2020-10-20T16:30:38Z", "involvedObject": {"apiVersion": "v1"}, "kind": "Event"}, {"apiVersion": "v1", "count": 11, "eventTime": "2020-10-20T16:17:10.182317Z", "firstTimestamp": null, "lastTimestamp": null, "involvedObject": {"apiVersion": "v1"}, "kind": "Event"}]
tasks:
- name: Process API events to determine time we will use
vars:
new_timestamp: "{{ item.firstTimestamp | ternary({}, {'firstTimestamp': item.eventTime}) }}"
current_event: "{{ item | combine(new_timestamp) }}"
set_fact:
processed_api_events: "{{ processed_api_events | default([]) + [current_event] }}"
loop: "{{ api_events }}"
- name: Show result sorted
debug:
msg: "{{ processed_api_events | sort(attribute='firstTimestamp') }}"
which gives:
PLAY [Process date and sort] ***********************************************************************************************************************************************************************************************************
TASK [Process API events to determine time we will use] ********************************************************************************************************************************************************************************
ok: [localhost] => (item={'apiVersion': 'v1', 'count': 11, 'eventTime': None, 'firstTimestamp': '2020-10-20T16:17:08Z', 'lastTimestamp': '2020-10-20T16:30:38Z', 'involvedObject': {'apiVersion': 'v1'}, 'kind': 'Event'})
ok: [localhost] => (item={'apiVersion': 'v1', 'count': 11, 'eventTime': '2020-10-20T16:17:10.182317Z', 'firstTimestamp': None, 'lastTimestamp': None, 'involvedObject': {'apiVersion': 'v1'}, 'kind': 'Event'})
TASK [Show result sorted] **************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
{
"apiVersion": "v1",
"count": 11,
"eventTime": null,
"firstTimestamp": "2020-10-20T16:17:08Z",
"involvedObject": {
"apiVersion": "v1"
},
"kind": "Event",
"lastTimestamp": "2020-10-20T16:30:38Z"
},
{
"apiVersion": "v1",
"count": 11,
"eventTime": "2020-10-20T16:17:10.182317Z",
"firstTimestamp": "2020-10-20T16:17:10.182317Z",
"involvedObject": {
"apiVersion": "v1"
},
"kind": "Event",
"lastTimestamp": null
}
]
}
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Ansible set_fact from dictionary based on item type

I am reading a dictionary from a Kubernetes config map (cm_config below) and am using it to replace variables set in defaults/main.yml like this:
- name: 'Overwrite defaults'
set_fact: "{{ item.key }}={{ item.value }}"
with_dict: "{{ cm_config }}"
This works fine as long as the items are simple variables. But as soon as an item is another dictionary, I'd like to combine the values.
How can I integrate this into the above task? I thought about running the loop twice, with some kind of type check. Not sure how this would work. Additionally, I believe there might be a better way?
One solution below to achieve your requirement in a single task whit just a bit of jinja2 templating and a vars lookup to get existing dict content. The key is to calculate the value based on the variable type.
Note that this does not take into account the situations when the var is a list which will be replaced as all other type of values. This will not either deal with type mismatch between existing vars and config map. e.g. if your existing var is string and the corresponding one in config map a dict it will break.
The following playbook:
---
- hosts: localhost
gather_facts: false
vars:
cm_config:
label1: toto
label2:
a_value: 1
other_value: 2
label3:
a_value: 3
other_value: 4
label4: tata
label1: I am set in play
label3:
some_value: I'm a poor lonesome cowboy
tasks:
- name: show initial state
debug:
var: "{{ item.key }}"
with_dict: "{{ cm_config }}"
- name: Process values from config map
vars:
my_value: >-
{% if item.value is mapping %}
{{ lookup('vars', item.key, default={}) | combine(item.value) }}
{% else %}
{{ item.value }}
{% endif %}
set_fact:
"{{ item.key }}": "{{ my_value }}"
with_dict: "{{ cm_config }}"
- name: Show the result after processing config map
debug:
var: "{{ item.key }}"
with_dict: "{{ cm_config }}"
gives the following result:
PLAY [localhost] ****************************************************************************************************************************************************************************************************************************
TASK [show initial state] *******************************************************************************************************************************************************************************************************************
ok: [localhost] => (item=label1) => {
"ansible_loop_var": "item",
"item": {
"key": "label1",
"value": "toto"
},
"label1": "I am set in play"
}
ok: [localhost] => (item=label2) => {
"ansible_loop_var": "item",
"item": {
"key": "label2",
"value": {
"a_value": 1,
"other_value": 2
}
},
"label2": "VARIABLE IS NOT DEFINED!"
}
ok: [localhost] => (item=label3) => {
"ansible_loop_var": "item",
"item": {
"key": "label3",
"value": {
"a_value": 3,
"other_value": 4
}
},
"label3": {
"some_value": "I'm a poor lonesome cowboy"
}
}
ok: [localhost] => (item=label4) => {
"ansible_loop_var": "item",
"item": {
"key": "label4",
"value": "tata"
},
"label4": "VARIABLE IS NOT DEFINED!"
}
TASK [Process values from config map] *******************************************************************************************************************************************************************************************************
ok: [localhost] => (item={'key': 'label1', 'value': 'toto'})
ok: [localhost] => (item={'key': 'label2', 'value': {'a_value': 1, 'other_value': 2}})
ok: [localhost] => (item={'key': 'label3', 'value': {'a_value': 3, 'other_value': 4}})
ok: [localhost] => (item={'key': 'label4', 'value': 'tata'})
TASK [Show the result after processing config map] ******************************************************************************************************************************************************************************************
ok: [localhost] => (item=label1) => {
"ansible_loop_var": "item",
"item": {
"key": "label1",
"value": "toto"
},
"label1": " toto "
}
ok: [localhost] => (item=label2) => {
"ansible_loop_var": "item",
"item": {
"key": "label2",
"value": {
"a_value": 1,
"other_value": 2
}
},
"label2": " {'a_value': 1, 'other_value': 2} "
}
ok: [localhost] => (item=label3) => {
"ansible_loop_var": "item",
"item": {
"key": "label3",
"value": {
"a_value": 3,
"other_value": 4
}
},
"label3": " {'some_value': \"I'm a poor lonesome cowboy\", 'a_value': 3, 'other_value': 4} "
}
ok: [localhost] => (item=label4) => {
"ansible_loop_var": "item",
"item": {
"key": "label4",
"value": "tata"
},
"label4": " tata "
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

how to get ec2 instances public dns name by ansible playbook

I am playing with ansible to automate ec2 instances. I created ec2 instance and then m trying to get its public dns name via ec2_remote_facts module. but it is giving variable not defined in output.
playbook:
- hosts: localhost
connection: local
tasks:
- name: ec2 instance facts
ec2_remote_facts:
region: ap-southeast-2
filters:
instance-state-name: running
register: ec2
- debug: var=ec2.instances.public_name
Output:PLAY [localhost] ***************************************************************
TASK [setup] *******************************************************************
ok: [localhost]
TASK [create ec2 instance] *****************************************************
ok: [localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"ec2.instances.public_name": "VARIABLE IS NOT DEFINED!"
}
PLAY RECAP *********************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0
Output for ec2.instances.
ok: [localhost] => {
"ec2.instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"client_token": "",
"ebs_optimized": false,
"groups": [
{
"id": "sg-6c016a08",
"name": "default"
}
],
"hypervisor": "xen",
"id": "i-915b1813",
"image_id": "ami-fedafc9d",
"instance_profile": null,
"interfaces": [
{
"id": "eni-96de4acf",
"mac_address": "0a:14:ac:64:c4:13"
}
],
"kernel": null,
"key_name": "ansible.key",
"launch_time": "2016-08-29T07:32:10.000Z",
"monitoring_state": "disabled",
"persistent": false,
"placement": {
"tenancy": "default",
"zone": "ap-southeast-2c"
},
"private_dns_name": "ip-xx-xx-xx-107.ap-southeast-2.compute.internal",
"private_ip_address": "xx.xx.xx.107",
"public_dns_name": "ec2-xx-xxx-xx-80.ap-southeast-2.compute.amazonaws.com",
"ramdisk": null,
"region": "ap-southeast-2",
"requester_id": null,
"root_device_type": "ebs",
"source_destination_check": "true",
"spot_instance_request_id": null,
"state": "running",
"tags": {
"Name": "Demo"
},
"virtualization_type": "hvm",
"vpc_id": "vpc-abcaf4ce"
}
]
}
what i m missing here?
Thanks
Benjo
If you look closely to ec2.instances you may note that:
it is a list, so you either access items by index ec2.instances[0] or iterate over them with with_items: structure.
there is no public_name attribute, only public_dns_name.

Resources