openstackopenstack-heat

openstack heat: Add port to instance based on condition


I want to add port to the instance only if the name is zee_1

template file:

resources:

  vm_port_routable:
    type: OS::Neutron::Port
    properties:
      network: { get_param: abc_routable_net }
      name:
        str_replace:
          template: $stack_$name_routable_port
          params:
            $stack: { get_param: stack }
            $name: { get_param: vm_name }
      security_groups: [{ get_param: security_group }]
      fixed_ips:
        - ip_address: { get_param: port_ip_routable }

  test_vm_port_routable:
    type: OS::Neutron::Port
    properties:

      network: { get_param: test_zee_routable_net }
      name:
        str_replace:
          template: $stack_$name_routable_port
          params:
            $stack: { get_param: stack }
            $name: { get_param: vm_name }
      security_groups: [{ get_param: security_group }]
      fixed_ips:
        - ip_address: { get_param: test_port_ip_routable }

      zee_server:
        type: OS::Nova::Server
        properties:
          block_device_mapping_v2:
            - boot_index: 0
              delete_on_termination: false
              volume_id: { get_resource: root_volume }
              device_name: vda
              device_type: disk
            - boot_index: -1
              delete_on_termination: false
              volume_id: { get_param: backup_volume }
              device_name: vdc
              device_type: disk
          flavor: { get_param: flavor }
          key_name: { get_param: ssh_keypair }
          name: { get_param: vm_name }

          networks:
            - port: { get_resource: vm_port_routable }
            - port: { get_resource: test_vm_port_routable }

I want to add port "test_vm_port_routable" only if name is zee_1....

I tried with below condition but it is giving error :ERROR: The template section is invalid: conditions

conditions:
       create_prod_res: { equal: [{get_param: "vm_name"},"zee_1"]}
resources:

  vm_port_routable:
    type: OS::Neutron::Port
    properties:
      network: { get_param: abc_routable_net }
      name:
        str_replace:
          template: $stack_$name_routable_port
          params:
            $stack: { get_param: stack }
            $name: { get_param: vm_name }
      security_groups: [{ get_param: security_group }]
      fixed_ips:
        - ip_address: { get_param: port_ip_routable }

  test_vm_port_routable:
    type: OS::Neutron::Port
    properties:

      network: { get_param: test_zee_routable_net }
      name:
        str_replace:
          template: $stack_$name_routable_port
          params:
            $stack: { get_param: stack }
            $name: { get_param: vm_name }
      security_groups: [{ get_param: security_group }]
      fixed_ips:
        - ip_address: { get_param: test_port_ip_routable }

      zee_server:
        type: OS::Nova::Server
        properties:
          block_device_mapping_v2:
            - boot_index: 0
              delete_on_termination: false
              volume_id: { get_resource: root_volume }
              device_name: vda
              device_type: disk
            - boot_index: -1
              delete_on_termination: false
              volume_id: { get_param: backup_volume }
              device_name: vdc
              device_type: disk
          flavor: { get_param: flavor }
          key_name: { get_param: ssh_keypair }
          name: { get_param: vm_name }
          condition:create_prod_res  
          networks:
            - port: { get_resource: vm_port_routable }
            - port: { get_resource: test_vm_port_routable }

with the above changes to template it is failing
can some one help me in this .....


Solution

  • If you read the specification, you will note that support for the conditions section was only added in Newton. So you need either:

    heat_template_version: newton
    

    Or:

    heat_template_version: 2016-10-14
    

    Or the equivalent for a later release. If you're running an openstack release earlier than Newton, you simply won't be able to use this feature.