I want to disable a service if `var.sshkey` is not...
# 🤖|ask-kapa
f
@microscopic-river-74022 I want to disable a service if
var.sshkey
is not set. How?
m
Please stand by @fresh-yak-35965 as I search the knowledge base. I normally take about 20 seconds or so. :wave:
---------------------- In Garden, you can disable a service by setting the
disabled
configuration key to
true
. You can use conditional template strings to enable or disable services based on certain conditions. In your case, you want to disable a service if
var.sshkey
is not set. You can achieve this by using a conditional template string like so:
Copy code
yaml
services:
  - name: my-service
    disabled: ${var.sshkey == undefined}
In this example,
my-service
will be disabled if
var.sshkey
is not set. Please note that disabling a service means that it will not be deployed, and will also be ignored if it is declared as a runtime dependency for another service, test or task. Also, template strings referencing the service's outputs will fail to resolve when the service is disabled, so you need to make sure to provide alternate values for those if you're using them, using conditional expressions. For more information, you can refer to the Garden documentation on exec services (), container services (), and jib-container services (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
f
@microscopic-river-74022 That acutally doesn't work. It complains about being unable to find the variable:
Copy code
Invalid template string (${var.sshkey == undefined || var.cluster == undefined}?) at path disabled: Could not find key sshkey under var. Available keys: apphost, baseHostName and mode. Could not find key undefined. Available keys: command, datetime, environment, git, inputs, local, parent, project, secrets, template, this, var and variables.
m
Let me check the knowledge base for you. One moment! :books:
---------------------- I apologize for the confusion. The error message indicates that the variable
sshkey
is not found under
var
. This means that
sshkey
is not defined in your Garden configuration. In Garden, variables are defined in the
variables
field in your project configuration or in a
varfile
. If
sshkey
is not defined, you will get an error when you try to reference it. To fix this, you need to ensure that
sshkey
is defined in your Garden configuration. Here is an example of how to define variables in your project configuration:
Copy code
yaml
kind: Project
name: my-project
variables:
  sshkey: your-ssh-key
In this example,
sshkey
is defined and set to
your-ssh-key
. You can replace
your-ssh-key
with your actual SSH key. Once
sshkey
is defined, you should be able to use it in your conditional template string. However, please note that Garden does not currently support checking if a variable is undefined directly in the template string. You might need to ensure that all variables are defined, possibly with a default value, before using them in a template string. For more information, you can refer to the Garden documentation on variables (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted
f
@microscopic-river-74022 Is it possible to set defaults for variables which can be overwritten by using
--var
in the garden command?
m
Let me check the knowledge base for you. One moment! :books:
---------------------- Yes, it is possible to set default values for variables in your Garden configuration, which can then be overridden by using the
--var
flag in the Garden command. You can define default values for your variables in the
variables
field in your project configuration. Here is an example:
Copy code
yaml
kind: Project
name: my-project
variables:
  my-variable: default-value
In this example,
my-variable
is set to
default-value
by default. You can then override this default value by using the
--var
flag in the Garden command. Here is an example:
Copy code
bash
garden run my-task --var my-variable=custom-value
In this example,
my-variable
is set to
custom-value
, overriding the default value. Please note that variables set with
--var
CLI flags have the highest precedence, meaning they will override any values set in your project configuration or in a varfile. For more information, you can refer to the Garden documentation on variables () and CLI commands (). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
message has been deleted