第 4 章 在订购工作流中启用可替换变量


您可以在您的订购过程工作流中使用的产品中替换变量。实现可替换变量需要创建或编辑 Ansible Tower 上的作业模板问卷调查,并启用对附加到 Automation Services Catalog 中的产品的调查的替换功能。

替换格式和要求

可替换值需要以 {{sub replaceion_express}} 格式表示,在大括号和表达式之间不能有空格。相关的详情,请参阅每个参数的替换变量的信息。

4.1. 在订购过程工作流中实现可替换变量

在本小节中,我们将演示如何使用本指南前面提供的 playbook 示例,在 playbook 中通过可替换变量使用在 Automation Services Catalog 中使用的数据。

用于 before order 进程的 before_order.yml playbook 示例

# 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"
Copy to Clipboard Toggle word wrap

此 playbook 为 Automation Services Catalog 返回一个 playbook 工件 favorite_color,它的值为 orange。因为不需要用户输入,所以不需要在 Ansible Tower 上进行作业模板问卷调查。您可以通过编辑 Automation Services Catalog 中的问卷调查,设置 before order 进程产品,将 favorite_color 传递给产品 playbook。

4.1.2. 配置 product order 来使用一个替换的变量

这个 product playbook 示例接受一个 {{favorite_color}} 可替代变量,并返回 Automation Services Catalog 工件 'after_data'。

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}}"
Copy to Clipboard Toggle word wrap

在 Ansible Tower 上为 'product_order.yml' playbook 创建一个作业模板,将 favorite_color 设置为 *Answer Variable Name。

product_order.yml 作业模板创建问卷调查,其中包括:

  1. Prompt 字段中输入 What is your favorite color?
  2. Answer Variable Name 字段中输入 favorite_color
  3. UPDATE

在 Automation Services Catalog 上启用 product order 调查,以使用从 before_order.yml 传递的 'favorite_color' 值。

在附加到您的 product order 的问卷调查中:

  1. 使用以下值更新字段:

    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. Disabled 开关切换到活跃位置。
  3. Substitution 开关切换到活跃位置。

现在,您已将 product order 配置为使用 before_order.yml playbook 中的 favorite_color 工件作为替换的变量。

通过使用可替换变量,可以使用问卷调查将 'product_order.yml' 工件 after_data 传递给 after_order.yml

用于 after order 产品的 after_order.yml playbook 示例

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

为 'after_order.yml' playbook 在 Ansible Tower 上创建一个作业模板,该 playbook 将 after_data 设置为 *Answer Variable Name。

在附加到 after_order.yml 作业模板的问卷调查中:

  1. Prompt 字段中输入"Enter your after data"。
  2. Answer Variable Name 字段中输入 after_data
  3. UPDATE

在 Automation Services Catalog 上启用 after order 问卷调查,以使用从 after_order.yml 传递的 'after_data' 值。

在附加到您的 after order 的问卷调查中:

  1. 使用以下值更新字段:

    1. Name: after_data
    2. Initial Value: {{product.artifacts.after_data}}
    3. Label: "Enter your after data"
    4. Placeholder: {{product.artifacts.after_data}}
  2. Disabled 开关切换到活跃位置。
  3. Substitution 开关切换到活跃位置。

在运行作业模板时,playbook 将显示为 after data 传递的值。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat