Sending a pipeline parameter to a bash or Ansible script is easy, but the reverse is less obvious.

In this article, we’ll look at how to return parameters from an Ansible script to a Cloudbees CD/RO job.

The easy way

You can easily transfer a parameter from the Cloudbees CD/RO procedure to an Ansible script. Simply pass the parameter or variable to the command to execute:

ansible-playbook playbooks/pipeline_action.yml -l $[ansibleHost] -e myAnsibleParam="${myCDParams}"

Types of arguments

Many kinds of data can be used to transfer values to Ansible script:

  • Input Parameter, from pipeline or procedure form. Exemple: $[myParameter]
  • Properties, from any object in Cloudbees CD/RO. Exemple: $[/projects/my_project_name/my_property]
  • Variable, define on procedure runtime. Exemple: ${my_varaibles}

Getting data, the hard way

Sending data is easy, but receiving data from a script to use it into a pipeline is more complicated.

To obtain data from a script, you need to run a script that creates a property for the job. After that, the property can be used in the procedure run.

Running a script to get data

Simply run a script, such as:

ansible-playbook playbooks/get_values.yml 

Parameter transfer takes place inside the script using the Cloudbees CD/RO command ectool:

- name: Define environment
  command: "ectool setProperty /myJob/environmentCreated {{ success_state }}"

This task defines an “environmentCreated” property in the job scope with as value for the ansible variable success_state.

You can create as many properties as you need.

Using the property

Like all properties, it can be accessed using $[…]
Example:

if [[ "$[/myJob/environmentCreated]" == "success" ]]; then
   ...

Conclusion

Retrieving values from a script in Cloudbees pipelines is perfectly possible using ectool, however, scripts need to be adapted to define properties that can be used in procedures.

To learn more about properties, see the Cloudbees CD/RO documentation