Ce contenu n'est pas disponible dans la langue sélectionnée.
Configuring and managing cloud-init for RHEL 9
Using cloud-init to automate the initialization of cloud instances
Abstract
- How cloud-init works
- How to use cloud-init to initiate cloud instances
- What uses of cloud-init Red Hat supports
Providing feedback on Red Hat documentation Copier lienLien copié sur presse-papiers!
We are committed to providing high-quality documentation and value your feedback. To help us improve, you can submit suggestions or report errors through the Red Hat Jira tracking system.
Procedure
Log in to the Jira website.
If you do not have an account, select the option to create one.
- Click Create in the top navigation bar.
- Enter a descriptive title in the Summary field.
- Enter your suggestion for improvement in the Description field. Include links to the relevant parts of the documentation.
- Click Create at the bottom of the dialogue.
Chapter 1. Introducing RHEL on public cloud platforms Copier lienLien copié sur presse-papiers!
Public cloud platforms offer computing resources as a service. Instead of using on-premise hardware, you can run your IT workloads, including Red Hat Enterprise Linux (RHEL) systems, as public cloud instances.
1.1. Benefits of using RHEL in a public cloud Copier lienLien copié sur presse-papiers!
Red Hat Enterprise Linux (RHEL) cloud instances on public cloud platforms have these benefits over on-premise RHEL systems or virtual machines (VMs):
- Flexible and fine-grained allocation of resources
A RHEL cloud instance runs as a VM on a cloud platform. The platform is a cluster of remote servers that the cloud service provider maintains. You can select hardware resources at the software level. For example, you can select a CPU type or storage setup.
Unlike a local RHEL system, you are not limited by what your physical host can do. Instead, you can select from many features that the cloud provider offers.
- Space and cost efficiency
You do not need to own on-premise servers to host cloud workloads. This removes the space, power, and maintenance needs for physical hardware.
On public cloud platforms, you pay the cloud provider for cloud instance usage. Costs depend on the hardware you use and how long you use it. You can control costs to meet your needs.
- Software-controlled configurations
You can save a cloud instance configuration as data on the cloud platform and control it with software. With this configuration, you can create, remove, clone, or migrate instances easily. You can also manage a cloud instance remotely through a cloud provider console. The instance connects to remote storage by default.
You can back up a cloud instance as a snapshot at any time. You can then load the snapshot to restore the instance to the saved state.
- Separation from the host and software compatibility
Unlike a local VM, a RHEL cloud instance uses Kernel-based Virtual Machine (KVM) virtualization. The guest kernel is separate from the host operating system. It is also separate from the client system you use to connect to the instance.
You can install any operating system on the cloud instance. On a RHEL public cloud instance, you can run RHEL apps you cannot use on your local operating system.
If the instance operating system becomes unstable or compromised, it does not affect your client system.
1.2. Public cloud use cases for RHEL Copier lienLien copié sur presse-papiers!
Deploying applications on a public cloud offers many benefits, but might not be the most efficient solution for every scenario. If you are evaluating the migration of your Red Hat Enterprise Linux (RHEL) deployments to the public cloud, consider whether your use case will benefit from the advantages of the public cloud.
Beneficial use cases
Deploying public cloud instances is effective for increasing and decreasing the active computing power of your deployments, also known as scaling up and scaling down. Therefore, consider using RHEL on public cloud for the following scenarios:
- Clusters with high peak workloads and low general performance requirements. Scaling up and down based on your demands can be efficient in terms of resource costs.
- Setting up or expanding your clusters to a public cloud to avoid high upfront costs of setting up local servers.
- Cloud instances are agnostic of the local environment. Therefore, you can use them for backup and disaster recovery.
Potentially problematic use cases
- You are running an existing environment that is not flexible to migrate to a public cloud. Therefore, customizing a cloud instance to fit the specific needs of an existing deployment might not be suitable for your use case and compared to your current host platform.
- You are operating on a tight resource budget. Maintaining your deployment in a local data center typically provides less flexibility but more control over the maximum resource costs than the public cloud.
Next steps
1.3. Frequent concerns when migrating to a public cloud Copier lienLien copié sur presse-papiers!
Moving your RHEL workloads from a local environment to a public cloud platform might raise concerns about the changes involved. The following are the most commonly asked questions.
Will my RHEL work differently as a cloud instance than as a local virtual machine?
In most respects, RHEL instances on a public cloud platform work the same as RHEL virtual machines on a local host, such as an on-premises server. Notable exceptions include:
- Instead of private orchestration interfaces, public cloud instances use provider-specific console interfaces for managing your cloud resources.
- Certain features, such as nested virtualization, may not work correctly. If a specific feature is critical for your deployment, check the feature’s compatibility in advance with your chosen public cloud provider.
Will my data stay safe in a public cloud as opposed to a local server?
The data in your RHEL cloud instances is in your ownership, and your public cloud provider does not have any access to it. In addition, major cloud providers support data encryption in transit, which improves the security of data when migrating your virtual machines to the public cloud.
The general security of your RHEL public cloud instances is managed as follows:
- Your public cloud provider is responsible for the security of the cloud hypervisor
- Red Hat provides the security features of the RHEL guest operating systems in your instances
- You manage the specific security settings and practices in your cloud infrastructure
What effect does my geographic region have on the functionality of RHEL public cloud instances?
You can use RHEL instances on a public cloud platform regardless of your geographical location. Therefore, you can run your instances in the same region as your on-premises server.
However, hosting your instances in a physically distant region might cause high latency when operating them. In addition, depending on the public cloud provider, certain regions may provide additional features or be more cost-efficient. Before creating your RHEL instances, review the properties of the hosting regions available for your chosen cloud provider.
1.4. Obtaining RHEL for public cloud deployments Copier lienLien copié sur presse-papiers!
To deploy a Red Hat Enterprise Linux (RHEL) system in a public cloud environment, you need to:
Select the optimal cloud provider for your use case, based on your requirements and the current offer on the market. The cloud providers currently certified for running RHEL instances are:
- Amazon Web Services (AWS)
- Google Cloud
- For more information, see Deploying RHEL 9 on Amazon Web Services.
- For more information, see Deploying RHEL 9 on Google Cloud.
- For more information, see Deploying RHEL 9 on Microsoft Azure.
- Create a RHEL cloud instance on your chosen cloud platform. For more information, see Methods for creating RHEL cloud instances.
- To keep your RHEL deployment up-to-date, use Red Hat Update Infrastructure (RHUI).
1.5. Methods for creating RHEL cloud instances Copier lienLien copié sur presse-papiers!
To deploy a RHEL instance on a public cloud platform, you can use one of the following methods:
| Create a system image of RHEL and import it to the cloud platform.
|
| Purchase a RHEL instance directly from the cloud provider marketplace.
|
Chapter 2. Introduction to cloud-init Copier lienLien copié sur presse-papiers!
The cloud-init utility automates the initialization of cloud instances during system boot. You can configure cloud-init to perform a variety of tasks:
- Configuring a host name
- Installing packages on an instance
- Running scripts
- Suppressing default virtual machine (VM) behavior
Prerequisites
- Sign up for a Red Hat Customer Portal account.
The cloud-init is available in various types of RHEL images. For example:
-
If you download a KVM guest image from the Red Hat Customer Portal, the image comes preinstalled with the
cloud-initpackage. After you launch the instance, thecloud-initpackage becomes enabled. KVM guest images on the Red Hat Customer Portal are intended to use with Red Hat Virtualization (RHV), the Red Hat OpenStack Platform (RHOSP), and Red Hat OpenShift Virtualization. -
You can also download the RHEL ISO image from the Red Hat Customer Portal to create a custom guest image. In this case, you need to install the
cloud-initpackage on the customized guest image. If you require to use an image from a cloud service provider (for example, AWS or Azure), use the RHEL image builder to create the image. Image builder images are customized for specific cloud providers. The following image types include
cloud-initalready installed:- Amazon Machine Image (AMI)
- Virtual Hard Disk (VHD)
QEMU copy-on-write (qcow2)
For details about the RHEL image builder, see Composing a customized RHEL system image.
Most cloud platforms support cloud-init, but configuration procedures and supported options vary. Alternatively, you can configure cloud-init for the NoCloud environment.
In addition, you can configure cloud-init on one VM and then use that VM as a template to create additional VMs or clusters of VMs.
Specific Red Hat products, for example, Red Hat Virtualization, have documented procedures to configure cloud-init for those products.
2.1. Overview of the cloud-init configuration Copier lienLien copié sur presse-papiers!
The cloud-init utility uses YAML-formatted configuration files to apply user-defined tasks to instances. When an instance boots, the cloud-init service starts and executes the instructions from the YAML file. Depending on the configuration, tasks complete either during the first boot or on subsequent boots of the VM.
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.The file also includes default and optional modules for
cloud-init. These modules execute in order in the following phases: .. Thecloud-initinitialization phase .. The configuration phase .. The final phase.+ In the
cloud.cfgfile, the modules for the three phases are listed undercloud_init_modules,cloud_config_modules, andcloud_final_modulesrespectively.-
You can add additional directives for
cloud-initin thecloud.cfg.ddirectory. When adding directives to thecloud.cfg.ddirectory, you need to add them to a custom file named*.cfg, and always include#cloud-configat the top of the file.
2.2. cloud-init operates in stages Copier lienLien copié sur presse-papiers!
During system boot, the cloud-init utility operates in five stages that determine whether cloud-init runs and where it finds its datasources, among other tasks. The stages are as follows:
-
Generator stage: By using the
systemdservice, this phase determines whether to runcloud-initutility at the time of boot. -
Local stage:
cloud-initsearches local datasources and applies network configuration, including the DHCP-based fallback mechanism. -
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. -
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. -
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, as well as triggering configuration management plug-ins and user-defined scripts. You can add, remove, enable, or disable modules in thecloud_final_modulessection.
2.3. cloud-init modules execute in phases Copier lienLien copié sur presse-papiers!
When cloud-init runs, it executes the modules within cloud.cfg in order within three phases:
-
The network phase (
cloud_init_modules) -
The configuration phase (
cloud_config_modules) -
The final phase (
cloud_final_modules)
When cloud-init runs for the first time on a VM, all the modules you have configured run in their respective phases. On a subsequent running of cloud-init, whether a module runs within a phase depends on the module frequency of the individual module. Some modules run every time cloud-init runs; some modules only run 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.
The possible module frequency values are as follows:
-
Per instancemeans that the module runs on first boot of an instance. For example, if you clone an instance or create a new instance from a saved image, the modules designated as per instance run again. -
Per 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. cloud-init acts upon user data, metadata, and vendor data Copier lienLien copié sur presse-papiers!
The datasources that cloud-init consumes are user data, metadata, and vendor data.
-
User data includes directives you specify in the
cloud.cfgfile and in 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 platform determines where your instances find user data and metadata. Your platform may require that you add metadata and user data to an HTTP service; in this case, when
cloud-initruns it consumes metadata and user data 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.Refer to the
cloud-initDocumentation section Instance Metadata for a description of metadata; Datasources for a list of datasources; and Vendor Data for more information about vendor data.
2.5. cloud-init identifies the cloud platform Copier lienLien copié sur presse-papiers!
cloud-init attempts to identify the cloud platform using the script ds-identify. The script runs on the first boot of an instance.
Adding a 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. For example:
datasource_list:[Ec2]
datasource_list:[Ec2]
Beyond adding the directive for your cloud platform, you can further configure cloud-init by adding additional configuration details, such as metadata URLs.
datasource_list: [Ec2]
datasource:
Ec2:
metadata_urls: ['http://169.254.169.254']
datasource_list: [Ec2]
datasource:
Ec2:
metadata_urls: ['http://169.254.169.254']
After cloud-init runs, you can view a log file (run/cloud-init/ds-identify.log) that provides detailed information about the platform.
Chapter 3. Red Hat support for cloud-init Copier lienLien copié sur presse-papiers!
Red Hat supports the cloud-init utility, cloud-init modules, and default directories and files across various Red Hat products.
3.1. cloud-init significant directories and files Copier lienLien copié sur presse-papiers!
By using directories and files in the following table, you can perform tasks such as:
-
Configuring
cloud-init -
Finding information about your configuration after
cloud-inithas run - Examining log files
- Finding templates
Depending on your scenario 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 |
3.2. Red Hat products that use cloud-init Copier lienLien copié sur presse-papiers!
You can use cloud-init with these Red Hat products:
-
Red Hat Virtualization. Once you install
cloud-initon a VM, you can create a template and leveragecloud-initfunctions for all VMs created from that template. Refer to Using Cloud-Init to Automate the Configuration of Virtual Machines for information about usingcloud-initwith VMs. -
Red Hat OpenStack Platform. You can use
cloud-initto help configure images for OpenStack. Refer to the Instances and Images Guide for more information. -
Red Hat Satellite. You can use
cloud-initwith Red Hat Satellite. Refer to Preparing Cloud-init Images in Red Hat Virtualization for more information. -
Red Hat OpenShift. You can use
cloud-initwhen you create VMs for OpenShift. Refer to Creating Virtual Machines for more information.
3.3. Red Hat supports these cloud-init modules Copier lienLien copié sur presse-papiers!
Red Hat supports most cloud-init modules. Individual modules can contain multiple configuration options. In the following table, you can find all of the cloud-init modules that Red Hat currently supports and provides a brief description and the default module frequency. Refer to Modules in the cloud-init Documentation section for complete descriptions and options for these modules.
| cloud-init Module | Description | Default Module Frequency |
|---|---|---|
| bootcmd | Runs commands early in the boot process | per always |
| ca_certs | Adds CA certificates | per instance |
| debug | Enables or disables output of internal information to assist with debugging | per instance |
| disable_ec2_metadata | Enables or disables the AWS EC2 metadata | per always |
| disk_setup | Configures simple partition tables and file systems | per instance |
| final_message |
Specifies the output message once | per always |
| foo | Example shows module structure (Module does nothing) | per instance |
| growpart | Resizes partitions to fill the available disk space | per always |
| keys_to_console | Allows controls of fingerprints and keys that can be written to the console | per instance |
| landscape | Installs and configures a landscape client | per instance |
| locale | Configures the system locale and applies it system-wide | per instance |
| mcollective |
Installs, configures, and starts | per instance |
| migrator |
Moves old versions of | per always |
| mounts | Configures mount points and swap files | per instance |
| phone_home | Posts data to a remote host after boot completes | per instance |
| power_state_change | Completes shutdown and reboot after all configuration modules have run | per instance |
| puppet | Installs and configures puppet | per instance |
| resizefs | Resizes a file system to use all available space on a partition | per always |
| resolv_conf |
Configures | per instance |
| rh_subscription | Registers a Red Hat Enterprise Linux system | per instance |
| rightscale_userdata |
Adds support for RightScale configuration hooks to | per instance |
| rsyslog |
Configures remote system logging using | per instance |
| runcmd | Runs arbitrary commands | per instance |
| salt_minion |
Installs, configures, and starts | per instance |
| scripts_per_boot | Runs per boot scripts | per always |
| scripts_per_instance | Runs per instance scripts | per instance |
| scripts_per_once | Runs scripts once | per once |
| scripts_user | Runs user scripts | per instance |
| scripts_vendor | Runs vendor scripts | per instance |
| seed_random | Provides random seed data | per instance |
| set_hostname | Sets host name and fully qualified domain name (FQDN) | per always |
| set_passwords | Sets user passwords and enables or disables SSH password authentication | per instance |
| ssh_authkey_fingerprints | Logs fingerprints of user SSH keys | per instance |
| ssh_import_id | Imports SSH keys | per instance |
| ssh | Configures SSH, and host and authorized SSH keys | per instance |
| timezone | Sets the system time zone | per instance |
| update_etc_hosts |
Updates | per always |
| update_hostname | Updates host name and FQDN | per always |
| users_groups | Configures users and groups | per instance |
| write_files | Writes arbitrary files | per instance |
| yum_add_repo | Adds dnf repository configuration to the system | per always |
The following list of modules is not supported by Red Hat:
| Module |
|---|
| apt_configure |
| apt_pipeline |
| byobu |
| chef |
| emit_upstart |
| grub_dpkg |
| ubuntu_init_switch |
3.4. The default cloud.cfg file Copier lienLien copié sur presse-papiers!
The /etc/cloud/cloud.cfg file lists the modules comprising the basic configuration for cloud-init.
The modules in the file are the default modules for cloud-init. You can configure the modules for your environment or remove modules you do not need. Modules that are included in cloud.cfg do not necessarily do anything by being listed in the file. You need to configure them individually if you want them to perform actions during one of the cloud-init phases.
The cloud.cfg file provides the chronology for running individual modules. You can add additional modules to cloud.cfg as long as Red Hat supports the modules you want to add.
The default contents of the file for Red Hat Enterprise Linux (RHEL) are as follows:
-
Modules run in the order given in
cloud.cfg. You typically do not change this order. -
The
cloud.cfgdirectives can be overridden by user data. -
When running
cloud-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, prefer using module names with underscores (
_) rather than dashes (-).
Notable parameters
-
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 containscloud-init-specific subdirectories. For more information, see Directory layout. -
templates_dirspecifies where templates are located. -
ssh_svcnameis the name of the SSH service.
3.5. The cloud.cfg.d directory Copier lienLien copié sur presse-papiers!
cloud-init acts upon directives that you provide and configure. Typically, those directives are included in the cloud.cfg.d directory.
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.
There are multiple ways to add directives. You can include directives in a file named *.cfg, which includes the heading #cloud-config. Typically, the directory would contain multiple *cfg files. There are other options for adding directives, for example, you can add a user data script. Refer to User-Data Formats for more information.
3.6. The default 05_logging.cfg file Copier lienLien copié sur presse-papiers!
The 05_logging.cfg file sets logging information for cloud-init. The /etc/cloud/cloud.cfg.d directory includes this file, along with other cloud-init directives that you add.
cloud-init uses the logging configuration in 05_logging.cfg by default. The default contents of the file for Red Hat Enterprise Linux (RHEL) are as follows:
3.7. The cloud-init /var/lib/cloud directory layout Copier lienLien copié sur presse-papiers!
When cloud-init first runs, it creates a directory layout that includes information about your instance and cloud-init configuration.
The directory can include optional directories, such as /scripts/vendor.
The following is a sample directory layout for cloud-init:
Chapter 4. Configuring cloud-init Copier lienLien copié sur presse-papiers!
By using cloud-init, you can perform a variety of configuration tasks. Your cloud-init configuration can require that you add directives to the cloud.cfg file and the cloud.cfg.d directory. Alternatively, your specific data source might require that you add directives to files, such as a user data file and a metadata file. A data source might require that you upload your directives to an HTTP server. Check the requirements of your data source and add directives accordingly.
4.1. Creating a virtual machine that includes cloud-init for a NoCloud datasource Copier lienLien copié sur presse-papiers!
To create a new virtual machine (VM) that includes cloud-init, create a meta-data file and a user-data file.
-
The
meta-datafile includes instance details. -
The
user-datafile includes information to create a user and grant access.
Include these files in a new ISO image, and attach the ISO file to a new VM created from a KVM Guest Image. In this scenario, the datasource is NoCloud.
Procedure
Create a directory named
cloudinitisoand set is as your working directory:mkdir cloudinitiso cd cloudinitiso
$ mkdir cloudinitiso $ cd cloudinitisoCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
meta-datafile and add the following information:instance-id: citest local-hostname: citest-1
instance-id: citest local-hostname: citest-1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
user-datafile and add the following information:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe last line of the
user-datafile references an SSH public key. Find your SSH public keys in~/.ssh/id_rsa.pub. When trying this sample procedure, modify the line to include one of your public keys.Use the
genisoimagecommand to create an ISO image that includesuser-dataandmeta-data:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Download a KVM Guest Image from the Red Hat Customer Portal to the
/var/lib/libvirt/imagesdirectory. Create a new VM from the KVM Guest Image using the
virt-installutility and attach the downloaded image to the existing image:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Log on to your image with username
cloud-userand passwordcilogon:citest-1 login: cloud-user Password: [cloud-user@citest-1 ~]$
citest-1 login: cloud-user Password: [cloud-user@citest-1 ~]$Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Check the
cloud-initstatus to confirm that the utility has completed its defined tasks:cloud-init status status: done
[cloud-user@citest-1 instance]$ cloud-init status status: doneCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
cloud-initutility creates thecloud-initdirectory layout under/var/lib/cloudwhen it runs, and it updates or changes certain directory contents based upon the directives you have specified.For example, you can confirm that the datasource is
NoCloudby checking the datasource file.cd /var/lib/cloud/instance cat datasource DataSourceNoCloud: DataSourceNoCloud [seed=/dev/sr0][dsmode=net]
$ cd /var/lib/cloud/instance $ cat datasource DataSourceNoCloud: DataSourceNoCloud [seed=/dev/sr0][dsmode=net]Copy to Clipboard Copied! Toggle word wrap Toggle overflow cloud-initcopies user-data into/var/lib/cloud/instance/user-data.txt:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
For OpenStack, the Creating and managing instances includes information for configuring an instance using cloud-init. See Creating a customized instance for specific procedures.
4.2. Expiring a cloud user password with cloud-init Copier lienLien copié sur presse-papiers!
To force cloud-user to change the password at the first login, you can set the password as expired.
Procedure
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.Change the line
chpasswd: {expire: False}tochpasswd: {expire: True}:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This step expires the password because
passwordandchpasswdoperate on the default user unless you specify.NoteThis is a global setting. When you set
chpasswdtoTrue, all users you create need to change their passwords when they log in.
4.3. Changing a default user name with cloud-init Copier lienLien copié sur presse-papiers!
You can change the default user name from cloud-user to another user.
Procedure
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.Add the line
user: <username>, replacing <username> with the new default user name:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.4. Setting a root password with cloud-init Copier lienLien copié sur presse-papiers!
To set the root password, create a user list.
Procedure
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.Create a user list in the
chpasswdsection of the file:NoteWhite space is significant. Do not include white space before or after the colon in your user list. If you include white space, the password will set with a space.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou must set the
all passwordsto set the user password.
4.5. Managing Red Hat subscriptions with cloud-init Copier lienLien copié sur presse-papiers!
You can use the rh_subscription directive to register your system. For each subscription, you need to edit user data.
- Using the
defaultoption Under
rh_subscription, add yourusernameandpassword.rh_subscription: username: <example@redhat.com> password: <example_password>
rh_subscription: username: <example@redhat.com> password: <example_password>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Using the
activation-keyandorgoptions Under
rh_subscription, add youractivation keyandorgnumber.rh_subscription: activation-key: <example_key> org: <example_id>
rh_subscription: activation-key: <example_key> org: <example_id>Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Using the
server-hostnameoption You can set a server hostname in the
/etc/rhsm/rhsm.conffile:Under
rh_subscription, add yourusername,password, andserver-hostname.rh_subscription: username: <example@redhat.com> password: <example_password> server-hostname: <test.example.com>
rh_subscription: username: <example@redhat.com> password: <example_password> server-hostname: <test.example.com>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.6. Adding users and user options with cloud-init Copier lienLien copié sur presse-papiers!
You create and describe users in a users section. You can change the section to add more users to your initial system configuration, and you can set additional user options.
If you add the users section, you must also set the default user option.
Procedure
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.Add or change the
userssection to add users.-
If you want
cloud-userto be the default user created along with the other users you specify, ensure that you adddefaultas the first entry in the section. If it is not the first entry,cloud-useris not created. By default, users are labeled as
unconfined_uif there is noselinux-uservalue.Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe example places the user
user2into theusersandwheelgroups.
-
If you want
4.7. Running first boot commands with cloud-init Copier lienLien copié sur presse-papiers!
You can use the runcmd and bootcmd sections to run commands during startup and initialization. The bootcmd section executes early in the initialization process and by default runs on every boot. The runcmd section executes near the end of the process and is only executed during the first boot.
Procedure
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.Add the sections for
bootcmdandruncmdand include commands forcloud-initto run:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.8. Adding additional sudoers with cloud-init Copier lienLien copié sur presse-papiers!
You can configure a user as a sudoer by adding a sudo and groups entry to the users section.
Procedure
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.-
Add a
sudoentry and specify the user access. For example,sudo: ALL=(ALL) NOPASSWD:ALLallows a user unrestricted user access. Add a
groupsentry and specify the groups that include the user:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.9. Setting up a static networking configuration with cloud-init Copier lienLien copié sur presse-papiers!
You can set up network configuration with cloud-init by adding a network-interfaces section to the metadata.
NetworkManager is a dynamic network control and configuration daemon that keeps network devices and connections up and active when they are available. Your datasource might offer a network configuration. For details, see the cloud-init section Network Configuration Sources.
If you do not specify network configuration for cloud-init and have not disabled network configuration, cloud-init searches for connections with the attached devices.
If it finds a connected device, it generates a network configuration that issues a DHCP request on the interface. Refer to the cloud-init documentation section Fallback Network Configuration for more information.
Procedure
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.Add a
network-interfacessection.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Disable a network configuration by adding details to the metadata:
network: config: disabled
network: config: disabledCopy to Clipboard Copied! Toggle word wrap Toggle overflow
4.10. Configuring only a root user with cloud-init Copier lienLien copié sur presse-papiers!
You can configure your user data so that you have only a root user.
Procedure
Depending on the requirements of the datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.Create an entry for the user
rootin theuserssection.The simple example that follows includes a
userssection with only thenameoption.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Set up SSH keys for the root user.
users: - name: root ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AA..vz user@domain.comusers: - name: root ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AA..vz user@domain.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow
4.11. Setting up storage with container-storage-setup in cloud-init Copier lienLien copié sur presse-papiers!
You can set up storage by referencing the container-storage-setup utility within the write_files module.
Procedure
Depending on the requirements of your datasource, edit the
user-datafile or add the following directive to thecloud.cfg.ddirectory:NoteAll user directives include
#cloud-configat the top of the file so thatcloud-initrecognizes the file as containing user directives. When you include directives in thecloud.cfg.ddirectory, name the file*.cfg, and always include#cloud-configat the top of the file.Add or change the
write_filesmodule to include the path to thecontainer-storage-setuputility.The following example sets the size of the root logical volume to 6 GB rather than the default 3 GB.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteBefore RHEL 7.4, container-storage-setup was formerly known as docker-storage-setup. If you are using OverlayFS for storage, as of RHEL 7.4 you can now use that type of file system with SELinux in enforcing mode.
4.12. Changing the system locale with cloud-init Copier lienLien copié sur presse-papiers!
You can configure the system location with the locale module.
Procedure
-
Depending on the requirements of your datasource, edit the
meta-datafile. You can also add the following directive to thecloud.cfgfile or thecloud.cfg.ddirectory. Add the
localedirective, specifying the location. The following sample sets thelocaletoja_JP(Japan) withUTF-8encoding.#cloud-config locale: ja_JP.UTF-8
#cloud-config locale: ja_JP.UTF-8Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.13. cloud-init and shell scripts Copier lienLien copié sur presse-papiers!
You can add list values or string values to bootcmd or runcmd. You can also provide a shell script within userdata.
-
If you use a list value for
bootcmdorruncmd, each list item runs in turn usingexecve. - If you use a string value, then the entire string runs as a shell script.
-
If you want to use
cloud-initto run a shell script, you can provide a shell script (complete with shebang (#!) ) instead of providingcloud-initwith a.yamlfile.
Refer to Run commands on first boot for examples of how to put shell scripts in bootcmd and runcmd.
4.14. Preventing cloud-init from updating config files Copier lienLien copié sur presse-papiers!
When you create or restore an instance from a backup image, the instance ID changes. With the change in the instance ID, the cloud-init utility updates configuration files. However, you can ensure that cloud-init does not update certain configuration files when you create or restore from backup.
Procedure
Edit the
/etc/cloud/cloud.cfgfile, for example:vi /etc/cloud/cloud.cfg
# vi /etc/cloud/cloud.cfgCopy to Clipboard Copied! Toggle word wrap Toggle overflow Comment out or remove the configuration that you do not want
cloud-initto update when you restore your instance. For example, to avoid updating the SSH key file, remove-sshfrom thecloud_init_modulessection.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To check the configuration files updated by
cloud-init, examine the/var/log/cloud/cloud-init.logfile. Updated files are logged during instance startup with messages beginning withWriting to. For example:2019-09-03 00:16:07,XXX - util.py[DEBUG]: Writing to /root/.ssh/authorized_keys - wb: [XXX] 554 bytes 2019-09-03 00:16:08,XXX - util.py[DEBUG]: Writing to /etc/ssh/sshd_config - wb: [XXX] 3905 bytes
2019-09-03 00:16:07,XXX - util.py[DEBUG]: Writing to /root/.ssh/authorized_keys - wb: [XXX] 554 bytes 2019-09-03 00:16:08,XXX - util.py[DEBUG]: Writing to /etc/ssh/sshd_config - wb: [XXX] 3905 bytesCopy to Clipboard Copied! Toggle word wrap Toggle overflow
4.15. Modifying a VM created from a KVM Guest Image after cloud-init has run Copier lienLien copié sur presse-papiers!
You can modify your cloud-init configuration before rerunning the cloud-init utility. When you launch a VM with the cloud-init package installed and enabled, cloud-init runs in its default state on the initial boot of the VM.
Procedure
- Log in to your VM.
-
Add or change directives, for example, modify the
cloud.cfgfile in the/etc/clouddirectory or add directives to the/etc/cloud/cloud.cfg.ddirectory. Run the
cloud-init cleancommand to clean directories so thatcloud-initcan rerun. You can also run the following commands as root to clean the VM:rm -Rf /var/lib/cloud/instances/ rm -Rf /var/lib/cloud/instance rm -Rf /var/lib/cloud/data/
rm -Rf /var/lib/cloud/instances/ rm -Rf /var/lib/cloud/instance rm -Rf /var/lib/cloud/data/Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou can save the cleaned image as a new image and use that image for multiple VMs. The new VMs will use updated
cloud-initconfiguration to runcloud-init.Rerun
cloud-initor reboot the VM.cloud-initreruns, implementing the configuration changes you made.
4.16. Modifying a VM for a specific datasource after cloud-init has run Copier lienLien copié sur presse-papiers!
You can modify your cloud-init configuration before rerunning cloud-init. This procedure uses OpenStack as an example datasource. Note that the exact steps you need to perform vary based on your datasource.
Procedure
-
Create and launch an instance for the OpenStack Platform. For information about creating instances for OpenStack, see Creating an instance. In this example, the virtual machine (VM) includes
cloud-init, which runs upon boot of the VM. -
Add or change directives. For example, modify the
user-data.filefile that is stored on the OpenStack HTTP server. Clean the virtual machine. Run the following commands as root.
rm -rf /etc/resolv.conf /run/cloud-init userdel -rf cloud-user hostnamectl set-hostname localhost.localdomain rm /etc/NetworkManager/conf.d/99-cloud-init.conf
# rm -rf /etc/resolv.conf /run/cloud-init # userdel -rf cloud-user # hostnamectl set-hostname localhost.localdomain # rm /etc/NetworkManager/conf.d/99-cloud-init.confCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou can save the cleaned image as a new image and use that image for multiple virtual machines. The new virtual machines run
cloud-init, using your updatedcloud-initconfiguration.Rerun
cloud-initor reboot the virtual machine.Cloud-initreruns, implementing the configuration changes you made.
4.17. Troubleshooting cloud-init Copier lienLien copié sur presse-papiers!
After running the cloud-init utility, you can troubleshoot the instance by examining the configuration and log files. After identifying the issue, rerun cloud-init on your instance. You can run cloud-init from the command line. For details, run the cloud-init --help command.
Procedure
Review the
cloud-initconfiguration files:-
Examine the
/etc/cloud/cloud.cfgconfiguration file. Check which modules are included undercloud_init_modules,cloud_config_modules, andcloud_final_modules. -
Check directives (
*.cfgfiles) in the/etc/cloud/cloud.cfg.ddirectory.
-
Examine the
Review the
/var/log/cloud-init.logand/var/log/cloud-init-output.logfiles for details on a specific issue. For example, if the root partition was not automatically extended, check log messages for thegrowpartutility. If the file system was not extended, check log messages forresizefs. For example:grep resizefs /var/log/cloud-init.log
# grep resizefs /var/log/cloud-init.logCopy to Clipboard Copied! Toggle word wrap Toggle overflow Notegrowpartdoes not support LVM. If your root partition is based in LVM, the root partition is not automatically extended upon first boot.Rerun
cloud-initcommands as root:Rerun
cloud-initwith only the init modules:/usr/bin/cloud-init -d init
# /usr/bin/cloud-init -d initCopy to Clipboard Copied! Toggle word wrap Toggle overflow Rerun
cloud-initwith all modules in the configuration:/usr/bin/cloud-init -d modules
# /usr/bin/cloud-init -d modulesCopy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the
cloud-initcache and forcecloud-initto run after boot:rm -rf /var/lib/cloud/ && /usr/bin/cloud-init -d init
# rm -rf /var/lib/cloud/ && /usr/bin/cloud-init -d initCopy to Clipboard Copied! Toggle word wrap Toggle overflow Clean directories and simulate a clean instance:
rm -rf /var/lib/cloud/instances/ rm -rf /var/lib/cloud/instance rm -rf /var/lib/cloud/data/ reboot
# rm -rf /var/lib/cloud/instances/ # rm -rf /var/lib/cloud/instance # rm -rf /var/lib/cloud/data/ # rebootCopy to Clipboard Copied! Toggle word wrap Toggle overflow Rerun the
cloud-initutility:cloud-init init --local cloud-init init
# cloud-init init --local # cloud-init initCopy to Clipboard Copied! Toggle word wrap Toggle overflow