shellopenstackansible-templateopenstack-heat

Unable to run commands inside user_data in openstack heat template


I want to run some commands after server boot from HOT template. There isn't any error thrown; the commands inside 'runcmd' just don't work.

  Server02:
    type: OS::Nova::Server
    properties:
      name: { get_param: [SV_HOSTNAMES, 1] }
      flavor: { get_param: FLAVOR_N }
      image: { get_param: IMAGE_N }
      networks:
        - port: { get_resource: Server02_PRI_NET_ID_port }
        - port: { get_resource: Server02_SEC_NET_ID_port }
      config_drive: true
      user_data_format: RAW
      user_data:
        str_replace:
           template: |
               runcmd: |
                  #!/bin/sh
                  echo $ETH0_IP $NODENAME.vil.in >> /etc/hosts
                  rm -f /etc/sysconfig/network-scripts/ifcfg-eth0
                  echo -e "BOOTPROTO=static\nDEVICE=eth0\nIPADDR=$ETH0_IP\nPREFIX=24\nONBOOT=yes\nMTU=9000\nTYPE=Ethernet\nUSERCTL=no\nNMCONTROLLED=no" > /etc/sysconfig/network-scripts/ifcfg-eth0
                  rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
                  echo -e "BOOTPROTO=static\nDEVICE=eth1\nIPADDR=$ETH1_IP\nPREFIX=24\nONBOOT=yes\nMTU=9000\nTYPE=Ethernet\nUSERCTL=no\nNMCONTROLLED=no" > /etc/sysconfig/network-scripts/ifcfg-eth1
                  sync;sync;shutdown -r +1
           params:
              $NODENAME: { get_param: [SV_HOSTNAMES, 1] }
              $ETH0_IP: { get_attr: [Server02_PRI_NET_ID_port, fixed_ips, 0, ip_address] }
              $NETGW0: { get_attr: [Server02_PRI_NET_ID_port, subnets, 0, gateway_ip] }
              $NETCIDR0: { get_attr: [Server02_PRI_NET_ID_port, subnets, 0, cidr] } 
              $ETH1_IP: { get_attr: [Server02_SEC_NET_ID_port, fixed_ips, 0, ip_address] }
              $NETGW1: { get_attr: [Server02_SEC_NET_ID_port, subnets, 0, gateway_ip] }
              $NETCIDR1: { get_attr: [Server02_SEC_NET_ID_port, subnets, 0, cidr] } 
              $VOL0: {get_resource: Server02_VDB_VOL }

Below lines work as expected but not able to pass params to cloud_config

  #*************************************************#
  #***********Instance Definition*****************#
  #*************************************************#
  vServer1:
    type: OS::Nova::Server
    properties:
      name: 
        str_replace:
           template: { get_param: sv_hostname }
           params:
             HOSTNAME: { get_param: sv_hostname }
      flavor: F_TestSmall
      image: rhel7-image
      networks:
        - port: { get_resource: port_instance1a }
        - port: { get_resource: port_instance1b }
      config_drive: true
      user_data_format: SOFTWARE_CONFIG
      user_data: {get_resource: root_config}
  #*************************************************#
  #***********Script Definition*****************#
  #*************************************************#
  root_config:
    type: OS::Heat::CloudConfig
    properties:
      cloud_config:
        ssh_pwauth: true
        disable_root: false
        chpasswd: { expire: False }
        write_files:
        - path: /tmp/test_script.sh
          permissions:  '0777'
          content: |
            #!/bin/bash
            rm -f /etc/sysconfig/network-scripts/ifcfg-eth0
            echo -e "BOOTPROTO=static\nDEVICE=eth0\nIPADDR=12.16.33.5\nNETMASK=255.255.255.0\nONBOOT=yes\nMTU=9000\nTYPE=Ethernet\nUSERCTL=no\nNMCONTROLLED=no" > /etc/sysconfig/network-scripts/ifcfg-eth0
            rm -f /etc/sysconfig/network-scripts/ifcfg-eth1
            echo -e "BOOTPROTO=static\nDEVICE=eth1\nIPADDR=12.16.34.5\nNETMASK=255.255.255.0\nONBOOT=yes\nMTU=9000\nTYPE=Ethernet\nUSERCTL=no\nNMCONTROLLED=no" > /etc/sysconfig/network-scripts/ifcfg-eth1
            service network restart
            systemctl disable cloud-init.service
        runcmd:
            - sh /tmp/test_script.sh

I have also tried removing 'runcmd' and executing commands under user_data but in vain.


Solution

  • thanks for your response. I had already tried this option but again somehow it did not work for me. But I did find a solution just an hour back and it worked. Out of a 100 ways to do it; probably the 50th option worked for me. :)

      Server02:
        type: OS::Nova::Server
        properties:
          name: { get_param: [SV_HOSTNAMES, 1] }
          flavor: { get_param: FLAVOR_N }
          image: { get_param: IMAGE_N }
          networks:
            - port: { get_resource: Server02_PRI_NET_ID_port }
            - port: { get_resource: Server02_SEC_NET_ID_port }
          config_drive: true
          user_data_format: SOFTWARE_CONFIG
          user_data: {get_resource: root_config_02}
      root_config_02:
        type: OS::Heat::SoftwareConfig
        properties:
          config:
            str_replace: 
              params:
                 $NODENAME: { get_param: [SV_HOSTNAMES, 1] }
                 $ETH0_IP: { get_attr: [Server02_PRI_NET_ID_port, fixed_ips, 0, ip_address] }
              template: |
                #!/bin/bash
                hostnamectl set-hostname $NODENAME.vil.in
                echo $ETH0_IP $NODENAME.vil.in >> /etc/hosts
    

    hope other find this useful