JBoss EAP 6 : using variables in CLI scripts (jboss-cli.sh)
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 6.0.x
- 6.1.x
- 6.2
Issue
- User wants to use variables in JBoss 6 CLI scripts
- Failed to use variable with jboss-cli.sh
- Using a property file with jboss-cli.sh
- How to pass parameters into the CLI scripts?
- CLI is accepting
profile
name through environment variable in domain mode ?
Resolution
-
Starting in
EAP 6.1.0
one can enable client property substitution inbin/jboss-cli.xml
. -
When using
EAP 6.1.0
one must install the patch described in EAP 6.1.0 different jboss-cli behaviour with automated bash scripts. This patch is no longer needed starting withEAP 6.1.1
<!-- 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>false</resolve-parameter-values>
Change the above to
<resolve-parameter-values>true</resolve-parameter-values>
- Use the "--properties=/path/to/file.properties" to pass in the properties.
Example:
myvar=value
- The notation in the
CLI
script is then as per usual"${myvar}"
Note :
- The address, the operation name, and the parameter names can NOT include substitutions.Only the parameter values can include substitutions.
- eg : See below cli cmd :
/subsystem=datasources/xa-data-source=MyDS/statistics=pool:read-resource(include-runtime=${myvar}) << work
/subsystem=datasources/xa-data-source=${myvar}/statistics=pool:read-resource(include-runtime=true) << not work
- The first CLI cmd would work since only parameter values can include substitutions.
- In the second CLI cmd the address (/subsystem=datasources/xa-data-source=MyDS/statistics=pool), the operation name (read-resource), and the parameter names (include-runtime) can NOT include substitutions.So it would not work
Second Method :
- An alternative method (also working on pre-6.1 versions) is based on combining bash scripting with
CLI
commands. Here is an example of such a bash file and the use of bash environment variables :
#!/bin/bash
PASSWORD=secret
KEYFILE=ourKeyFileName
FQHN=host.domain.com
$JBOSS_HOME/bin/jboss-cli.sh --connect <<EOF
batch
/subsystem=web/connector=https:add(secure=true,name=https,socket-binding=https,scheme=https,protocol="HTTP/1.1")
/subsystem=web/connector=https/ssl=configuration:add(name=ssl,password="$PASSWORD",certificate-key-file="\${jboss.server.config.dir}/$KEYFILE.jks",key-alias="$FQHN")
run-batch
exit
EOF
-
In other words, when bash loads the script, all environment variables in the actual
CLI
lines will be replaced by their values before theCLI
executes the lines. -
Keep in mind that the above must be a bash script that you subsequently execute. Using the commands interactively in bash will not work correctly.
Note) IF-Else command is not allowed with batch mode
NOTE :
- Currently this environment variable substitution works for the parameters and not for the addresses. following is the general form of CLI command :
/address:command-with-parameters
-
Here, environment variable substitution will not work for the address part which comes before the colon
":"
and hence it will not substitute theprofile name
in domain mode and also there is a feature request for itPRODMGT-601
. -
However the
Second method
stated above will serve as a work around for substituting addresses in CLI.
Related Solutions
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments