Files
the_information_nexus/tech_docs/automation/cloud-init.md
2024-05-01 12:28:44 -06:00

60 lines
2.0 KiB
Markdown

Here's a simple example of using cloud-init to automate the configuration of an instance on first boot:
```yaml
#cloud-config
# Update packages on first boot
package_update: true
package_upgrade: true
# Install additional packages
packages:
- nginx
- php-fpm
# Write files to the system
write_files:
- path: /var/www/html/index.php
content: |
<?php
phpinfo();
?>
# Run commands on first boot
runcmd:
- systemctl start nginx
- systemctl enable nginx
# Create a user
users:
- name: webadmin
groups: sudo
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAA...your_public_ssh_key_here
# Configure SSH access
ssh_pwauth: false
disable_root: true
```
In this example:
1. The `package_update` and `package_upgrade` directives ensure that the system packages are updated on first boot.
2. The `packages` section specifies additional packages to be installed, in this case, `nginx` and `php-fpm`.
3. The `write_files` section is used to create a file on the system. Here, it creates a simple PHP script at `/var/www/html/index.php`.
4. The `runcmd` section specifies commands to be executed on first boot. In this case, it starts and enables the Nginx service.
5. The `users` section is used to create a user named `webadmin` with sudo privileges and an authorized SSH key.
6. The `ssh_pwauth` and `disable_root` directives are used to configure SSH access, disabling password authentication and root login.
To use this cloud-init configuration, you would save it as a YAML file (e.g., `cloud-config.yaml`) and provide it to your cloud provider or provisioning tool when launching a new instance.
Cloud-init will execute the specified configuration on the instance's first boot, automating the process of updating packages, installing software, creating files and users, and configuring SSH access.
This is just a simple example, but cloud-init supports a wide range of directives and modules for configuring various aspects of an instance, such as networking, storage, and more.