WildFly CLI (Command Line Interface) allows you to manage and configure WildFly application server instances. One of its powerful features is expansion expressions, which enable you to work with multiple resources efficiently. In this tutorial we will learn how to perform multiple operation commands using the expansion operator (*) when possible and by iterating over WildFly resources.
What Are Expansion Expressions?
Expansion expressions are used in WildFly CLI to reference and manipulate multiple resources at once. They use the *
wildcard to select resources based on a pattern. For example, data-source=*
refers to all data sources.
Using Wildcards in WildFly CLI
- Reading Resources: You can use the
*
operator to read multiple resources. This is useful when you want to list or query information about resources that match a pattern.
/subsystem=datasources/data-source=*:read-resource(recursive=true)
- Modifying Multiple Resources: Unfortunately, you cannot use the
*
wildcard to modify or update multiple resources in a single command. For example:
In some cases, however, using an expansion operator can be helpful. For example, if you have to set an operation on hundred of resources. Common cases are indeed datasources in Domain mode or Logging profiles. Let’s see in the following example how to circumvent this issue with an iteration over the list of datasources.
Iteration over resources using the CLI
One of the best kept-secrets of the CLI is that you can easily iterate over the resources of your subsystem. To do that, firstly change your current position in the Model. Then, you can loop over a child resource and execute commands over it.
For example, the following command, loops over the data-source
child resource and executes a write-attribute
of the max-pool-size
.
cd /subsystem=datasources for ds in :read-children-names(child-type=data-source) /subsystem=datasources/data-source=$ds:write-attribute(name=max-pool-size,value=10) done
Make sure that the variable you are using in the loop is not defined outside of the loop otherwise it will throw an error. See this article to learn more about variables in the CLI: How to use variables in WildFly CLI
Also, the scope of the loop variable ds
is private to the inner loop.
4. Example Bash Script for Looping and setting Resources
Finally, if you want to execute a command on all resources of a certain type inside a shell script, then you can also use the standard bash for operator. This allows to export the result of the commands if you need to use them in a bash script.
Prerequisites:
- WildFly CLI installed and accessible at
/home/jboss/wildfly-32.0.0.Final/bin/jboss-cli.sh
Here is the bash script:
#!/bin/bash # Define the CLI path CLI="/home/jboss/wildfly-32.0.0.Final/bin/jboss-cli.sh" # Get the list of all data sources RESOURCES=$($CLI -c --commands="cd /subsystem=datasources/data-source, ls") # Read the resource names into an array read -r -a nodes <<< "$RESOURCES" # Iterate over each data source and update the max-pool-size attribute for node in "${nodes[@]}"; do $CLI -c --commands="/subsystem=datasources/data-source=$node:write-attribute(name=max-pool-size,value=10)" done
Conclusion
By understanding and using expansion expressions correctly, you can effectively manage and configure your WildFly application server. The provided scripts demonstrates a practical approach to automating the configuration of multiple data sources using WildFly CL