Questo contenuto non è disponibile nella lingua selezionata.
Chapter 4. Configuring cloud-init
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 Copia collegamentoCollegamento copiato negli appunti!
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
[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
$ 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 Copia collegamentoCollegamento copiato negli appunti!
To force cloud-user to change the cloud-user password at the first login, you can set their 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 works to expire the password because
passwordandchpasswdoperate on the default user unless you indicate otherwise.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 Copia collegamentoCollegamento copiato negli appunti!
You can change the default user name to something other than cloud-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 Copia collegamentoCollegamento copiato negli appunti!
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 is set with a space in it.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you use this method to set the user password, you must set all passwords in this section.
4.5. Managing Red Hat subscriptions with cloud-init Copia collegamentoCollegamento copiato negli appunti!
You can use the rh_subscription directive to register your system. For each subscription, you need to edit user data.
Example 1
You can use the
auto-attachandservice-leveloptions:Under
rh_subscription, add yourusernameandpassword, setauto-attachtoTrue, and setservice-leveltoself-support.rh_subscription: username: sample@redhat.com password: 'mypassword' auto-attach: True service-level: self-support
rh_subscription: username: sample@redhat.com password: 'mypassword' auto-attach: True service-level: self-supportCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe
service-leveloption requires that you use theauto-attachoption.
Example 2
You can use the
activation-keyandorgoptions:Under
rh_subscription, add youractivation keyandorgnumber and setauto-attachtoTrue.rh_subscription: activation-key: example_key org: 12345 auto-attach: True
rh_subscription: activation-key: example_key org: 12345 auto-attach: TrueCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Example 3
You can add a subscription pool:
Under
rh_subscription, add yourusername,password, and pool number.rh_subscription: username: sample@redhat.com password: 'password' add-pool: XYZ01234567
rh_subscription: username: sample@redhat.com password: 'password' add-pool: XYZ01234567Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThis sample is the equivalent of the
subscription-manager attach --pool=XYZ01234567command.
Example 4
You can set a server host name in the
/etc/rhsm/rhsm.conffile:Under
rh_subscription, add yourusername,password,server-hostname, and setauto-attachtoTrue.rh_subscription: username: sample@redhat.com password: 'password' server-hostname: test.example.com auto-attach: True
rh_subscription: username: sample@redhat.com password: 'password' server-hostname: test.example.com auto-attach: TrueCopy to Clipboard Copied! Toggle word wrap Toggle overflow
4.6. Adding users and user options with cloud-init Copia collegamentoCollegamento copiato negli appunti!
You create and describe users in a users section. You can modify 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 options in this 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 or modify 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 not anselinux-uservalue.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note-
The example places the user
user2into two groups,usersandwheel.
-
The example places the user
-
If you want
4.7. Running first boot commands with cloud-init Copia collegamentoCollegamento copiato negli appunti!
You can use the runcmd and bootcmd sections to execute 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 and initialization.
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
bootcmdandruncmd; include commands you wantcloud-initto execute.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.8. Adding additional sudoers with cloud-init Copia collegamentoCollegamento copiato negli appunti!
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 Copia collegamentoCollegamento copiato negli appunti!
You can set up network configuration with cloud-init by adding a network-interfaces section to the metadata.
Red Hat Enterprise Linux provides its default networking service through NetworkManager, a dynamic network control and configuration daemon that keeps network devices and connections up and active when they are available.
Your datasource might provide 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 tries to determine if any attached devices have a connection. 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
The following example adds a static networking configuration.
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
You can disable a network configuration by adding the following information to your metadata.
network: config: disabled
network:
config: disabled
4.10. Configuring only a root user with cloud-init Copia collegamentoCollegamento copiato negli appunti!
You can configure your user data so that you have a root user and no other users.
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 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 Copia collegamentoCollegamento copiato negli appunti!
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 modify 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 NotePrior to RHEL 7.4, container-storage-setup was called 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 Copia collegamentoCollegamento copiato negli appunti!
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-8
4.13. cloud-init and shell scripts Copia collegamentoCollegamento copiato negli appunti!
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 Copia collegamentoCollegamento copiato negli appunti!
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 Copia collegamentoCollegamento copiato negli appunti!
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 Copia collegamentoCollegamento copiato negli appunti!
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 Copia collegamentoCollegamento copiato negli appunti!
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