How to use json_query against a output stored in a registered variable in Ansible? - ansible

I am writing an Ansible role to fetch current details of Audit settings from SQL Server through ansible.windoww.win_powershell module. In doing so, I am trying to use json_query in Ansible that has the following structure in the output but json_query is returning empty values. Please help.
This is the Ansible task:
- name: Sample Test debug:
msg: "{{list_of_audit_actions.output}}"
This variable list_of_audit_actions.output has this structure:
"msg": [
{
"HasErrors": false,
"ItemArray": [
"Test-Audit_Specification_SCM",
"AUDIT_CHANGE_GROUP",
"SUCCESS AND FAILURE",
"APPLICATION LOG"
],
"RowError": "",
"RowState": {
"String": "Detached",
"Type": "System.Data.DataRowState",
"Value": 1
},
"Table": {
"CaseSensitive": false,
"ChildRelations": "",
"Columns": "name audit_action_name audited_result type_desc",
"Constraints": "",
"Container": null,
"ContainsListCollection": false,
"DataSet": null,
"DefaultView": "",
"DesignMode": false,
"DisplayExpression": "",
"ExtendedProperties": "System.Data.PropertyCollection",
"HasErrors": false,
"IsInitialized": true,
"Locale": "en-GB",
"MinimumCapacity": 50,
"Namespace": "",
"ParentRelations": "",
"Prefix": "",
"PrimaryKey": "",
"RemotingFormat": 0,
"Rows": "",
"Site": null,
"TableName": ""
},
"audit_action_name": "AUDIT_CHANGE_GROUP",
"audited_result": "SUCCESS AND FAILURE",
"name": "Test-Audit_Specification_SCM",
"type_desc": "APPLICATION LOG"
},
{
"HasErrors": false,
"ItemArray": [
"TestAuditSpec1",
"AUDIT_CHANGE_GROUP",
"SUCCESS AND FAILURE",
"APPLICATION LOG"
],
"RowError": "",
"RowState": {
"String": "Detached",
"Type": "System.Data.DataRowState",
"Value": 1
},
"Table": {
"CaseSensitive": false,
"ChildRelations": "",
"Columns": "name audit_action_name audited_result type_desc",
"Constraints": "",
"Container": null,
"ContainsListCollection": false,
"DataSet": null,
"DefaultView": "",
"DesignMode": false,
"DisplayExpression": "",
"ExtendedProperties": "System.Data.PropertyCollection",
"HasErrors": false,
"IsInitialized": true,
"Locale": "en-GB",
"MinimumCapacity": 50,
"Namespace": "",
"ParentRelations": "",
"Prefix": "",
"PrimaryKey": "",
"RemotingFormat": 0,
"Rows": "",
"Site": null,
"TableName": ""
},
"audit_action_name": "AUDIT_CHANGE_GROUP",
"audited_result": "SUCCESS AND FAILURE",
"name": "TestAuditSpec1",
"type_desc": "APPLICATION LOG"
},
{
"HasErrors": false,
"ItemArray": [
"TestAuditSpec1",
"FAILED_LOGIN_GROUP",
"SUCCESS AND FAILURE",
"APPLICATION LOG"
],
"RowError": "",
"RowState": {
"String": "Detached",
"Type": "System.Data.DataRowState",
"Value": 1
},
"Table": {
"CaseSensitive": false,
"ChildRelations": "",
"Columns": "name audit_action_name audited_result type_desc",
"Constraints": "",
"Container": null,
"ContainsListCollection": false,
"DataSet": null,
"DefaultView": "",
"DesignMode": false,
"DisplayExpression": "",
"ExtendedProperties": "System.Data.PropertyCollection",
"HasErrors": false,
"IsInitialized": true,
"Locale": "en-GB",
"MinimumCapacity": 50,
"Namespace": "",
"ParentRelations": "",
"Prefix": "",
"PrimaryKey": "",
"RemotingFormat": 0,
"Rows": "",
"Site": null,
"TableName": ""
},
"audit_action_name": "FAILED_LOGIN_GROUP",
"audited_result": "SUCCESS AND FAILURE",
"name": "TestAuditSpec1",
"type_desc": "APPLICATION LOG"
}
]
}
How do I use json_query to filter all the values of audit_action_name in the above output?
I tried something like
- name: Sample Test
debug:
msg: "{{list_of_audit_actions.output| community.general.json_query('audit_action_name')}}"
But that does not yield anything but empty output

For building a query to use with the json_query filter, I like to use the jp command line tool to experiment with queries.
The contents of list_of_audit_actions.output is a list, so a query for audit_action_name doesn't make sense -- it's not a dictionary and does not have an audit_action_name attribute.
We want to extract the audti_action_name attribute from every item in the list, which we can do like this:
- debug:
var: list_of_audit_actions.output | json_query('[].audit_action_name')
Which will produce as output:
TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"list_of_audit_actions.output | json_query('[].audit_action_name')": [
"AUDIT_CHANGE_GROUP",
"AUDIT_CHANGE_GROUP",
"FAILED_LOGIN_GROUP"
]
}

Related

Getting a value out of registered variable in Ansible

So, I'm provisioning an EC2 instance using the ec2 module. At the end I'm using register: ec2info, so, I can later reference values inside, such as instance ID and Public DNS.
- name: Print the results
debug:
var: ec2info
Gets me
TASK [Print the results] ***********************************************************************************************************************
ok: [localhost] => {
"ec2info": {
"changed": true,
"deprecations": [
{
"collection_name": "amazon.aws",
"msg": "The 'ec2' module has been deprecated and replaced by the 'ec2_instance' module'",
"version": "4.0.0"
}
],
"failed": false,
"instance_ids": [
"i-0e9de15eb82eda3ad"
],
"instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"block_device_mapping": {
"/dev/sda1": {
"delete_on_termination": true,
"status": "attached",
"volume_id": "vol-066bb2dd4d14bdcac"
}
},
"dns_name": "ec2-35-173-126-60.compute-1.amazonaws.com",
"ebs_optimized": false,
"groups": {
"sg-0a732c6cbeb3f1025": "launch-wizard-39"
},
"hypervisor": "xen",
"id": "i-0e9de15eb82eda3ad",
"image_id": "ami-06644055bed38ebd9",
"instance_type": "t2.micro",
"kernel": null,
"key_name": "daro.io",
"launch_time": "2022-02-01T02:05:22.000Z",
"placement": "us-east-1c",
"private_dns_name": "ip-172-31-90-87.ec2.internal",
"private_ip": "172.31.90.87",
"public_dns_name": "ec2-35-173-126-60.compute-1.amazonaws.com",
"public_ip": "35.173.126.60",
"ramdisk": null,
"region": "us-east-1",
"root_device_name": "/dev/sda1",
"root_device_type": "ebs",
"state": "running",
"state_code": 16,
"tags": {
"Name": "new_demo_template"
},
"tenancy": "default",
"virtualization_type": "hvm"
}
],
"tagged_instances": []
}
}
I would like to get the value of Public DNS I have tried
- name: Print the results
debug:
var: ec2info.instances.public_dns_name
## AND
var: ec2info.instances['public_dns_name']
## AND
var: '{{ec2info.instances.public_dns_name}}'
If you could also point out to the part of the documentation that covers this?
So, I figured it out
var: ec2info.instances[0].public_dns_name

Anlible. How do I add a condition to a loop operation?

