How to use variables in WildFly CLI

In this short tutorial we will learn how to use variables in WildFly / JBoss Command Line Interface (CLI) showing some examples and hacks to get the most of it.

Declaring Variables in the CLI

First things first: WildFly CLI has a set function that you can use to assign a certain path of the server model to a variable. Example:

[standalone@localhost:9990 /] set s1=/host=master/server=server-one

This is quite useful in domain mode as you can include references to host and profiles in variables so that you can easily replicate scripts on different servers. Example:

[standalone@localhost:9990 /] $s1/subsystem=datasources/data-source=ExampleDS:test-connection-in-pool

You can also set multiple variables at once by using a space as separator. Example:

[standalone@localhost:9990 /] set var1=value1 var2=value2

To get the list of variables available in the current CLI Session, just use the set command:

[standalone@localhost:9990 /] set
var1=value1
var2=value2

Here is a nice hack. If you are running the CLI from within a bash script:, you can bundle the variables within the Bash script as in this example:

#!/bin/bash

PASSWORD=password
KEYFILE=myFileName
FQHN=mydomain.com

$JBOSS_HOME/bin/jboss-cli.sh --connect <<EOF

  batch

       /subsystem=web/connector=https:add(secure=true,name=https,socketbinding=https,scheme=https,protocol="HTTP/1.1")

       /subsystem=web/connector=https/ssl=configuration:add(name=ssl,password="$PASSWORD",certificate-keyfile="${jboss.server.config.dir}/$KEYFILE.jks",key-alias="$FQHN")

  run-batch

  exit

EOF

How to make CLI variables persistent

By default, all variables set within the CLI session will be lost at the end of the session. Luckily the .jbossclirc file comes to rescue us. Firstly, include the variables in this file (available in the “bin” folder of your server installation):

# .jbossclirc
set s1=/host=master/server=server-one
set s2=/host=master/server=server-two

Next restart the CLI and issue a set command to check the available variables:

[domain@localhost:9990 /] set
s1=/host=master/server=server-one
s2=/host=master/server=server-two

Using variables to set attributes

You can also use variables to set values of your configuration. Let’s see an example:

[standalone@localhost:9990 /] set MYVAR=foo
[standalone@localhost:9990 /] /system-property=test:add(value="${MYVAR}")

However, the above command fails with:

{
    "outcome" => "failed",
    "failure-description" => "WFLYCTL0211: Cannot resolve expression '${MYVAR}'",
    "rolled-back" => true
}

That’s because you need to use a different format in your CLI to set values with variables. You need to include a colon (“:”) when setting attributes. That also allows setting a default value. The following will work:

/system-property=test:add(value="${MYVAR:}")

Finally, here is how to allow a default value for your variable:

/system-property=test:add(value="${MYVAR:default}")

Using environment variables in the CLI

Within the application server configuration, it is common to use this Beanshell expression to refer to environment variables:

${env.VARNAME}

However, if you try to use the above expression with the CLI, you won’t get the actual value of the environment variable:

[standalone@localhost:9990 /] echo ${env.MODULEPATH}
${env.MODULEPATH}

To allow it, edit the jboss-cli.,xml and set the attribute resolve-parameter-values to true:

<!-- whether to resolve system properties specified as command argument or operation parameter values
                 in the CLI VM before sending the operation requests to the controller -->
   <resolve-parameter-values>true</resolve-parameter-values>

With that in place, restart the CLI and you will be able to reference the environment variable:

[standalone@localhost:9990 /] echo ${env.MODULEPATH}
/etc/scl/modulefiles:/etc/scl/modulefiles:/usr/share/Modules/modulefiles:/etc/modulefiles:/usr/share/modulefiles

How to create aliases for commands

Finally, we will discuss about using aliases in the CLI. WildFly CLI can use aliases pretty much the same you would do it in a Linux Box:

[domain@localhost:9990 /] alias ll='ls'
[domain@localhost:9990 /] ll
core-service                    management-client-content       domain-organization=undefined   name=Unnamed Domain             release-version=2.0.10.Final    
deployment                      path                            launch-type=DOMAIN              namespaces=[]                   schema-locations=[]             
deployment-overlay              profile                         local-host-name=master          process-type=Domain Controller  
extension                       server-group                    management-major-version=4      product-name=WildFly Full       
host                            socket-binding-group            management-micro-version=0      product-version=10.0.0.Final    
interface                       system-property                 management-minor-version=0      release-codename=Kenny

As for variables, the alias created in a CLI session are not persistent. Since the CLI is derived from the Aesh project (http://aeshell.github.io/) , aliases can be defined in the .aesh_aliases within the user’s home folder (Not WildFly home!).

Let’s test it. We will add the following alias in $HOME/.aesh_aliases

alias create='/subsystem=ejb3/strict-max-bean-instance-pool=demo:add(max-pool-size=10)'

Now start the CLI and test it:

[francesco@localhost bin]$ ./jboss-cli.sh -c
[standalone@localhost:9990 /] alias
alias create='/subsystem=ejb3/strict-max-bean-instance-pool=demo:add(max-pool-size=10)'
[standalone@localhost:9990 /] create
{"outcome" => "success"}

You could be also interested in the following tutorial:  Using properties in CLI scripts