【问题标题】:How to pass json/list values to a template in HEAT user_data如何将 json/list 值传递给 HEAT user_data 中的模板
【发布时间】:2021-04-22 14:57:15
【问题描述】:

需要将 user_data 替换为 comma_delimited_list 中资源组中的动态值。

%index% 不起作用。

heat_template_version: 2015-10-15

test_parameter:
type: comma_delimited_list
label: test param  list
description: test descr
default: 'test_param_1,test_param_2'
.......
...... 

type: OS::Heat::ResourceGroup
properties:
.........
.........
user_data:
    str_replace:
      template: |
        get the local value of the instance : $val
      params:
        $val: { get_param: [ test_parameter, %index% ] } 
        
        

如何将 json/list 值传递给 HEAT-user_data 中的模板

【问题讨论】:

    标签: openstack openstack-heat


    【解决方案1】:

    遗憾的是,基于 aws-cloudformation 的语言仍然在一定程度上受限于您通常可以做的事情,并且特定于热的语言(仍然基于 aws-cloudformation)受到同样的限制。

    通常,您可以拥有的最佳解决方案是在模板顶部有一个“选择”列表,并将一些选择传递给您的资源。在以下示例中,其中一个选项要求安装特定的网络应用程序(来自 nginx 和 apache),并将其作为变量传递给模板的用户数据部分:

    heat_template_version: 2015-04-30
    
    description: >
      Hello world HOT template that just defines a single server.
      Contains just base features to verify base HOT support.
    
    parameters:
      key_name:
        type: string
        description: Name of an existing key pair to use for the server
        default: "topcat-01"
        constraints:
          - custom_constraint: nova.keypair
      flavor:
        type: string
        description: Flavor for the server to be created
        default: m1.normal
        constraints:
          - custom_constraint: nova.flavor
      image:
        type: string
        description: Image ID or image name to use for the server
        default: Ubuntu-1404lts-32-Cloud
        constraints:
          - custom_constraint: glance.image
      my_network:
        type: string
        description: Neutron Network
        default: public-internet-access
        constraints:
          - custom_constraint: neutron.network
      application-install:
        type: string
        default: apache
        constraints: 
          - allowed_values: [apache, nginx]
            description: Value must be one of 'apache', or 'nginx'
    
    resources:
      server:
        type: OS::Nova::Server
        properties:
          key_name: { get_param: key_name }
          image: { get_param: image }
          flavor: { get_param: flavor }
          security_groups: 
            - default
          name: test-single-topcat-instance
          networks:
            - network: { get_param: my_network }
          user_data_format: RAW
          user_data:
            str_replace:
              template: |
                #!/bin/bash
                myapp="$selectedapp01"
                echo "MY APP IS $myapp"
                PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
                case $myapp in
                apache)
                    echo "INSTALLING APACHE"
                    if [ -f /etc/debian_version ]
                    then
                       aptitude -y update
                       aptitude -y install apache2
                    fi
                    if [ -f /etc/redhat-release ]
                    then
                       yum -y install httpd
                       chkconfig httpd on
                       echo "" >> /var/www/html/index.html
                       service httpd start
                    fi
                    ;;
                nginx)
                    echo "INSTALLING NGINX"
                    if [ -f /etc/debian_version ]
                    then
                       aptitude -y update
                       aptitude -y install nginx-full
                    fi
                    if [ -f /etc/redhat-release ]
                    then
                       yum -y install nginx
                       chkconfig nginx on
                       service nginx start
                    fi
                    ;;
                esac
              params:
                $selectedapp01: {get_param: application-install}
    
    outputs:
      server_networks:
        description: The networks of the deployed server
        value: { get_attr: [server, networks] }
    

    从上面的示例中,“application-install”参数(可以是 apache 或 nginx)在 user-data 部分的末尾将被解释为“$selectedapp01”,并解释为“$myapp”(myapp ="$selectedapp01") 在用户数据的开头。

    这并不完美,但这是正常的方法。大多数 cloudformation 部署工具从一组“问题和答案”构建最终模板,并且您完成了一个静态(半静态)模板。

    我在这里添加的这个作为示例,会询问你很多事情并设置一些默认值。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 2020-03-02
      • 2017-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      相关资源
      最近更新 更多