How can I get from this data only the value ("subclientName": "test") where "count" = 1
"subclientName": "start9pm" should not be in the selection
{
"countbackupsetName": {
"results": [
{
"actions": {
"xpath": "//subClientProperties[subClientEntity[#subclientName=\"test\"]]/vmContent/children",
"namespaces": {},
"state": "present"
},
"changed": false,
"count": 2,
"msg": "found 2 nodes",
"invocation": {
"module_args": {
"path": "/var/lib/awx/projects/commv/parse/get_subclient2.xml",
"xpath": "//subClientProperties[subClientEntity[#subclientName=\"test\"]]/vmContent/children",
"count": true,
"namespaces": {},
"state": "present",
"print_match": false,
"pretty_print": false,
"input_type": "yaml",
"backup": false,
"strip_cdata_tags": false,
"insertbefore": false,
"insertafter": false,
"xmlstring": null,
"value": null,
"attribute": null,
"add_children": null,
"set_children": null,
"content": null
}
},
"failed": false,
"item": {
"appName": "Virtual Server",
"backupsetName": "backupset-test",
"clientName": "name05-vcagent",
"displayName": "name05-VCAgent",
"instanceName": "VC01",
"subclientName": "test"
},
"ansible_loop_var": "item"
},
{
"actions": {
"xpath": "//subClientProperties[subClientEntity[#subclientName=\"start9pm\"]]/vmContent/children",
"namespaces": {},
"state": "present"
},
"changed": false,
"count": 2,
"msg": "found 2 nodes",
"invocation": {
"module_args": {
"path": "/var/lib/awx/projects/commv/parse/get_subclient2.xml",
"xpath": "//subClientProperties[subClientEntity[#subclientName=\"start9pm\"]]/vmContent/children",
"count": true,
"namespaces": {},
"state": "present",
"print_match": false,
"pretty_print": false,
"input_type": "yaml",
"backup": false,
"strip_cdata_tags": false,
"insertbefore": false,
"insertafter": false,
"xmlstring": null,
"value": null,
"attribute": null,
"add_children": null,
"set_children": null,
"content": null
}
},
"failed": false,
"item": {
"appName": "Virtual Server",
"backupsetName": "Test01",
"clientName": "name05-vcagent",
"displayName": "name05-VCAgent",
"instanceName": "VC01",
"subclientName": "start9pm"
},
"ansible_loop_var": "item"
}
],
"msg": "All items completed",
"changed": false
},
"_ansible_verbose_always": true,
"_ansible_no_log": false,
"changed": false
}
This is how I managed to select everything, without the condition operation:
- debug:
msg: "{{ item.subclientName }}"
loop: "{{ countbackupsetName.results|map(attribute='item')|list }}"
result:
{
"msg": "test",
"_ansible_verbose_always": true,
"_ansible_no_log": false,
"changed": false,
"item": {
"appName": "Virtual Server",
"backupsetName": "backupset-test",
"clientName": "name05-vcagent",
"displayName": "name05-VCAgent",
"instanceName": "VC01",
"subclientName": "test"
},
"ansible_loop_var": "item",
"_ansible_item_label": {
"appName": "Virtual Server",
"backupsetName": "backupset-test",
"clientName": "name05-vcagent",
"displayName": "name05-VCAgent",
"instanceName": "VC01",
"subclientName": "test"
}
}
This is how I get to the data I need, but the number of lines is always different.
I also don't need lines where count ":" 2 "
code:
- name: Show value in countbackupsetName.results.0.count
debug:
var: countbackupsetName.results.0.count
- debug:
var: countbackupsetName.results.0.item.subclientName
- name: Show value in countbackupsetName.results.1.count
debug:
var: countbackupsetName.results.1.count
- debug:
var: countbackupsetName.results.1.item.subclientName
result:
TASK [Show value in countbackupsetName.results.0.count] ************************
ok: [localhost] => {
"countbackupsetName.results.0.count": "1"
}
TASK [debug] *******************************************************************
ok: [localhost] => {
"countbackupsetName.results.0.item.subclientName": "test"
}
TASK [Show value in countbackupsetName.results.1.count] ************************
ok: [localhost] => {
"countbackupsetName.results.1.count": "2"
}
code:
- name: debug jinja
debug:
msg: |
[
{% for p in countbackupsetName.results %}
{% for o in countbackupsetName.results[p].item %}
{
"count": "{{ countbackupsetName.results[p].count }}",
"key": "{{ o }}",
"value": "{{ countbackupsetName.results[p].item[o] }}"
},
{% endfor %}
{% endfor %}
]
result:
line 112, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.
Not sure I've got properly what is desired output. But idea should be the same regardless: loop through results checking for condition and printing matching items. Here you go:
- hosts: localhost
become: false
tasks:
- set_fact:
data: {
"countbackupsetName": {
"results": [
{
"actions": {
"xpath": "//subClientProperties[subClientEntity[#subclientName=\"test\"]]/vmContent/children",
"namespaces": {},
"state": "present"
},
"changed": false,
"count": 2,
"msg": "found 2 nodes",
"invocation": {
"module_args": {
"path": "/var/lib/awx/projects/commv/parse/get_subclient2.xml",
"xpath": "//subClientProperties[subClientEntity[#subclientName=\"test\"]]/vmContent/children",
"count": true,
"namespaces": {},
"state": "present",
"print_match": false,
"pretty_print": false,
"input_type": "yaml",
"backup": false,
"strip_cdata_tags": false,
"insertbefore": false,
"insertafter": false,
"xmlstring": null,
"value": null,
"attribute": null,
"add_children": null,
"set_children": null,
"content": null
}
},
"failed": false,
"item": {
"appName": "Virtual Server",
"backupsetName": "backupset-test",
"clientName": "name05-vcagent",
"displayName": "name05-VCAgent",
"instanceName": "VC01",
"subclientName": "test"
},
"ansible_loop_var": "item"
},
{
"actions": {
"xpath": "//subClientProperties[subClientEntity[#subclientName=\"start9pm\"]]/vmContent/children",
"namespaces": {},
"state": "present"
},
"changed": false,
"count": 2,
"msg": "found 2 nodes",
"invocation": {
"module_args": {
"path": "/var/lib/awx/projects/commv/parse/get_subclient2.xml",
"xpath": "//subClientProperties[subClientEntity[#subclientName=\"start9pm\"]]/vmContent/children",
"count": true,
"namespaces": {},
"state": "present",
"print_match": false,
"pretty_print": false,
"input_type": "yaml",
"backup": false,
"strip_cdata_tags": false,
"insertbefore": false,
"insertafter": false,
"xmlstring": null,
"value": null,
"attribute": null,
"add_children": null,
"set_children": null,
"content": null
}
},
"failed": false,
"item": {
"appName": "Virtual Server",
"backupsetName": "Test01",
"clientName": "name05-vcagent",
"displayName": "name05-VCAgent",
"instanceName": "VC01",
"subclientName": "start9pm"
},
"ansible_loop_var": "item"
}
],
"msg": "All items completed",
"changed": false
},
"_ansible_verbose_always": true,
"_ansible_no_log": false,
"changed": false
}
- debug:
var: result
# check for condition in result
when: result['item']['subclientName'] == "test"
# iterate over result list
loop: "{{ data['countbackupsetName']['results'] }}"
# set loop_var to not confuse ansible's "item" var with "item" key in data
loop_control:
loop_var: result
Was able to solve the problem using json_query!
code:
- set_fact:
add_filter: "{{ countbackupsetName|json_query(query2) }}"
vars:
query2: "results[?count==`1`].item.subclientName"
- name: debug json_query(query2)
debug:
var: add_filter
result:
TASK [debug json_query(query2)] ************************************************
ok: [localhost] => {
"add_filter": [
"test"
]
}
code:
- set_fact:
add_filter: "{{ count_add_hostname.results|json_query(_query) }}"
vars:
_query: "[].{subclientName: '[?count==`1`]'.item.subclientName,
backupsetName: '[?count==`1`]'.item.backupsetName}"
- name: debug json_query(add_filter)
debug:
var: add_filter
I try to execute in a loop
result:
TASK [debug json_query(add_filter) var=add_filter] *****************************
ok: [localhost] => {
"add_filter": [
{
"backupsetName": null,
"subclientName": null
},
{
"backupsetName": null,
"subclientName": null
},
{
"backupsetName": null,
"subclientName": null
}
]
}
is the empty result related to a conflict in the loop item and the dictionary name item ...

