Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 2. Introduction to cloud-init
The cloud-init utility automates the initialization and configuration of virtual machines (VM) during the first boot. You can configure cloud-init to set hostnames, configure network interfaces, install packages, run scripts, and change default VM actions.
2.1. Overview of the cloud-init configuration Link kopierenLink in die Zwischenablage kopiert!
The cloud-init utility uses YAML-formatted configuration files to apply user-defined tasks to instances. When an instance boots, the cloud-init service initiates and executes the instructions from the YAML file. These tasks complete during the first boot or on subsequent boots of a virtual machine (VM), depending on the configuration.
To define the specific tasks, configure the /etc/cloud/cloud.cfg file and add directives under the /etc/cloud/cloud.cfg.d/ directory.
The
cloud.cfgfile includes directives for various system configurations, such as user access, authentication, and system information. This file also includes default and optional modules forcloud-init. The default module groups are as follows:-
cloud_init_modules -
cloud_config_modules -
cloud_final_modules
-
-
You can include additional directives for
cloud-initin thecloud.cfg.ddirectory. -
While adding directives to the
cloud.cfg.ddirectory, add them to a custom file named*.cfg, and always include#cloud-configat the top of the file.
For details, refer to an example file of cloud.cfg. See an example of cloud.cfg file.
2.2. Datasource types of cloud-init Link kopierenLink in die Zwischenablage kopiert!
Datasources are the sources of configuration data for cloud-init in the form of user data, metadata, and optionally vendor data. Metadata has the configuration drive created by certified cloud service provider. cloud-init automatically identifies the existing datasource, or you can configure a custom datasource directive.
# grep -i "vendor data" /var/log/cloud-init.log
For datasources, there are three types of configuration data: user data, metadata, and vendor data.
-
User data includes directives specified in the
cloud.cfgfile and thecloud.cfg.ddirectory. For example, user data can include files to run, packages to install, and shell scripts. Refer to thecloud-initDocumentation section User-Data Formats for information about the types of user data thatcloud-initallows. -
Metadata includes data associated with a specific datasource. For example, metadata can include a server name and instance ID. If you are using a specific cloud platform, the CCSP determines where your instance can find user data and metadata. After adding user data and metadata to an HTTP service. In this case, when
cloud-initruns, it consumes user data and metadata from the HTTP service. -
Vendor data is optionally provided by the organization (for example, a cloud provider) and includes information that can customize the image to better fit the environment where the image runs.
cloud-initacts upon optional vendor data and user data after it reads any metadata and initializes the system. By default, vendor data runs on the first boot. You can disable vendor data execution.
By default, cloud-init automatically identifies the existing datasource. cloud-init attempts to identify the cloud platform by using the script ds-identify. The script runs on the first boot of an instance. Adding a custom datasource directive can save time when cloud-init runs. You would add the directive in the /etc/cloud/cloud.cfg file or in the /etc/cloud/cloud.cfg.d directory. After cloud-init runs, you can view a log file (run/cloud-init/ds-identify.log) that provides detailed information about the platform. For details on datasource_list, see custom datasources.
For details on configuring datasources for certified cloud service provider, see:
2.3. Boot stages of cloud-init Link kopierenLink in die Zwischenablage kopiert!
During system boot, the cloud-init utility runs through five stages that specify whether cloud-init runs and where it finds its datasources, among other tasks. Each stage also belongs to a module execution phase, which controls what modules to run.
Detect stage: By using the
systemdservice, this stage determines whether to runcloud-initutility at the time of boot. Theds-identifytool detects whether or not to runcloud-initin the absence of a valid platform.This stage belongs to the Initialization phase and does not run any modules. Instead, it identifies the datasource for
cloud-initconfiguration.Local stage:
cloud-initsearches local data sources and applies network configuration, including the DHCP-based fallback mechanism.This stage belongs to the Initialization phase and does not run any modules.
Network stage:
cloud-initprocesses user data by running modules listed undercloud_init_modulesin the/etc/cloud/cloud.cfgfile. You can add, remove, enable, or disable modules in thecloud_init_modulessection.This stage belongs to the Networking module execution stage.
Config stage:
cloud-initruns modules listed undercloud_config_modulessection in the/etc/cloud/cloud.cfgfile. You can add, remove, enable, or disable modules in thecloud_config_modulessection.This stage belongs to the Configuration module execution phase.
Final stage:
cloud-initruns modules and configurations included in thecloud_final_modulessection of the/etc/cloud/cloud.cfgfile. It can include the installation of specific packages, and triggering configuration management plugins and user-defined scripts. You can add, remove, enable, or disable modules in thecloud_final_modulessection.This stage is equivalent to the Finalization module execution phase.
During the first boot of VM, when the cloud-init service initiates, all the configured modules run in their phases. At the next boot, whether a module runs within a phase depends on the module frequency of that module. Module frequency sets in the cloud-init configuration, and determines whether a module runs every time cloud-init runs on the instance, or only the first time cloud-init runs, even if the instance ID changes.
An instance ID uniquely identifies an instance. When an instance ID changes, cloud-init treats the instance as a new instance. For details, see Schema definition for module creation.
The possible module frequency values are as follows:
-
PER_INSTANCEmeans that the module runs on the first boot of an instance. For example, if you clone an instance or create a new instance from a saved image, the modules designated according to instance run again. -
ONCEmeans that the module runs only once. For example, if you clone an instance or create a new instance from a saved image, the modules designated per once do not run again on those instances. -
PER_ALWAYSmeans the module runs on every boot.
You can override a module’s frequency when you configure the module or by using the command line.
2.4. Files and directories significant for cloud-init Link kopierenLink in die Zwischenablage kopiert!
Red Hat supports the cloud-init utility, cloud-init modules, and default directories and files. You can use these directories and files to configure cloud-init, examine log files, find templates, and review configuration details after cloud-init completes execution. Depending on your requirement and datasource, there can be additional files and directories important to your configuration.
| Directory or File | Description |
|---|---|
|
|
The |
|
|
The |
|
|
When |
|
|
The |
|
|
This directory includes templates that you can enable in |
|
|
The |
|
|
The |
- The default
cloud.cfgfile To list and use modules included in the basic configuration for
cloud-init, use the/etc/cloud/cloud.cfgfile. You can configure or remove modules based on your requirements:-
To perform actions during one of the
cloud-initphases, you must configure each module individually and list them in thecloud.cfgfile. Modules run in the order given incloud.cfg. You typically do not change this order. However, you can add additional modules tocloud.cfg, if Red Hat supports the modules that you want to add. -
The
cloud.cfgdirectives can be overridden by user data. When runningcloud-initmanually, you can overridecloud.cfgwith command-line options. - Each module includes its own configuration options, where you can add specific information.
-
To ensure optimal functionality of the configuration, use module names with underscores (
_) rather than dashes (-). The default contents of the file for Red Hat Enterprise Linux (RHEL) are as follows:
users: - default disable_root: true resize_rootfs_tmp: /dev ssh_pwauth: false mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2'] ssh_deletekeys: true ssh_genkeytypes: ['rsa', 'ecdsa', 'ed25519'] syslog_fix_perms: ~ disable_vmware_customization: false cloud_init_modules: - migrator - seed_random - bootcmd - write_files - growpart - resizefs - disk_setup - mounts - set_hostname - update_hostname - update_etc_hosts - ca_certs - rsyslog - users_groups - ssh cloud_config_modules: - ssh_import_id - locale - set_passwords - rh_subscription - spacewalk - yum_add_repo - ntp - timezone - disable_ec2_metadata - runcmd cloud_final_modules: - package_update_upgrade_install - write_files_deferred - puppet - chef - ansible - mcollective - salt_minion - reset_rmc - rightscale_userdata - scripts_vendor - scripts_per_once - scripts_per_boot - scripts_per_instance - scripts_user - ssh_authkey_fingerprints - keys_to_console - install_hotplug - phone_home - final_message - power_state_change system_info: default_user: name: cloud-user lock_passwd: true gecos: Cloud User groups: [adm, systemd-journal] sudo: ["ALL=(ALL) NOPASSWD:ALL"] shell: /bin/bash distro: rhel network: renderers: ['sysconfig', 'eni', 'netplan', 'network-manager', 'networkd'] paths: cloud_dir: /var/lib/cloud templates_dir: /etc/cloud/templates ssh_svcname: sshd # vim:syntax=yaml-
usersspecifies the default user for the system. For more information, see Users and Groups. -
disable_rootenables or disables root login. For more information, see Authorized Keys. -
ssh_pwauthspecifies whethersshis configured to accept password authentication. For more information, see Set Passwords. -
mount_default_fieldsconfigures mount points. It must be a list containing six values. For more information, see Mounts. -
ssh_deletekeysspecifies whether to remove default host SSH keys. For more information, see Host Keys. -
ssh_genkeytypesspecifies key types to generate. For more information, see Host Keys. -
syslog_fix_permsconfigurescloud-initto log all boot stages to its log file. For more information, see thecloud-config.txtfile in theusr/share/doc/cloud-init/examplesdirectory. -
disable_vmware_customizationenables or disables VMware vSphere customization. -
cloud_init_modules:The modules in this section are services that run when thecloud-initservice starts, early in the boot process. -
cloud_config_modules:These modules run duringcloud-initconfiguration, after initial boot. -
cloud_final_modules:These modules run in the final phase ofcloud-init, after the configuration finishes. -
default_userspecifies details about the default user. For more information. see Users and Groups. -
distro:specifies the distribution. -
cloud_dirspecifies the main directory that hascloud-init-specific subdirectories. For more information, see Directory layout. -
templates_dirspecifies the location of the templates. -
ssh_svcnameis the name of the SSH service.
-
To perform actions during one of the
- The default
cloud.cfg.ddirectory -
cloud-initacts upon directives that you provide and configure. Typically, those directives are included in thecloud.cfg.ddirectory.
While you can configure modules by adding user data directives within the cloud.cfg file, as a best practice consider leaving cloud.cfg unmodified. Add your directives to the /etc/cloud/cloud.cfg.d directory. Adding directives to this directory can make future modifications and upgrades easier.
Refer to User-Data Formats for details on how to add a user script as *.cfg file.
- The default
05_logging.cfgfile -
To set logging information for
cloud-init, use the05_logging.cfgfile located in the/etc/cloud/cloud.cfg.ddirectory. This directory contains variouscloud-initdirectives that you can add.
By default, cloud-init uses the logging configuration in the 05_logging.cfg file for Red Hat Enterprise Linux (RHEL):
## This yaml formatted config file handles setting
## logger information. The values that are necessary to be set
## are seen at the bottom. The top '_log' are only used to remove
## redundancy in a syslog and fallback-to-file case.
##
## The 'log_cfgs' entry defines a list of logger configs
## Each entry in the list is tried, and the first one that
## works is used. If a log_cfg list entry is an array, it will
## be joined with '\n'.
_log:
- &log_base |
[loggers]
keys=root,cloudinit
[handlers]
keys=consoleHandler,cloudLogHandler
[formatters]
keys=simpleFormatter,arg0Formatter
[logger_root]
level=DEBUG
handlers=consoleHandler,cloudLogHandler
[logger_cloudinit]
level=DEBUG
qualname=cloudinit
handlers=
propagate=1
[handler_consoleHandler]
class=StreamHandler
level=WARNING
formatter=arg0Formatter
args=(sys.stderr,)
[formatter_arg0Formatter]
format=%(asctime)s - %(filename)s[%(levelname)s]: %(message)s
[formatter_simpleFormatter]
format=[CLOUDINIT] %(filename)s[%(levelname)s]: %(message)s
- &log_file |
[handler_cloudLogHandler]
class=FileHandler
level=DEBUG
formatter=arg0Formatter
args=('/var/log/cloud-init.log',)
- &log_syslog |
[handler_cloudLogHandler]
class=handlers.SysLogHandler
level=DEBUG
formatter=simpleFormatter
args=("/dev/log", handlers.SysLogHandler.LOG_USER)
log_cfgs:
# Array entries in this list will be joined into a string
# that defines the configuration.
#
# If you want logs to go to syslog, uncomment the following line.
# - [ *log_base, *log_syslog ]
#
# The default behavior is to just log to a file.
# This mechanism that does not depend on a system service to operate.
- [ *log_base, *log_file ]
# A file path can also be used.
# - /etc/log.conf
# This tells cloud-init to redirect its stdout and stderr to
# 'tee -a /var/log/cloud-init-output.log' so the user can see output
# there without needing to look on the console.
output: {all: '| tee -a /var/log/cloud-init-output.log'}
- The
/var/lib/clouddirectory layout -
When
cloud-initinitiates, it creates a directory layout with instance details andcloud-initconfiguration. This directory can include optional directories, such as/scripts/vendor. The following is a sample directory layout forcloud-init:
/var/lib/cloud/
- data/
- instance-id
- previous-instance-id
- previous-datasource
- previous-hostname
- result.json
- set-hostname
- status.json
- handlers/
- instance
- boot-finished
- cloud-config.txt
- datasource
- handlers/
- obj.pkl
- scripts/
- sem/
- user-data.txt
- user-data.txt.i
- vendor-data.txt
- vendor-data.txt.i
- instances/
f111ee00-0a4a-4eea-9c17-3fa164739c55/
- boot-finished
- cloud-config.txt
- datasource
- handlers/
- obj.pkl
- scripts/
- sem/
- user-data.txt
- user-data.txt.i
- vendor-data.txt
- vendor-data.txt.i
- scripts/
- per-boot/
- per-instance/
- per-once/
- vendor/
- seed/
- sem/
- config_scripts_per_once.once