Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to define multiple assign statements? #226

Closed
log1-c opened this issue Nov 6, 2023 · 7 comments
Closed

[Question] How to define multiple assign statements? #226

log1-c opened this issue Nov 6, 2023 · 7 comments
Labels
question Further information is requested

Comments

@log1-c
Copy link
Contributor

log1-c commented Nov 6, 2023

Question

Hello :)
First of all, thanks for the great module!

I'd like to ask how to define multiple assign statements so that I end up with something like this:
image
assign where host.vars.hostgroup == "msd" || host.vars.tags.customer == "msd"

I have tried the following:
assign_filter: "host.vars.hostgroup=%22{{ var_pb_customer_token }}%22%7Chost.vars.tags.customer=%22{{ var_pb_customer_token }}%22" (also tried with a literal |, or and ||)
which leads to
/** Failed to render this object **/ /* Got invalid JSON in filter string: host.vars.hostgroup="q1au2"|host.vars.tags.customer="q1au2" */

I have also tried the part from the config preview
host.vars.hostgroup == "msd" || host.vars.tags.customer == "msd"
this also leads to /** Failed to render this object **/ /* Got invalid JSON in filter string:

A single condition like assign_filter: "host.vars.hostgroup=%22{{ var_pb_customer_token }}%22" works without problems.

If it makes a difference I'm trying to create a host group.

🙋‍♂️

@log1-c log1-c added the question Further information is requested label Nov 6, 2023
@rndmh3ro
Copy link
Collaborator

rndmh3ro commented Nov 6, 2023

I have locally defined this assign_filter:

assign_filter: host.name="{{ "*customer-prod*" }}" | host.name="customer-azure"

This results in:

> curl  -H "Accept: application/json" https://example.com/director/hostGroup?uuid=821b02b9-874c-49c4-981b-219000000006
{
    "assign_filter": "host.name=\"*customer-prod*\" | host.name=\"customer-azure\"",
    "object_name": "customer-prod",
    "object_type": "object"
}

image

Can you try this?

@log1-c
Copy link
Contributor Author

log1-c commented Nov 6, 2023

Well, that is kinda confusing what the Director does here...

Having multiple conditions does work like this

director/hostgroup?name=q1poc -X POST -d '{"assign_filter": "host.vars.hostgroup=\"q1poc\"|host.vars.tags.customer=\"q1poc\"", "object_type": "object", "object_name": "q1poc"}'

image

{
    "assign_filter": "host.vars.hostgroup=\"q1poc\"|host.vars.tags.customer=\"q1poc\"",
    "object_name": "q1poc",
    "object_type": "object"
}

but not with URL-encoding

/director/hostgroup?name=q1poc -X POST -d '{"assign_filter": "host.vars.hostgroup=%22q1poc%22%7Chost.vars.tags.customer=%22q1poc%22", "object_type": "object", "object_name": "q1poc"}'

image

{
    "assign_filter": "host.vars.hostgroup=%22q1poc%22%7Chost.vars.tags.customer=%22q1poc%22",
    "object_name": "q1poc",
    "object_type": "object"
}

while with a single condition it doesn't care about the URL encoding

/director/hostgroup?name=q1poc -X POST -d '{"assign_filter": "host.vars.hostgroup=%22q1poc%22", "object_type": "object", "object_name": "q1poc"}'

image

{
    "assign_filter": "host.vars.hostgroup=%22q1poc%22",
    "object_name": "q1poc",
    "object_type": "object",
    "uuid": "5cd672fb-71e4-4c9d-93ae-da57c5491976"
}

Will try this now with Ansible and the module. If that works without problems as well (which I expect), I'll close the issue.
Thanks for pointing me in the right direction!

@rndmh3ro
Copy link
Collaborator

rndmh3ro commented Nov 6, 2023

Interesting... Though the director-api often has these little quirks..

@log1-c
Copy link
Contributor Author

log1-c commented Nov 7, 2023

As expected it works via the Ansible module as well, when using the above mentioned first format of the condition.
Small request: Such an "advanced" filter should also be mentioned in the docs/examples. It was a bit of a journey getting to the point at which I knew what format the module expected.

@log1-c log1-c closed this as completed Nov 7, 2023
@rndmh3ro rndmh3ro reopened this Nov 10, 2023
@rndmh3ro
Copy link
Collaborator

Such an "advanced" filter should also be mentioned in the docs/examples

Good idea!

do you want to create a PR to add an example here?

You can check the Contributing Guidelines if you need help.

@log1-c
Copy link
Contributor Author

log1-c commented Nov 13, 2023

Done :)
#227

Also added a section to the general read-me.

@rndmh3ro
Copy link
Collaborator

fixed in #227

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants