intersight-ansible
intersight-ansible copied to clipboard
intersight_rest_api Module - $filter Query Parameter formatting problem
I have been testing API calls to Intersight using the intersight_rest_api
module, and I have encountered a problem with the $filter
Query Parameter, specifically when using the in
Operator. According to the API documentation, the $filter
Query Parameter can filter results based on the presence of a field value in a list of possible values, like this:
$filter=Name in ('hostname1', 'hostname2', 'hostname3')
I have tested this syntax using the Intersight API Docs Swagger UI and it definitely works as expected. Using the asset/Targets
API endpoint, I can provide a list of possible hostnames in the format above, contained in parenthesis with each element surrounded by single quotes, and the API response will contain only those Targets included in my search query.
When attempting this using the Intersight Ansible Collection, I'm finding that only the first search element is recognized and passed to the API Endpoint - I only get one search result back from each API call. I have tried several methods of formatting this $filter
string, including escaping the single quotes and the parenthesis, but I have been unable to get a successful result from the module.
Environment Information:
- Python v3.12.0
- Ansible v2.16.3
- Intersight Collection v2.0.7
Example Task:
- name: Call Intersight
cisco.intersight.intersight_rest_api:
api_private_key: "{{ api_private_key }}"
api_key_id: "{{ api_key_id }}"
api_uri: "{{ api_uri | default(omit) }}"
resource_path: /asset/Targets
query_params:
$filter: "Name in ('Pod1-DNACenter','Pod2-DNACenter','Pod2-Services','Pod1-Services')"
$select: "Moid,TargetId,Name,Tags"
api_body: {}
register: serial_asset_target_result
Response:
ok: [localhost] => {
"api_response": {
"ClassId": "asset.Target",
"Moid": "<omitted>",
"Name": "Pod1-DNACenter",
"ObjectType": "asset.Target",
"Tags": [],
"TargetId": [
"<omitted>"
]
},
"changed": false,
"invocation": {
"module_args": {
"api_body": {},
"api_key_id": "<omitted>",
"api_private_key": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"api_uri": "https://<omitted>.intersight.com/api/v1",
"list_body": null,
"query_params": {
"$filter": "Name in ('Pod1-DNACenter','Pod2-DNACenter','Pod2-Services','Pod1-Services')",
"$select": "Moid,TargetId,Name,Tags"
},
"resource_path": "/asset/Targets",
"return_list": false,
"state": "present",
"update_method": "patch",
"use_proxy": true,
"validate_certs": true
}
},
"trace_id": "<omitted>"
}
Example Response from API Docs Swagger UI:
{
"ObjectType": "asset.Target.List",
"Results": [
{
"ClassId": "asset.Target",
"Moid": "<omitted>",
"Name": "Pod1-DNACenter",
"ObjectType": "asset.Target",
"Tags": [],
"TargetId": [
"<omitted>"
]
},
{
"ClassId": "asset.Target",
"Moid": "<omitted>",
"Name": "Pod1-Services",
"ObjectType": "asset.Target",
"Tags": [],
"TargetId": [
"<omitted>"
]
},
{
"ClassId": "asset.Target",
"Moid": "<omitted>",
"Name": "Pod2-DNACenter",
"ObjectType": "asset.Target",
"Tags": [],
"TargetId": [
"<omitted>"
]
},
{
"ClassId": "asset.Target",
"Moid": "<omitted>",
"Name": "Pod2-Services",
"ObjectType": "asset.Target",
"Tags": [],
"TargetId": [
"<omitted>"
]
}
]
}
I don't know where exactly the problem is occurring, but it seems like a string formatting issue in the Collection code itself, or some quirk in Ansible. Please look into this and determine if it can be fixed or a workaround put in place. Thank you!