更新时间:2025-07-24 GMT+08:00
分享

cloud-init-0.7.9及以上版本配置

  1. 添加如下键值对,前后各空出一行:
    no_ssh_fingerprints: true
  2. 设置ssh_pwauth,该选项表示是否支持ssh密码登录。true为启用,false或0为禁用。
    ssh_pwauth: true
  3. 修改disable_root为false。该参数表示是否禁用远程ssh root登录。
    disable_root: false
  4. 添加preserve_hostname: false。
    preserve_hostname: false
  5. 使用“#”注释掉以下语句(不存在忽略):
    mount_default_fields: [~, ~, 'auto', 'defaults,nofail', '0', '2']
    resize_rootfs_tmp: /dev
    ssh_deletekeys:   0
  6. 修改ssh_genkeytypes为下面语句(不存在添加):
    ssh_genkeytypes: ['rsa', 'dsa']
  7. 修改syslog_fix_perms为下面语句(不存在添加):
    syslog_fix_perms: root:root
  8. 添加下面语句:
    network:
       config: disabled
    datasource_list: [ OpenStack ]
    datasource:
      OpenStack:
        metadata_urls: ['http://169.254.169.254']
        max_wait: 120
        timeout: 10
        retries: 5
  9. (可选)在“/etc/cloud/cloud.cfg”中配置“apply_network_config: False”。

    对于使用Cloud-Init 18.3及以上版本的用户,需执行本操作

  10. 在cloud_final_modules段的“- final-message”后面添加一句:
    - power-state-change
  11. 查看并修改system info中的信息:
    system_info:
       distro: rhel
       default_user:
         name: root   //登录操作系统使用的用户名
         lock_passwd: False   //True表示禁用密码登录方式,注意部分操作系统此处配置为1表示禁用

    其中,distro参数需要根据具体操作系统类型修改,比如distro: sles、distro: rhel、distro: ubuntu、distro: debian、distro: fedora等。

  12. cloud-init 23.x以外的版本,修改如下源码:

    源码路径:xxx/xxx/cloudinit/sources/DataSourceOpenStack.py

    • 新增“return True”行。
    • 删除“(DataSourceOpenStackLocal, (sources.DEP_FILESYSTEM,)),”行。
     def detect_openstack(accept_oracle=False):
         """Return True when a potential OpenStack platform is detected."""
         return True
         if not util.is_x86():
             return True  # Non-Intel cpus don't properly report dmi product names
         product_name = util.read_dmi_data('system-product-name')
    ......
     # Used to match classes to dependencies
     datasources = [
         (DataSourceOpenStack, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
     ] 
  13. 针对cloud-init 23.x版本及以上版本,修改如下源码:
    • 源码路径:xxx/xxx/cloudinit/sources/DataSourceOpenStack.py,删除“(DataSourceOpenStackLocal, (sources.DEP_FILESYSTEM,))”行。
       # Used to match classes to dependencies
       datasources = [
           (DataSourceOpenStack, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
       ] 
    • 源码路径:xxx/xxx/cloudinit/sources/__init__.py,修改“return_value = self._check_and_get_data()”为“return_value = self._get_data()”。
      def get_data(self) -> bool:
          """Datasources implement _get_data to setup metadata and userdata_raw.
          Minimally, the datasource should return a boolean True on success.
          """
          self._dirty_cache = True
          return_value = self._get_data()
          if not return_value:
              return return_value
          self.persist_instance_data()
          return return_value
  14. (可选)对于SUSE 12 SP1和SUSE 12 SP2操作系统,还需要修改“/usr/lib/systemd/system/cloud-init-local.service”配置文件的[Unit]配置信息。

    vi /usr/lib/systemd/system/cloud-init-local.service

    保持[Unit]配置项与下面内容一致:

    [Unit]
    Description=Initial cloud-init job (pre-networking)
    DefaultDependencies=no
    Wants=network-pre.target
    Wants=local-fs.target
    After=local-fs.target
    Before=network-pre.target
    Before=shutdown.target
    Before=basic.target
    Conflicts=shutdown.target
    # Other distros use Before=sysinit.target. There is not a clearly identified
    # reason for usage of basic.target instead.
  15. (可选)对于Ubuntu 16.04操作系统,还需要执行以下命令,配置OpenStack源。

    dpkg-reconfigure cloud-init

    执行命令vim /etc/cloud/cloud.cfg.d/90_dpkg.cfg打开配置文件,检查配置项是否正确。

    # to update this file, run dpkg-reconfigure cloud-init
    datasource_list: [ OpenStack ]
    ~
    ~
    ~

    配置文件和以上回显信息一致,表示配置成功。

相关文档