### General Disk Setup Example Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/disk_setup.rst A general example demonstrating the structure of the `disk_setup` directive. ```yaml #cloud-config disk_setup: ephemeral0: table_type: 'mbr' layout: true /dev/xvdh: table_type: 'mbr' layout: - 33 - [33, 82] - 33 overwrite: True ``` -------------------------------- ### Configure Ubuntu fan networking with cloud-init Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/fan.rst This example installs the ubuntu-fan package, writes the configuration path, and starts or restarts the service. Refer to the Fan module schema for a full list of keys. ```yaml package: ['ubuntu-fan'] runcmd: - echo "/etc/fan/fan.conf" > /etc/fan/fan.conf - systemctl restart fan ``` -------------------------------- ### Install and Run Chef Recipes (Example 1) Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/chef.rst This example installs the Chef client and runs specified recipes on instance boot. It is intended to be passed as user data. The default installation method is from packages. ```yaml #cloud-config chef: chef_license: accept environment: _default run_list: ['recipe[learn-chef]'] ``` -------------------------------- ### Disk Setup with Cloud-Config Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/examples.rst Defines the setup for disks, including creating partitions and filesystems. This example sets up a GPT partition table and a filesystem. ```yaml disk_setup: # Example: Partitioning a disk disks: /dev/sdb: layout: - [100, "free", "%" ] # Example: Creating a filesystem filesystems: - "/dev/sdb1" # Example: Mounting the filesystem mounts: - ["/dev/sdb1", "/data", "ext4", "defaults,nofail", "0", "2"] ``` -------------------------------- ### Install and Run Chef Recipes with Custom Configuration (Example 2) Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/chef.rst This example configures the Chef client with various settings including custom APT sources, license acceptance, environment, initial attributes, node name, and run list. It demonstrates advanced configuration options for Chef installation via packages. ```yaml #cloud-config apt: sources: source1: source: "deb http://packages.chef.io/repos/apt/stable $RELEASE main" key: | -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.12 (Darwin) Comment: GPGTools - http://gpgtools.org mQGiBEppC7QRBADfsOkZU6KZK+YmKw4wev5mjKJEkVGlus+NxW8wItX5sGa6kdUu twAyj7Yr92rF+ICFEP3gGU6+lGo0Nve7KxkN/1W7/m3G4zuk+ccIKmjp8KS3qn99 dxy64vcji9jIllVa+XXOGIp0G8GEaj7mbkixL/bMeGfdMlv8Gf2XPpp9vwCgn/GC JKacfnw7MpLKUHOYSlb//JsEAJqao3ViNfav83jJKEkD8cf59Y8xKia5OpZqTK5W ShVnNWS3U5IVQk10ZDH97Qn/YrK387H4CyhLE9mxPXs/ul18ioiaars/q2MEKU2I XKfV21eMLO9LYd6Ny/Kqj8o5WQK2J6+NAhSwvthZcIEphcFignIuobP+B5wNFQpe DbKfA/0WvN2OwFeWRcmmd3Hz7nHTpcnSF+4QX6yHRF/5BgxkG6IqBIACQbzPn6Hm sMtm/SVf11izmDqSsQptCrOZILfLX/mE+YOl+CwWSHhl+YsFts1WOuh1EhQD26aO Z84HuHV5HFRWjDLw9LriltBVQcXbpfSrRP5bdr7Wh8vhqJTPjrQnT3BzY29kZSBQ YWNrYWdlcyA8cGFja2FnZXNAb3BzY29kZS5jb20+iGAEExECACAFAkppC7QCGwMG CwkIBwMCBBUCCAMEFgIDAQIeAQIXgAAKCRApQKupg++Caj8sAKCOXmdG36gWji/K +o+XtBfvdMnFYQCfTCEWxRy2BnzLoBBFCjDSK6sJqCu0IENIRUYgUGFja2FnZXMg PHBhY2thZ2VzQGNoZWYuaW8+iGIEExECACIFAlQwYFECGwMGCwkIBwMCBhUIAgkK CwQWAgMBAh4BAheAAAoJEClAq6mD74JqX94An26z99XOHWpLN8ahzm7cp13t4Xid AJ9wVcgoUBzvgg91lKfv/34cmemZn7kCDQRKaQu0EAgAg7ZLCVGVTmLqBM6njZEd Zbv+mZbvwLBSomdiqddE6u3eH0X3GuwaQfQWHUVG2yedyDMiG+EMtCdEeeRebTCz SNXQ8Xvi22hRPoEsBSwWLZI8/XNg0n0f1+GEr+mOKO0BxDB2DG7DA0nnEISxwFkK OFJFebR3fRsrWjj0KjDxkhse2ddU/jVz1BY7Nf8toZmwpBmdozETMOTx3LJy1HZ/ Te9FJXJMUaB2lRyluv15MVWCKQJro4MQG/7QGcIfrIZNfAGJ32DDSjV7/YO+IpRY IL4CUBQ65suY4gYUG4jhRH6u7H1p99sdwsg5OIpBe/v2Vbc/tbwAB+eJJAp89Zeu twADBQf/ZcGoPhTGFuzbkcNRSIz+boaeWPoSxK2DyfScyCAuG41CY9+g0HIw9Sq8 DuxQvJ+vrEJjNvNE3EAEdKl/zkXMZDb1EXjGwDi845TxEMhhD1dDw2qpHqnJ2mtE WpZ7juGwA3sGhi6FapO04tIGacCfNNHmlRGipyq5ZiKIRq9mLEndlECr8cwaKgkS 0wWu+xmMZe7N5/t/TK19HXNh4tVacv0F3fYK54GUjt2FjCQV75USnmNY4KPTYLXA dzC364hEMlXpN21siIFgB04w+TXn5UF3B4FfAy5hevvr4DtV4MvMiGLu0oWjpaLC MpmrR3Ny2wkmO0h+vgri9uIP06ODWIhJBBgRAgAJBQJKaQu0AhsMAAoJEClAq6mD 74Jq4hIAoJ5KrYS8kCwj26SAGzglwggpvt3CAJ0bekyky56vNqoegB+y4PQVDv4K zA== =IxPr -----END PGP PUBLIC KEY BLOCK----- chef: chef_license: accept encrypted_data_bag_secret: /etc/chef/encrypted_data_bag_secret environment: production force_install: false initial_attributes: apache: prefork: {maxclients: 100} keepalive: false install_type: packages node_name: your-node-name omnibus_url: https://www.chef.io/chef/install.sh omnibus_version: 12.3.0 run_list: ['recipe[apache2]', 'role[db]'] server_url: https://chef.yourorg.com validation_name: yourorg-validator validation_cert: | -----BEGIN RSA PRIVATE KEY----- YOUR-ORGS-VALIDATION-KEY-HERE -----END RSA PRIVATE KEY----- ``` -------------------------------- ### Cloud-Init Analyze Show Example Output Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/explanation/analyze.rst Illustrates the output format of the 'cloud-init analyze show' command, detailing the start time and duration of events within different boot stages. It includes a note on interpreting the time values. ```text -- Boot Record 01 -- The total time elapsed since completing an event is printed after the "@" character. The time the event takes is printed after the "+" character. Starting stage: init-local |``->no cache found @00.01700s +00.00200s ``` -------------------------------- ### Install LXD Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/tutorial/lxd.rst Installs LXD using snap. This is a prerequisite for the tutorial if LXD is not already set up. ```shell $ sudo snap install lxd ``` -------------------------------- ### Simple Cloud Boothook Example Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/explanation/format/boothook.rst A basic cloud-boothook script to modify the hosts file. Ensure the script starts with '#cloud-boothook'. ```shell #cloud-boothook #!/bin/sh echo 192.168.1.130 us.archive.ubuntu.com > /etc/hosts ``` -------------------------------- ### Example mounts configuration Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/mounts.rst Provides a detailed example of configuring various mount points with specific options and fstab fields. ```yaml mounts: - [ ephemeral0, /mnt, auto, "defaults,noexec" ] - [ sdc, /opt/data ] - [ xvdh, /opt/data, "auto", "defaults,nofail", "0", "0" ] - [ dd, /dev/zero ] ``` -------------------------------- ### Install and Configure MCollective Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/mcollective.rst This example demonstrates the configuration of MCollective, including server private and public keys, and settings for the server.cfg file. Refer to the MCollective module schema for a full list of keys. ```yaml --- # Example MCollective configuration mcollective::server::stomp_host: "127.0.0.1" mcollective::server::stomp_port: 61613 mcollective::server::stomp_user: "mcollective" mcollective::server::stomp_password: "secret" # Example server private and public keys mcollective::server::private_key: | -----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAy+j+... -----END RSA PRIVATE KEY----- mcollective::server::public_key: | -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy+j+... -----END PUBLIC KEY----- # Other configuration settings mcollective::server::daemon_user: "mcollective" mcollective::server::daemon_group: "mcollective" mcollective::server::log_level: "info" mcollective::server::pluginconf: "activemq.yaml": "pool.1.host": "127.0.0.1" "pool.1.port": 61613 "pool.1.username": "mcollective" "pool.1.password": "secret" "pool.1.ssl": "false" "pool.1.virtualhost": "/" ``` -------------------------------- ### Set Hostname Example 2 Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/set_hostname.rst Example for setting both hostname and FQDN. ```yaml hostname: my-server fqdn: my-server.example.com ``` -------------------------------- ### Route Configuration Example Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/network-config-format-v1.rst Example of how to define static routes with destination, gateway, and metric. ```yaml network: version: 1 routes: - to: "192.168.10.0/24" via: "192.168.1.1" metric: 100 ``` -------------------------------- ### Install QEMU on Ubuntu Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/tutorial/qemu.rst Installs the QEMU system emulator for x86 architecture on Ubuntu systems. Use this command if you do not have QEMU installed. ```bash $ sudo apt install qemu-system-x86 ``` -------------------------------- ### Set Hostname Example 1 Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/set_hostname.rst Basic example for setting the hostname. ```yaml hostname: my-server ``` -------------------------------- ### Grow partition example 1 Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/disk_setup.rst This example demonstrates how to grow a partition using the growpart module. It specifies the device and the partition number to be grown. ```yaml #cloud-config cc_growpart: - device: /dev/sda partition: 1 ``` -------------------------------- ### Install Arbitrary Packages with cloud-init Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/package_update_upgrade.rst Use the 'packages' key to specify a list of packages to install. You can install packages by name or by name and specific version. ```yaml #cloud-config packages: - pwgen - pastebinit - [libpython2.7, 2.7.3-0ubuntu3.1] ``` -------------------------------- ### Nameserver Configuration Example Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/network-config-format-v1.rst Example of how to define DNS nameservers, including address, search paths, and interface binding. ```yaml network: version: 1 ethernets: eno1: dhcp4: true nameservers: address: ["8.8.8.8", "8.8.4.4"] search: ["example.com", "test.com"] # interface: eno1 ``` -------------------------------- ### Configure Ansible Controller with Cloud-Init Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/ansible_controller.rst This cloud-config example sets up an instance to act as an Ansible controller. It installs necessary packages, creates a dedicated user with sudo privileges, initializes LXD, and configures Ansible to run playbooks from a private Git repository using a deploy key. ```yaml #cloud-config # Update, upgrade and install packages # ------------------------------------ package_update: true package_upgrade: true packages: ['git', 'python3-pip'] # Set up an Ansible user # ---------------------- # We give the local Ansible user password-less ``sudo`` so that Ansible can # write to a local root-only file. users: - name: ansible gecos: Ansible User shell: /bin/bash groups: users,admin,wheel,lxd sudo: "ALL=(ALL) NOPASSWD:ALL" # Initialize LXD using cloud-init # ------------------------------- # A LXD container is started (using Ansible) on boot, so we must # initialize LXD. lxd: init: storage_backend: dir # Configure and run Ansible on boot # --------------------------------- # First we install Ansible using ``pip``, and ensure that the # ``community.general`` collection is installed (it is likely to be already # installed by ``pip``). # Then we use a deploy key to clone a remote private repository and run two # playbooks: # * The first starts a LXD container and creates a new inventory file # * The second connects to and configures the container using Ansible # The public version of the playbooks can be inspected at this URL: # ``https://github.com/holmanb/ansible-lxd-public`` ansible: install_method: pip package_name: ansible run_user: ansible galaxy: actions: ['ansible-galaxy', 'collection', 'install', 'community.general'] setup_controller: repositories: - path: /home/ansible/my-repo/ source: git@github.com:holmanb/ansible-lxd-private.git run_ansible: - playbook_dir: /home/ansible/my-repo playbook_name: start-lxd.yml timeout: 120 forks: 1 private_key: /home/ansible/.ssh/id_rsa - playbook_dir: /home/ansible/my-repo playbook_name: configure-lxd.yml become_user: ansible timeout: 120 forks: 1 private_key: /home/ansible/.ssh/id_rsa inventory: new_ansible_hosts # Write a deploy key to the filesystem for Ansible # ------------------------------------------------ # This deploy key is tied to a `private GitHub repository`_. It exists to # demonstrate how deploy keys are used in Ansible. A duplicate public copy # of the repository `exists here`_. write_files: - path: /home/ansible/.ssh/known_hosts owner: ansible:ansible permissions: 0o600 defer: true content: | |1|YJEFAk6JjnXpUjUSLFiBQS55W9E=|OLNePOn3eBa1PWhBBmt5kXsbGM4= ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl |1|PGGnpCpqi0aakERS4BWnYxMkMwM=|Td0piZoS4ZVC0OzeuRwKcH1MusM= ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ== |1|OJ89KrsNcFTOvoCP/fPGKpyUYFo=|cu7mNzF+QB/5kR0spiYmUJL7DAI= ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg= - path: /home/ansible/.ssh/id_rsa owner: ansible:ansible permissions: 0o600 defer: true encoding: base64 content: | LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFB QUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUJsd0FBQUFkemMyZ3RjbgpOaEFBQUFB d0VBQVFBQUFZRUEwUWlRa05WQS9VTEpWZzBzT1Q4TEwyMnRGckg5YVR1SWFNT1FiVFdtWjlNUzJh VTZ0cDZoClJDYklWSkhmOHdsaGV3MXNvWmphWVVQSFBsUHNISm5UVlhJTnFTTlpEOGF0Rldjd1gy ZTNBOElZNEhpN0NMMDE3MVBoMVUKYmJGNGVIT1JaVkY2VVkzLzhmbXQ3NmhVYnpiRVhkUXhQdVdh a0IyemxXNTdFclpOejJhYVdnY2pJUGdHV1RNZWVqbEpOcQpXUW9MNlFzSStpeUlzYXNMc1RTajha aVgrT1VjanJEMUY4QXNKS3ZWQStKbnVZNUxFeno1TGQ2SGxGc05XVWtoZkJmOWVOClpxRnJCc1Vw M2VUY1FtejFGaHFFWDJIQjNQT3VSTzlKemVGcTJaRE8wUlNQN09acjBMYm8vSFVTK3V5VkJNTDNi eEF6dEIKQWM5dFJWZjRqcTJuRjNkcUpwVTFFaXZzR0sxaHJZc0VNQklLK0srVzRwc1F5c3ZTL0ZK V2lXZmpqWVMwei9IbkV4MkpHbApOUXUrYkMxL1dXSGVXTGFvNGpSckRSZnNIVnVscTE2MElsbnNx ``` -------------------------------- ### Puppet Example 1 Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/puppet.rst This is the first example of a Puppet configuration. ```yaml --- - name: Install Apache puppet: package: apache2 service: apache2 state: present - name: Configure Apache puppet: template: apache2.conf.erb source: templates/apache2.conf.erb dest: /etc/apache2/apache2.conf owner: root group: root mode: '0644' - name: Ensure Apache is running puppet: service: apache2 state: running enabled: true ``` -------------------------------- ### Grow partition example 2 Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/disk_setup.rst Another example of growing a partition, potentially with different parameters or for a different device/partition combination. ```yaml #cloud-config cc_growpart: - device: /dev/sdb partition: 2 ``` -------------------------------- ### Install Packages Source: https://github.com/canonical/cloud-init/blob/main/doc/examples/cloud-config.txt Specify a list of packages to be installed on the instance. Ensure the package manager is configured correctly. ```yaml #cloud-config packages: - pwgen - pastebinit ``` -------------------------------- ### Example data.yaml for Module Documentation Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/development/module_creation.rst This YAML defines the metadata and examples for a cloud-init module, used for documentation generation and testing. ```yaml cc_module_name: description: > This module provides some functionality, which you can describe here. For straightforward text examples, use a greater-than (``>``) symbol next to ``description: `` to ensure proper rendering in the documentation. Empty lines will be respected, but line-breaks are folded together to create proper paragraphs. If you need to use call-outs or code blocks, use a pipe (``|``) symbol instead of ``>`` so that reStructuredText formatting (e.g. for directives, which take varying levels of indentation) is respected. examples: - comment: | Example 1: (optional) description of the expected behavior of the example file: cc_module_name/example1.yaml - comment: | Example 2: (optional) description of a second example. file: cc_module_name/example2.yaml name: Module Name ``` -------------------------------- ### Example meta-data configuration Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/datasources/nocloud.rst An example of a meta-data file, including instance-id, network interface configuration, and hostname. ```yaml instance-id: iid-abcdefg network-interfaces: | iface eth0 inet static address 192.168.1.10 network 192.168.1.0 netmask 255.255.255.0 broadcast 192.168.1.255 gateway 192.168.1.254 hostname: myhost ``` -------------------------------- ### Install Landscape Client from PPA Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/landscape.rst To install the 'landscape-client' package from a PPA, specify the PPA details within the 'apt.sources' section of the configuration. ```yaml apt: sources: landscape: source: "deb http://ppa.launchpad.net/landscape/1.0/ubuntu precise main" key: "3B94EF." # Example key, replace with actual key account-name: "example.com" computer-title: "My Server" ``` -------------------------------- ### Example cloud.cfg Configuration Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/base_config_reference.rst This is a comprehensive example of a cloud.cfg file, illustrating various configuration options for modules, system information, and package mirrors. ```yaml # The top level settings are used as module and base configuration. # A set of users which may be applied and/or used by various modules # when a 'default' entry is found it will reference the 'default_user' # from the distro configuration specified below users: - default # If this is set, 'root' will not be able to ssh in and they # will get a message to login instead as the default $user disable_root: true # This will cause the set+update hostname module to not operate (if true) preserve_hostname: false # If you use datasource_list array, keep array items in a single line. # If you use multi line array, ds-identify script won't read array items. # Example datasource config # datasource: # Ec2: # metadata_urls: [ 'blah.com' ] # timeout: 5 # (defaults to 50 seconds) # max_wait: 10 # (defaults to 120 seconds) # The modules that run in the 'init' stage cloud_init_modules: - seed_random - bootcmd - write_files - growpart - resizefs - disk_setup - mounts - set_hostname - update_hostname - update_etc_hosts - ca_certs - rsyslog - users_groups - ssh # The modules that run in the 'config' stage cloud_config_modules: - wireguard - snap - ubuntu_autoinstall - ssh_import_id - keyboard - locale - set_passwords - grub_dpkg - apt_pipelining - apt_configure - ubuntu_pro - ntp - timezone - disable_ec2_metadata - runcmd - byobu # The modules that run in the 'final' stage cloud_final_modules: - package_update_upgrade_install - fan - landscape - lxd - ubuntu_drivers - write_files_deferred - puppet - chef - ansible - mcollective - salt_minion - reset_rmc - 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 and/or distro specific settings # (not accessible to handlers/transforms) system_info: # This will affect which distro class gets used distro: ubuntu # Default user name + that default users groups (if added/used) default_user: name: ubuntu doas: - permit nopass ubuntu lock_passwd: True gecos: Ubuntu groups: [adm, cdrom, dip, lxd, sudo] sudo: ["ALL=(ALL) NOPASSWD:ALL"] shell: /bin/bash network: dhcp_client_priority: [dhclient, dhcpcd, udhcpc] renderers: ['netplan', 'eni', 'sysconfig'] activators: ['netplan', 'eni', 'network-manager', 'networkd'] # Automatically discover the best ntp_client ntp_client: auto # Other config here will be given to the distro class and/or path classes paths: cloud_dir: /var/lib/cloud/ templates_dir: /etc/cloud/templates/ package_mirrors: - arches: [i386, amd64] failsafe: primary: http://archive.ubuntu.com/ubuntu security: http://security.ubuntu.com/ubuntu search: primary: - http://%(ec2_region)s.ec2.archive.ubuntu.com/ubuntu/ - http://%(availability_zone)s.clouds.archive.ubuntu.com/ubuntu/ - http://%(region)s.clouds.archive.ubuntu.com/ubuntu/ security: [] - arches: [arm64, armel, armhf] failsafe: primary: http://ports.ubuntu.com/ubuntu-ports security: http://ports.ubuntu.com/ubuntu-ports search: ``` -------------------------------- ### Set Up Filesystems Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/disk_setup.rst Demonstrates configuring various filesystems on different devices using `fs_setup`. ```yaml fs_setup: - label: ephemeral0 filesystem: 'ext3' device: 'ephemeral0' partition: 'auto' - label: mylabl2 filesystem: 'ext4' device: '/dev/xvda1' - cmd: mkfs -t %(filesystem)s -L %(label)s %(device)s label: mylabl3 filesystem: 'btrfs' device: '/dev/xvdh' ``` -------------------------------- ### Install Arbitrary Packages with Cloud-Config Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/examples.rst Install specified packages using the instance's default package manager. This example shows installing `nginx` and `vim`. ```yaml packages: - nginx - vim ``` -------------------------------- ### Example Boot Analysis Output Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/explanation/analyze.rst The 'boot' subcommand provides key timestamps related to the kernel and cloud-init activation, including kernel start/end times and cloud-init activation time, along with calculated durations. ```text -- Most Recent Boot Record -- Kernel Started at: 2019-08-29 01:35:37.753790 Kernel ended boot at: 2019-08-29 01:35:38.807407 Kernel time to boot (seconds): 1.053617000579834 Cloud-init activated by systemd at: 2019-08-29 01:35:43.992460 Time between Kernel end boot and Cloud-init activation (seconds): 5.185053110122681 Cloud-init start: 2019-08-29 08:35:45.867000 successful ``` -------------------------------- ### Defer writing content until after package installation Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/write_files.rst Defers the writing of a file until after packages have been installed and users have been created. This is useful for configuration files that depend on installed software or specific user setups. ```yaml write_files: - path: /etc/my_app/config.yaml content: | database: host: localhost port: 5432 defer: true owner: myuser:mygroup permissions: 0600 ``` -------------------------------- ### OpenStack Vendor-Data Example Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/datasources/openstack.rst Example of vendor-data configuration for OpenStack to upgrade packages and install htop. This JSON object is placed in the 'cloud-init' attribute of the vendor-data. ```json {"cloud-init": "#cloud-config\npackage_upgrade: True\npackages:\n - htop"} ``` -------------------------------- ### Cloud-Config Content Example Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/explanation/kernel-command-line.rst Example of cloud-config data that can be fetched via the `cloud-config-url` kernel command line parameter. Only content starting with `#cloud-config` is supported. ```yaml #cloud-config datasource: MAAS: metadata_url: http://mass-host.localdomain/source consumer_key: Xh234sdkljf token_key: kjfhgb3n token_secret: 24uysdfx1w4 ``` -------------------------------- ### Example resolv.conf configuration Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/resolv_conf.rst This example demonstrates a basic configuration for the resolv.conf file using cloud-init. Ensure you verify the preferred method for your distribution before applying changes. ```yaml network: version: 2 ethernets: eth0: dhcp4: true nameservers: addresses: [8.8.8.8, 8.8.4.4] search: [example.com] ``` -------------------------------- ### Check WSL Version Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/howto/launch_wsl.rst Verify the installed WSL version. This is a prerequisite for following the guide. ```doscon PS> wsl --version ``` -------------------------------- ### User-data for Phone Home Module Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/module_run_frequency.rst This is an example of user-data configuration that complements the `phone_home` module setup. It specifies the URL and the data to be posted when the `phone_home` module runs. ```yaml ## template: jinja #cloud-config phone_home: url: http://example.com/{{ v1.instance_id }}/ post: all ``` -------------------------------- ### Configure mounts and swap file Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/mounts.rst This example configures multiple mount points with specific flags and creates an automatically-sized swap file with a maximum size. ```yaml #cloud-config mounts: - [ ephemeral0, "/", "auto", "noexec" ] - [ /dev/sdc, "/opt/data" ] - [ /dev/xvdh, "/opt/data", "auto", "defaults", "0", "0" ] swap: size: "auto" maxsize: 10485760 ``` -------------------------------- ### Default rsyslog Configuration on FreeBSD Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/rsyslog.rst This example shows the default rsyslog configuration, which involves only package installation, on FreeBSD. Refer to the rsyslog module schema for a full list of keys. ```yaml rsyslog: "package_name": "rsyslog" "module": "rsyslog" ``` -------------------------------- ### Basic fs_setup Configuration Source: https://github.com/canonical/cloud-init/blob/main/doc/examples/cloud-config-disk-setup.txt Defines filesystem setup for ephemeral and specific devices. 'ephemeral0' is set to ext3, and 'mylabl2' is configured on '/dev/xvda1' with ext4. ```yaml fs_setup: - label: ephemeral0 filesystem: 'ext3' device: 'ephemeral0' partition: 'auto' - label: mylabl2 filesystem: 'ext4' device: '/dev/xvda1' ``` -------------------------------- ### Set Debconf Selections Source: https://github.com/canonical/cloud-init/blob/main/doc/examples/cloud-config.txt Provide debconf answers or debian preseed values to configure package installation behavior. This example forces critical priority and sets the frontend to readline. ```yaml apt: debconf_selections: set1: debconf debconf/priority select critical set2: | debconf debconf/frontend select readline debconf debconf/frontend seen false ``` -------------------------------- ### Example Part Handler Configuration Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/explanation/part_handlers.rst This example demonstrates the basic structure of a part handler configuration file. It is intended for advanced users who need to customize cloud-init's behavior. ```python #!/usr/bin/python from cloudinit.config import PartHandler from cloudinit.config.schema import Schema class MyPartHandler(PartHandler): def __init__(self): self.schema = Schema(None) def handle(self, data, ctype, filename, sink): print('Handling part: %s' % filename) sink.write(data) cloudinit_part_handler = MyPartHandler() ``` -------------------------------- ### Update and Upgrade Packages with cloud-init Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/package_update_upgrade.rst This example demonstrates how to update and upgrade packages on the system during boot using cloud-init. Refer to the module schema for a full list of keys. ```yaml #cloud-config package_update_upgrade: true ``` -------------------------------- ### Add Daily Testing Repo Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/yum_repo.rst This example enables cloud-init upstream's daily testing repository for EPEL 8. Use this to install the latest version of cloud-init from the main branch for testing purposes. ```yaml repo: id: cloud-init-testing name: Cloud-init Daily Testing Repo baseurl: http://repos.example.com/epel8/testing/$basearch/ enabled: true gpgcheck: false ``` -------------------------------- ### Match Ethernet by Name Pattern Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/network-config-format-v2.rst Example of matching Ethernet devices using a name pattern. This configuration applies to all devices whose names start with 'enp2'. Note that globbing is supported by networkd but not NetworkManager. ```yaml match: name: enp2* ``` -------------------------------- ### Boot cloud image with QEMU and ISO Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/howto/launch_qemu.rst Launch a cloud image using QEMU, attaching the generated configuration ISO and specifying memory and network settings. ```shell-session $ qemu-system-x86_64 -m 1024 -net nic -net user \ -drive file=noble-server-cloudimg-amd64.img,index=0,format=qcow2,media=disk \ -drive file=seed.img,index=1,media=cdrom \ -machine accel=kvm:tcg ``` -------------------------------- ### Create a libvirt instance with cloud-init Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/howto/launch_libvirt.rst Use virt-install to create a new virtual machine instance. This command specifies the instance name, memory, disk image, and cloud-init configuration files (user-data, meta-data, network-config). Ensure the cloud image and configuration files are in the current directory. ```shell-session virt-install --name cloud-init-001 --memory 4000 --noreboot \ --os-variant detect=on,name=ubuntunoble \ --disk=size=10,backing_store="$(pwd)/noble-server-cloudimg-amd64.img" \ --cloud-init user-data="$(pwd)/user-data,meta-data=$(pwd)/meta-data,network-config=$(pwd)/network-config" ``` -------------------------------- ### Ubuntu WSL Welcome Screen Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/howto/launch_wsl.rst This is an example of the welcome screen displayed after successfully setting up your Ubuntu WSL instance credentials. ```text Welcome to Ubuntu Noble Numbat (GNU/Linux 5.15.146.1-microsoft-standard-WSL2 x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro System information as of Mon Apr 22 21:06:49 UTC 2024 System load: 0.08 Processes: 51 Usage of /: 0.1% of 1006.85GB Users logged in: 0 Memory usage: 4% IPv4 address for eth0: 172.29.240.255 Swap usage: 0% This message is shown once a day. To disable it please create the /root/.hushlogin file. root@machine:/mnt/c/Users/me# ``` -------------------------------- ### Puppet Example 2 Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/puppet.rst This is the second example of a Puppet configuration. ```yaml --- - name: Install Nginx puppet: package: nginx service: nginx state: present - name: Configure Nginx virtual host puppet: template: my_vhost.conf.erb source: templates/my_vhost.conf.erb dest: /etc/nginx/sites-available/my_vhost.conf owner: root group: root mode: '0644' - name: Enable Nginx virtual host puppet: command: ln -s /etc/nginx/sites-available/my_vhost.conf /etc/nginx/sites-enabled/ creates: /etc/nginx/sites-enabled/my_vhost.conf - name: Ensure Nginx is running puppet: service: nginx state: running enabled: true ``` -------------------------------- ### Landscape Client Example Configuration Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/landscape.rst This is a basic example of a Landscape client configuration. Refer to the landscape module schema or 'man landscape-config' for a full list of keys. ```yaml account-name: "example.com" computer-title: "My Server" ``` -------------------------------- ### Cloud-Config URL Example Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/explanation/kernel-command-line.rst Append this to the kernel command line to instruct cloud-init to fetch configuration from the specified URL. Ensure the URL points to valid cloud-config data. ```text cloud-config-url=http://your.url.here/abcdefg ``` -------------------------------- ### Cloud-Init Analyze Blame Example Output Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/explanation/analyze.rst Provides an example of the output generated by the 'cloud-init analyze blame' command, showing boot records and the time taken by various cloud-init modules in descending order. ```text -- Boot Record 01 -- 00.80300s (init-network/config-growpart) 00.64300s (init-network/config-resizefs) 00.62100s (init-network/config-ssh) 00.57300s (modules-config/config-grub_dpkg) 00.40300s (init-local/search-NoCloud) 00.38200s (init-network/config-users_groups) 00.19800s (modules-config/config-apt_configure) 00.03700s (modules-final/config-keys_to_console) 00.02100s (init-network/config-update_etc_hosts) 00.02100s (init-network/check-cache) 00.00800s (modules-final/config-ssh_authkey_fingerprints) 00.00800s (init-network/consume-vendor-data) 00.00600s (modules-config/config-timezone) 00.00500s (modules-final/config-final_message) 00.00400s (init-network/consume-user-data) 00.00400s (init-network/config-mounts) 00.00400s (init-network/config-disk_setup) 00.00400s (init-network/config-bootcmd) 00.00400s (init-network/activate-datasource) 00.00300s (init-network/config-update_hostname) 00.00300s (init-network/config-set_hostname) 00.00200s (modules-final/config-snappy) 00.00200s (init-network/config-rsyslog) 00.00200s (init-network/config-ca_certs) 00.00200s (init-local/check-cache) 00.00100s (modules-final/config-scripts_vendor) 00.00100s (modules-final/config-scripts_per_once) 00.00100s (modules-final/config-salt_minion) 00.00100s (modules-final/config-phone_home) 00.00100s (modules-final/config-package_update_upgrade_install) 00.00100s (modules-final/config-fan) 00.00100s (modules-config/config-ubuntu_pro) 00.00100s (modules-config/config-ssh_import_id) 00.00100s (modules-config/config-snap) 00.00100s (modules-config/config-set_passwords) 00.00100s (modules-config/config-runcmd) 00.00100s (modules-config/config-locale) 00.00100s (modules-config/config-byobu) 00.00100s (modules-config/config-apt_pipelining) 00.00100s (init-network/config-write_files) 00.00100s (init-network/config-seed_random) 00.00000s (modules-final/config-ubuntu_drivers) 00.00000s (modules-final/config-scripts_user) 00.00000s (modules-final/config-scripts_per_instance) 00.00000s (modules-final/config-scripts_per_boot) 00.00000s (modules-final/config-puppet) 00.00000s (modules-final/config-power_state_change) 00.00000s (modules-final/config-mcollective) 00.00000s (modules-final/config-lxd) 00.00000s (modules-final/config-landscape) 00.00000s (modules-final/config-chef) 00.00000s (modules-config/config-snap_config) 00.00000s (modules-config/config-ntp) 00.00000s (modules-config/config-disable_ec2_metadata) 00.00000s (init-network/setup-datasource) 1 boot records analyzed ``` -------------------------------- ### Bonded VLAN Configuration Example Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/network-config-format-v1.rst Example of configuring a bonded VLAN interface. ```yaml network: version: 1 bonds: bond0: interfaces: ["eth0", "eth1"] parameters: mode: "802.3ad" mii-monitor-interval: 100 "vlan-id": 100 "vlan-raw-device": "bond0" ``` -------------------------------- ### Install multiple snaps Source: https://github.com/canonical/cloud-init/blob/main/doc/rtd/reference/yaml_examples/snap.rst Use a list to specify multiple snap packages to be installed. ```yaml snap: - name: hello-world channel: latest/stable state: present - name: core channel: latest/stable state: present ```