Chapter 4. Enabling substitutable variables across the order process workflow

You can substitute variables across products used in your order process workflow. Implementing substitutable variables requires creating or editing the job template survey on Ansible Tower and enabling substitution on surveys attached to products on Automation Services Catalog.

Substitution format and requirements

Substitutable values are expressed in the format {{substitution_express}} with no spaces allowed between the braces and the expression. Refer to Substitute variables for information on each variable you can use for substitution.

4.1. Implementing substitutable variables in your order process workflow

In this section we will demonstrate how to align data you expose to Automation Services Catalog in your playbooks with substitutable variables, using the playbooks examples provided earlier in this guide.

4.1.1. Creating a before order process to pass a value to the product order.

Example playbook before_order.yml for before order process

# This playbook prints a simple debug message and set_stats
- name: Echo Hello, world!
  hosts: localhost
  gather_facts: yes
  tasks:
    - debug:
        msg: "Hello, world!"

    - set_stats:
        data:
          expose_to_cloud_redhat_com_favorite_color: "orange"

This playbook returns to Automation Services Catalog a playbook artifact favorite_color that has the value orange. You do not need to make a job template survey on Ansible Tower since no user input is required. Instead, you can pass favorite_color to the product playbook by editing the survey on Automation Services Catalog set to the before order process product.

4.1.2. Configuring the product order to use a substituted variable

This example product playbook accepts a substituted variable for {{favorite_color}} and passes back to Automation Services Catalog the artifact 'after_data'.

Example playbook product_order.yml

# This playbook prints a simple debug message with given information
- name: Echo Hello, world!
  hosts: localhost
  gather_facts: yes
  tasks:
    - debug:
        msg: "Hello, {{favorite_color}} world!"

    - set_stats:
        data:
          expose_to_cloud_redhat_com_after_data: "{{favorite_color}}"

Create a job template on Ansible Tower for the 'product_order.yml' playbook that sets favorite_color as the *Answer Variable Name.

Create a survey for the product_order.yml job template that includes:

  1. Enter What is your favorite color? in the Prompt field.
  2. Enter favorite_color in the Answer Variable Name field.
  3. Click btn:[UPDATE].

Enable the product order survey on Automation Services Catalog to use the 'favorite_color' value passed from before_order.yml.

In the survey attached to your product order:

  1. Update fields with the following values:

    1. Name: favorite_color
    2. Initial Value: {{before.before_order.artifacts.favorite_color}}
    3. Label: What is your favorite color?
    4. Placeholder: {{before.before_order.artifacts.favorite_color}}
  2. Toggle the Disabled switch to the active position.
  3. Toggle the Substitution switch to the active position.

You have now configured the product order to use the favorite_color artifact from the before_order.yml playbook as a substituted variable.

4.1.3. Configuring the after order product to accept after_data artifact values from the product order

Use surveys to pass the 'product_order.yml' artifact after_data to after_order.yml using substitutable variables.

Example playbook after_order.yml for after order product

# This playbook prints a simple debug message with given information
- name: Echo Hello, world!
  hosts: localhost
  gather_facts: yes
  tasks:
    - debug:
        msg: "{{after_data}}"

Create a job template on Ansible Tower for the 'after_order.yml' playbook that sets after_data as the *Answer Variable Name.

In the survey attached to your after_order.yml job template:

  1. Type "Enter your after data" in the Prompt field.
  2. Enter after_data in the Answer Variable Name field.
  3. Click btn:[UPDATE].

Enable the after order survey on Automation Services Catalog to use the 'after_data' value passed from after_order.yml.

In the survey attached to your after order:

  1. Update fields with the following values:

    1. Name: after_data
    2. Initial Value: {{product.artifacts.after_data}}
    3. Label: "Enter your after data"
    4. Placeholder: {{product.artifacts.after_data}}
  2. Toggle the Disabled switch to the active position.
  3. Toggle the Substitution switch to the active position.

The playbook will display the value passed for after data when the job template is run.

4.2. Substitute variables reference

A substitution has the format of {{substitution_express}}. Do not use a space between the curly brackets and the expression.

Substituted values are set in Ansible Tower jobs template surveys. Substituted variables pass from one product in the order process to another, depending on your configuration.

See Surveys in the Ansible Tower User Guide for more information on updating job template surveys.

Follow the table below when including substitutable variables in your surveys.

Table 4.1. Object model reference table

ObjectExpressionNotes

Order

order.order_id

order.created_at

order.ordered_by.name

order.ordered_by.email

order.approval.updated_at

order.approval.decision

order.approval.reason

order.created_at` is the date and time when the order started.

Date format: 2007-02-10T20:30:45Z

Example: {{order.approval.reason}}

Before order

before.<order_process_name>.artifacts.<fact_name>

before.<order_process_name>.parameters.<parameter_name>

before.<order_process_name>.status

No quotes are needed for the order_process_name if it contains space.

Example: {{before.Sample Order.artifacts.ticket_number}}

Product

product.name

product.description

product.long_description

product.help_url

product.support_url

product.vendor

product.platform

product.portfolio.name

product.portfolio.description

product.artifacts.<fact_name>

product.parameters.<parameter_name>

product.status

Example: {{product.artifacts.after_data}}

After order

after.<order_process_name>.artifacts.<fact_name>

after.<order_process_name>.parameters.<parameter_name>

after.<order_process_name>.status

No quotes are needed for the order_process_name if it contains space.

Example: {{after.SampleOrder.status}}