1

我想在服务器从 HOT 模板启动后运行一些命令。没有抛出任何错误;'runcmd' 中的命令不起作用。

  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 }

下面的行按预期工作,但无法将参数传递给 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

我也尝试过删除“runcmd”并在 user_data 下执行命令,但徒劳无功。

4

2 回答 2

0

感谢您的答复。我已经尝试过这个选项,但不知何故它对我不起作用。但我确实在一小时前找到了解决方案,并且它奏效了。100 种方法中的一种;可能第 50 个选项对我有用。:)

  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

希望其他人觉得这很有用

于 2020-07-13T05:07:22.107 回答
0

我对 HEAT 没有深入的了解,但也许这可以帮助你:

https://ask.openstack.org/en/question/103576/how-to-pass-parameter-value-to-runcmd-in-oheatcloudconfig-resource/

已经有人问过,是否可以将参数传递给cloud_config. 在那里,它通过嵌入式模板解决了。因为cloud_config对您有用,所以这也可以解决您的问题。

于 2020-07-12T22:12:41.597 回答