Sharing configuration

Sharing configuration information can be done in several ways between attributes, instances, containers and environments.

Below are the different scenarios explained.

Sharing and reusing files

File scope

Attribute “File scope” lets you choose where a file should be stored.

The following table shows the possible locations:

File scope Description
Current instance Default. The file is stored with the current instance only
Current container The file is stored at container level where it can be used by other scripts. Useful for environment variables and data that should be shared with all instances on the current container. Alternative can be the "Container" scope for attributes.
Current environment The file is stored at environment level where it can be used by other scripts. Useful for environment variables and data that should be shared with all instances on the current environment. Alternative can be the "Environment" scope if only a few attributes need to be shared. An example would be a JSON-file with the full configuration of Vault, a AWS VPC network, ...
Every instance The file will be generated as if it existed in every packet used in the environment. Every instance will get its own version of this file unless 'Skip "Every instance" files' is checked, which can be useful for configuration items that exist many times and that have no actions. File "init_instance.sh" is provided by default and it sources environment- and container-level files. It also handles the module-dependencies of the instance. Be very careful when using this option because it can have an impact on script generation performance in environments with many instances.
Every container Same as "Current Container", but the file will also be stored at the container-level for the other containers in the environment. Useful when a container with such an instance is shared with other environments.

Including files

You can include the contents of a file in other files using attribute “Include name”.

Usage

Write some Velocity macros and share them.

Example

Specify “include_me” for the attribute “Include name” on a file. Then enter the following anywhere in other files where you want to include the contents of this file:

<INCLUDE include_me>

Using files from other instances

It can be useful to execute scripts from other instances or to collect files.

Usage

  • Retrieve files from instances in the child-tree to use them as a group for Terraform, Ansible, Puppet, Chef, …

Example

The following example is from the Terraform Builder packet. It has Velocity parsing enabled and retrieves all instances underneath it in the instance tree that have at least one Terraform file.

For each instance, it executes “init.sh” if that file exists. In this case “init.sh”-scripts are used to retrieve AMIs for autoscaling groups and EC2 instances.

#foreach ($terraformInstance in $instance.getInstancesByFileExtension(".tf"))
dir="$terraformInstance.getRelativePath()";

cd ../../../${D}dir;
if [ -f "init.sh" ]; then
    log_info "Sourcing init.sh in ${D}dir";
    . ./init.sh;
fi;

log_info 'Copying .tf and .tpl files from "$terraformInstance.toString()"';
instance_id="$terraformInstance.getId()";
#[[
for f in $(find . -maxdepth 1 -type f -name \*.tf); do
    f="$(basename "$f")" # remove ./
    log_info "Copying $f as ${instance_id}_$f";
    cp $f /tmp/${instance_id}_$f;
done;

for f in $(find . -maxdepth 1 -type f -name \*.tpl); do
    f="$(basename "$f")" # remove ./
    log_info "Copying $f from ../../../$dir";
    cp $f /tmp/;
done;
]]#
#end

Files in modules

Modules can be used by any packet and so can the files within them.

Reusing attributes

Attribute scope

The following table lists the possible attribute scopes:

Scope Description
Instance Default. The attribute is available in the current instance. It's value is automatically stored in an environment variable with the same name when running actions of the instance.

Use it in Velocity through $instance.getAttribute("<attribute_name>") or $someOtherInstance.getAttribute("<attribute_name>")

Container The attribute is available to all instances on the container of the instance. Any action under the container can retrieve the value of this attribute through an environment variable with the same name.

Use it in Velocity through $container.getContainerVarValue("<attribute_name>")or $container.getContainerVarValue("<attribute_name>")

Environment The attribute is available to all instances on the container of the instance. Any action under the container can retrieve the value of this attribute through an environment variable with the same name.

Use it in Velocity through $environment.getContainerVarValue("<attribute_name>")

Instance tree

Any attribute or file can use attributes from any instance in the environment.

There are many ways to retrieve instances. See Apache Velocity for all available methods.

Usage

This is the most frequently used method of retrieving data from the environment.

Example

Retrieve the hostname of the parent Jenkins-instance (which can be up multiple level in the instance tree:

$instance.parent.getAttribute("jenkins_hostname")