Remove double quotes from Ansible fact

There is a json output which I am trying to parse. I registered the output into variable named instance_ip.
Here is the json output:
{
"msg": {
"instances": [
{
"root_device_type": "ebs",
"private_dns_name": "",
"cpu_options": {
"core_count": 2,
"threads_per_core": 1
},
"security_groups": [],
"state_reason": {
"message": "Client.UserInitiatedShutdown: User initiated shutdown",
"code": "Client.UserInitiatedShutdown"
},
"monitoring": {
"state": "disabled"
},
"ebs_optimized": false,
"state": {
"code": 48,
"name": "terminated"
},
"client_token": "test-Logst-14O6L4IETB05E",
"virtualization_type": "hvm",
"architecture": "x86_64",
"tags": {
"sg:environment": "TST",
"Name": "logstash1",
"aws:cloudformation:logical-id": "Logstash1A1594E87",
"sg:owner": "Platforms#paparapa.com",
"aws:cloudformation:stack-name": "test-three-ec2-instances-elk-demo",
"elastic_role": "logstash",
"sg:function": "Storage"
},
"key_name": "AWS_key",
"image_id": "ami-09f765d333a8ebb4b",
"state_transition_reason": "User initiated (2021-01-31 09:46:23 GMT)",
"hibernation_options": {
"configured": false
},
"capacity_reservation_specification": {
"capacity_reservation_preference": "open"
},
"public_dns_name": "",
"block_device_mappings": [],
"metadata_options": {
"http_endpoint": "enabled",
"state": "pending",
"http_tokens": "optional",
"http_put_response_hop_limit": 1
},
"placement": {
"group_name": "",
"tenancy": "default",
"availability_zone": "ap-southeast-2a"
},
"enclave_options": {
"enabled": false
},
"ami_launch_index": 0,
"ena_support": true,
"network_interfaces": [],
"launch_time": "2021-01-31T09:44:51+00:00",
"instance_id": "i-0fa5dbb869833d7c6",
"instance_type": "t2.medium",
"root_device_name": "/dev/xvda",
"hypervisor": "xen",
"product_codes": []
},
{
"root_device_type": "ebs",
"private_dns_name": "ip-10-x-x-x.ap-southeast-2.compute.internal",
"cpu_options": {
"core_count": 2,
"threads_per_core": 1
},
"source_dest_check": true,
"monitoring": {
"state": "disabled"
},
"subnet_id": "subnet-0d5f856afab8f0eec",
"ebs_optimized": false,
"iam_instance_profile": {
"id": "AIPARWXXVHXJWC2FL4AI6",
"arn": "arn:aws:iam::instance-profile/test-three-ec2-instances-elk-demo-Logstash1InstanceProfileC3035819-1F2LI7JM16FVM"
},
"state": {
"code": 16,
"name": "running"
},
"security_groups": [
{
"group_id": "sg-0e5dffa834a036fab",
"group_name": "Ansible_sec_group"
}
],
"client_token": "test-Logst-8UF6RX33BH06",
"virtualization_type": "hvm",
"architecture": "x86_64",
"public_ip_address": "3.x.x.x",
"tags": {
"Name": "logstash1",
"aws:cloudformation:logical-id": "Logstash1A1594E87",
"srg:environment": "TST",
"aws:cloudformation:stack-id": "arn:aws:cloudformation:ap-southeast-2:117557247443:stack/test-three-ec2-instances-elk-demo/ca8ef2b0-63ad-11eb-805f-02630ffccc8c",
"sg:function": "Storage",
"aws:cloudformation:stack-name": "test-three-ec2-instances-elk-demo",
"elastic_role": "logstash",
"sg:owner": "Platforms#paparapa.com"
},
"key_name": "AWS_SRG_key",
"image_id": "ami-09f765d333a8ebb4b",
"ena_support": true,
"hibernation_options": {
"configured": false
},
"capacity_reservation_specification": {
"capacity_reservation_preference": "open"
},
"public_dns_name": "ec2-3-x-x-x.ap-southeast-2.compute.amazonaws.com",
"block_device_mappings": [
{
"device_name": "/dev/xvda",
"ebs": {
"status": "attached",
"delete_on_termination": true,
"attach_time": "2021-01-31T10:22:21+00:00",
"volume_id": "vol-058662934ffba3a68"
}
}
],
"metadata_options": {
"http_endpoint": "enabled",
"state": "applied",
"http_tokens": "optional",
"http_put_response_hop_limit": 1
},
"placement": {
"group_name": "",
"tenancy": "default",
"availability_zone": "ap-southeast-2a"
},
"enclave_options": {
"enabled": false
},
"ami_launch_index": 0,
"hypervisor": "xen",
"network_interfaces": [
{
"status": "in-use",
"description": "",
"subnet_id": "subnet-0d5f856afab8f0eec",
"source_dest_check": true,
"interface_type": "interface",
"ipv6_addresses": [],
"network_interface_id": "eni-09b045668ac59990c",
"private_dns_name": "ip-10-x-x-x.ap-southeast-2.compute.internal",
"attachment": {
"status": "attached",
"device_index": 0,
"attachment_id": "eni-attach-0700cd11dfb27e2dc",
"delete_on_termination": true,
"attach_time": "2021-01-31T10:22:20+00:00"
},
"private_ip_addresses": [
{
"private_ip_address": "10.x.x.x",
"private_dns_name": "ip-10-x-x-x.ap-southeast-2.compute.internal",
"association": {
"public_ip": "3.x.x.x",
"public_dns_name": "ec2-3-x-x-x.ap-southeast-2.compute.amazonaws.com",
"ip_owner_id": "amazon"
},
"primary": true
}
],
"mac_address": "02:d1:13:01:59:b2",
"private_ip_address": "10.x.x.x",
"vpc_id": "vpc-0016dcdf5abe4fef0",
"groups": [
{
"group_id": "sg-0e5dffa834a036fab",
"group_name": "Ansible_sec_group"
}
],
"association": {
"public_ip": "3.x.x.x",
"public_dns_name": "ec2-3-x-x-x.ap-southeast-2.compute.amazonaws.com",
"ip_owner_id": "amazon"
},
"owner_id": "117557247443"
}
],
"launch_time": "2021-01-31T10:22:20+00:00",
"instance_id": "i-0482bb8ca1bef6006",
"instance_type": "t2.medium",
"root_device_name": "/dev/xvda",
"state_transition_reason": "",
"private_ip_address": "10.x.x.x",
"vpc_id": "vpc-0016dcdf5abe4fef0",
"product_codes": []
}
],
"failed": false,
"changed": false
},
"_ansible_verbose_always": true,
"_ansible_no_log": false,
"changed": false
}
The goal is to get the private ip address and append the port number.
With the following task I got the list with node ip address ["10.x.x.x"]
- name: Getting EC2 instance ip address
set_fact:
instance_ip: "{{ logstash_instance | json_query('instances[*].network_interfaces[*].private_ip_address') | flatten }}"
With next task in a play I am trying to append the port number but I am keep getting
"['10.x.x.x:5044']"
- name: Get everything between quotes and append port 5044
set_fact:
logstash_hosts: "{{ instance_ip | map('regex_replace', '^(.*)$', '\\1:5044') | list }}"
Here is the template output:
# ------------------------------ Logstash Output -------------------------------
output.logstash:
hosts: "['10.x.x.x:5044']"
I need to get rid of the double quotes and pass the clean variable ['10.x.x.x:5044'] to my template file.
You can try creating a new list variable with the port number appended to each element, using this approach:
- set_fact:
logstash_hosts: "{{ logstash_hosts|default([]) + [ item ~ ':5044' ] }}"
with_items: "{{ instance_ip }}"
Then in template:
output.logstash:
hosts: {{ logstash_hosts|to_yaml }}
Also since the Logstash configuration is a YAML formatted file, you use YAML list syntax and directly use the instance_ip variable (and avoid set_fact). Then the template will look like this:
output.logstash:
hosts:
{% for ip in instance_ip %}
- {{ ip }}:5044
{% endfor %}

Is it possible to combine with_dict with with_items?

I have an output of a playbook and I want to debug a message based on a condition of the output. I need the key and value pairs of the item to match a when statement then I will print out the actual data.
ok: [pynet-sw5] => {
"loop_iterate": {
"changed": false,
"msg": "All items completed",
"results": [
{
"_ansible_ignore_errors": null,
"_ansible_item_label": "1.1.1.1",
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": false,
"failed": false,
"invocation": {
"module_args": {
"count": null,
"destination": "1.1.1.1",
"dev_os": "eos",
"hostname": "arista5.twb-tech.com",
"optional_args": null,
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"ping_timeout": null,
"provider": {
"dev_os": "eos",
"hostname": "arista5.twb-tech.com",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"timeout": 60,
"username": "xxx"
},
"size": null,
"source": null,
"timeout": 60,
"ttl": null,
"username": "pyclass",
"vrf": null
}
},
"item": "1.1.1.1",
"results": {
"success": {
"packet_loss": 0,
"probes_sent": 5,
When I put the below statement it returns with item is not defined error.
- debug:
msg: "{{item[1].item}} is pingable from {{ansible_host}} with {{item[1].results.success.packet_loss}} out of 5 packets"
when: "item[0].key == 'packet_loss'"
with_sublements:
- "{{loop_iterate.results.results.success}}"
- "{{loop_iterate.results}}"
so is there a way of combining with_dict with with_items ? I want to use
- "{{loop_iterate.results.results.success}}" as with_dict iteration while using - "{{loop_iterate.results}}" as with_items

Unable to see files inside a container

I'm unable to access content in a container created using docker-compose; it's been suggested to me that this could be because the content folder on the host is not being mounted correctly. (Note: I don't know how to validate this advice, so I must assume that it's correct.)
Here's my docker-compose.yml file:
version: "2.1"
services:
docs:
image: docs/docstage
ports:
- "4000:4000"
volumes:
- "./:/usr/src/app"
Here's the output of my docker-compose command:
D:\Dev\Git\docker.github.io>docker-compose up
Creating dockergithubio_docs_1 ...
Creating dockergithubio_docs_1 ... done
Attaching to dockergithubio_docs_1
docs_1 | Configuration file: none
docs_1 | Configuration file: none
docs_1 | Source: /usr/src/app
docs_1 | Destination: /_site
docs_1 | Incremental build: disabled. Enable with --incremental
docs_1 | Generating...
docs_1 | done in 0.017 seconds.
docs_1 | Auto-regeneration: enabled for '/usr/src/app'
docs_1 | Configuration file: none
docs_1 | Server address: http://0.0.0.0:4000/
docs_1 | Server running... press ctrl-c to stop.
docs_1 | [2017-07-17 20:58:02] ERROR `/favicon.ico' not found.
...and here's the result:
C:\Users\Admin>docker exec -it 863a59969066 bash
root#863a59969066:/usr/src/app# ls
root#863a59969066:/usr/src/app#
As we can see, there's no content in the container. Also, browsing to the URL reveals an empty directory:
Here's the result of docker container inspect:
C:\Users\Admin>docker inspect dockergithubio_docs_1
[
{
"Id": "863a59969066444d0b6e908a46d0f05b68605b7fe72bfd4b0ddf2036847b0779",
"Created": "2017-07-17T20:57:06.7250794Z",
"Path": "/bin/sh",
"Args": [
"-c",
"jekyll serve -d /_site --watch -H 0.0.0.0 -P 4000"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3252,
"ExitCode": 0,
"Error": "",
"StartedAt": "2017-07-17T20:57:08.0003358Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:9670258d73f081ef2c7dd476c56fc5945627ee68867e1296fbe19e612ddd29a4",
"ResolvConfPath": "/var/lib/docker/containers/863a59969066444d0b6e908a46d0f05b68605b7fe72bfd4b0ddf2036847b0779/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/863a59969066444d0b6e908a46d0f05b68605b7fe72bfd4b0ddf2036847b0779/hostname",
"HostsPath": "/var/lib/docker/containers/863a59969066444d0b6e908a46d0f05b68605b7fe72bfd4b0ddf2036847b0779/hosts",
"LogPath": "/var/lib/docker/containers/863a59969066444d0b6e908a46d0f05b68605b7fe72bfd4b0ddf2036847b0779/863a59969066444d0b6e908a46d0f05b68605b7fe72bfd4b0ddf2036847b0779-json.log",
"Name": "/dockergithubio_docs_1",
"RestartCount": 0,
"Driver": "overlay2",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/D/Dev/Git/docker.github.io:/usr/src/app:rw"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "dockergithubio_default",
"PortBindings": {
"4000/tcp": [
{
"HostIp": "",
"HostPort": "4000"
}
]
},
"RestartPolicy": {
"Name": "",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": [],
"CapAdd": null,
"CapDrop": null,
"Dns": null,
"DnsOptions": null,
"DnsSearch": null,
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"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": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/8f7ba6861640a6fb639f64c475db0260cb4c9ded686711b05625ff37c19737fa-init/diff:/var/lib/docker/overlay2/0772e69f7faba8d149e7d9aed149d4607c905f1d01b28b97f5453772e5326904/diff:/var/lib/docker/overlay2/6d2a854de0c3c7af4e8e3b6ef831af1dde8c400f5aa8fd809d76a06f3ba5c705/diff:/var/lib/docker/overlay2/8a4466b60f60d0141625c1ad32233f3fee49821f534f8709685c2d6514b9d3f6/diff:/var/lib/docker/overlay2/6a4fe33cae424e9a671300332244aa19f5a314d90c945b399f35ea487e01d333/diff:/var/lib/docker/overlay2/de35de0b23cb93e811a7f2ec6b59e3e282faf770131179c60cad588c522551be/diff:/var/lib/docker/overlay2/e7f896a4b4d0da7ddbddd208a9130affea2358f4b1fd147f403b82fe7fe748aa/diff:/var/lib/docker/overlay2/b09694bfeb6b2e7d75de351286d95bf9af18181004f9d3c2d9bf73ea6538ba56/diff:/var/lib/docker/overlay2/4feb0e4dccefd6570fee715baf80ebe6ea77ab133cc3ac15fd850bb737f7e8b2/diff:/var/lib/docker/overlay2/1291c76b0bb03c133b70dad4dd08147f3c753b52f8ac3070d2e0f9bbdd99e874/diff:/var/lib/docker/overlay2/9166f2a32c7b3284fab5a95803ac66c83cba936161083f0405b630178f5dbeb2/diff:/var/lib/docker/overlay2/46499476944e8234be84f662104f3968f8717f3e36a67bb06d814f9c70998d9f/diff:/var/lib/docker/overlay2/fc1f9d566f52e9f994bd02dd73528fb3402a98a2618c5b3a9dbf10c8c5ae554c/diff",
"MergedDir": "/var/lib/docker/overlay2/8f7ba6861640a6fb639f64c475db0260cb4c9ded686711b05625ff37c19737fa/merged",
"UpperDir": "/var/lib/docker/overlay2/8f7ba6861640a6fb639f64c475db0260cb4c9ded686711b05625ff37c19737fa/diff",
"WorkDir": "/var/lib/docker/overlay2/8f7ba6861640a6fb639f64c475db0260cb4c9ded686711b05625ff37c19737fa/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "bind",
"Source": "/D/Dev/Git/docker.github.io",
"Destination": "/usr/src/app",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "863a59969066",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"4000/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"RUBY_MAJOR=2.3",
"RUBY_VERSION=2.3.3",
"RUBY_DOWNLOAD_SHA256=241408c8c555b258846368830a06146e4849a1d58dcaf6b14a3b6a73058115b7",
"RUBYGEMS_VERSION=2.6.8",
"BUNDLER_VERSION=1.13.6",
"GEM_HOME=/usr/local/bundle",
"BUNDLE_PATH=/usr/local/bundle",
"BUNDLE_BIN=/usr/local/bundle/bin",
"BUNDLE_SILENCE_ROOT_WARNING=1",
"BUNDLE_APP_CONFIG=/usr/local/bundle",
"NPM_CONFIG_LOGLEVEL=info",
"NODE_MAJOR_VERSION=4",
"GITHUB_GEM_VERSION=112"
],
"Cmd": [
"/bin/sh",
"-c",
"jekyll serve -d /_site --watch -H 0.0.0.0 -P 4000"
],
"ArgsEscaped": true,
"Image": "docs/docstage",
"Volumes": {
"/usr/src/app": {}
},
"WorkingDir": "/usr/src/app",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"com.docker.compose.config-hash": "f86127819d2d94cf924f8d7ef0fe8579286043aebafc2940e6ca0b1d1b4828b7",
"com.docker.compose.container-number": "1",
"com.docker.compose.oneoff": "False",
"com.docker.compose.project": "dockergithubio",
"com.docker.compose.service": "docs",
"com.docker.compose.version": "1.14.0"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "b6ad8a59f8f902f5a2fff0e4d6656bed6b3ecf1904424504886543614524f570",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"4000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "4000"
}
]
},
"SandboxKey": "/var/run/docker/netns/b6ad8a59f8f9",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"dockergithubio_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"docs",
"863a59969066"
],
"NetworkID": "8c5980632aa0810c818544573e76247a7b27f95e86d137e5f755cbff5b16b6aa",
"EndpointID": "ead13e880ebeede298f16c912d4eac0f5eb89ec5600da208202d54868273927d",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:02",
"DriverOpts": null
}
}
}
}
]
At first glance this appears OK, but I must admit to a lack of knowledge on exactly interpreting the detail.
I've opened an issue here, but it seems I've exhausted all resources on that thread.
How can I determine whether there's a mount error occurring, and—if so—how can I fix it?
You need to configure docker to share your D drive into the embedded docker VM. Without that, the VM has nothing at this location and when mounting a volume in a container to a directory that doesn't exist (inside the docker VM, not on your windows machine), you get the resulting empty directory.
See the windows install steps for how to share this drive:

Resources