Ask Your Question
0

openstack heat: Add port to instance based on condition

asked 2017-12-14 12:09:39 -0500

Naresh237 gravatar image

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 }

can some one help me in this .....

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2020-02-06 12:19:04 -0500

PaulN gravatar image

I know this is a somewhat old question but I had been trying to figure out how to make a port optional for quite a while and had stumbled on this question with no real answer. I was finally able to accomplish this using a combination of a condition, an OS::Heat::Value and some yaql so I figured I'd post my solution.

You can easily use a condition to not create the port. And you can pass a list of ports to the networks attribute of the OS::Nova::Server, but the issue I had was in creating the list with only the valid ports. If I used a conditional to control whether a port was added to the list or not I didn't end up with a non-existent entry in the list, I ended up with a list with an entry that was just empty (i.e. the list would contain an entry that consisted of whatever I put in the else portion of the if statement). The OS::Nova::Server wouldn't then accept that list with 'invalid' entries. I needed a way for it to not insert that entry at all. The solution I found was to use yaql to build the list and to omit any invalid entries.

This can be done as follows to create the port list.

server_port_list:
    type: OS::Heat::Value
    properties:
        value:
            yaql:
                expression: 'list($.data.where($.containsKey(port)))'
                data:
                    - port: { get_resource: int_port }
                    - port: { get_resource: oam_port }
                    - { if: [ "create_ext_port_condition", { port: { get_resource: ext_port } }, {} ] }

This can then be passed to the networks attibute of the OS::Nova::Server by getting the value attribute of the server_port_list:

networks: { get_attr: [ server_port_list, value ] }

Full example heat template is below:

heat_template_version: 2016-10-14

parameters: prefix: type: string oam_network: type: string ext_network: type: string availability_zone: type: string server_flavor: type: string server_key_name: type: string server_image: type: string server_security_group: type: string create_ext_port: type: string default: "0"

conditions:

create_ext_port_condition: { equals: [ { get_param: create_ext_port }, "1"] }

resources:

networks:
    type: networks.yaml
    properties:
        prefix: { get_param: prefix }

server_port_list:
    type: OS::Heat::Value
    properties:
        value:
            yaql:
                expression: 'list($.data.where($.containsKey(port)))'
                data:
                    - port: { get_resource: int_port }
                    - port: { get_resource: oam_port }
                    - { if: [ "create_ext_port_condition", { port: { get_resource: ext_port } }, {} ] }

server:
    type: OS::Nova::Server
    properties:
        name: server_1
        availability_zone: { get_param: availability_zone }
        image: { get_param: server_image }
        flavor: { get_param: server_flavor }
        key_name: { get_param: server_key_name }
        image_update_policy: REBUILD
        config_drive: 'True'
        networks: { get_attr: [ server_port_list, value ] }                               

int_port:
    type: OS::Neutron::Port
    properties:
        name: test_int_port
        network: { get_attr: [ networks,  int_network ] }
        security_groups: [ { get_param: server_security_group } ]
        fixed_ips: [ { subnet_id: { get_attr: [ networks,  int_network_subnet ] }, ip_address: "192.168.22.5" } ]

oam_port:
    type: OS::Neutron::Port
    properties:
        name: test_oam_port
        network: { get_param: oam_network }
        security_groups: [ { get_param: server_security_group } ]            

ext_port:
    type: OS::Neutron::Port
    properties:
        name: test_ext_port
        network: { get_param: ext_network }
        security_groups: [ { get_param: server_security_group } ] 
    condition: create_ext_port_condition

outputs:

oam_ip:
    value: { get_attr: [ oam_port, fixed_ips, 0, ip_address ] }
edit flag offensive delete link more
0

answered 2019-02-03 17:20:51 -0500

DId you get any solution ?. Btw for me its cribbing on condition:

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2017-12-14 12:09:39 -0500

Seen: 325 times

Last updated: Dec 14 '17