Skip to content

Instantly share code, notes, and snippets.

@bhattisatish
Last active October 29, 2022 10:11
Show Gist options
  • Save bhattisatish/ab26eb6895100f2faab38e598d72e2cb to your computer and use it in GitHub Desktop.
Save bhattisatish/ab26eb6895100f2faab38e598d72e2cb to your computer and use it in GitHub Desktop.
Cloud-Init file / User data file
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
# cloud-init directives
# if script-user,always then the bash script defined below (userdata.txt) will be executed everytime
cloud_final_modules:
- [scripts-user, always]
runcmd:
- [ mkdir, /test-cloudinit ]
write_files:
- path: /test-cloudinit/cloud-init.txt
content: Created by cloud-init
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
/bin/echo "Hello World." >> /tmp/sdksdfjsdlf
--//
#cloud-config
# Add groups to the system.
groups:
- group_name: [member1,member2]
# Add users to the system.
users:
- default
- name: User’s login name.
gecos: User’s real name.
primary_group: The primary group the user belongs to. If omitted, a new group is created under the user’s name.
groups: Additional groups to which you want to add the user.
selinux_user: The SELinux user for the user's login. If omitted, the system selects the default SELinux user.
expiredate: 'year-month-day' - Data when the user’s account should be disabled.
ssh_import_id: SSH IDs which you want to import.
lock_passwd: Use true or false to define whether you want to lock the password to disable password login. By default, the value is set to true.
inactive: 'x' - The number of days until the account is disabled after a password expires.
passwd: The hash of the password you want to use.
ssh_authorized_keys: Add keys to user’s authorized keys file:
- <ssh pub key 1>
- <ssh pub key 2>
system: Use true or false to define the new user as a system user.
sudo: Use sudo rule string(s) to define the user privileges. The user has no privileges by default, but it can accept one or multiple sudo rule strings. For example, add ALL=(ALL) NOPASSWD:ALL to give the user unrestricted access. To prevent sudo access, type: False.
sudo: ['ALL=(ALL) NOPASSWD:ALL']
- snapuser: Specify the email for your Ubuntu SSO account to allow snap to import a username and public keys into the system.
# Write out arbitrary files
write_files:
path: The file path to which the content is added.
content: The content you want to add to the path.
owner: The user/group that has ownership. By default, the owner is root:root.
permissions: The permission of the specified path, defined with the appropriate octal string. The default permission is ‘0644’.
append: Use true or false to choose whether to append the specified content to an existing file if the path provided exists. The default value is false.
# Add yum repos
yum_repos:
<repo_name>:
baseurl: Repository URL.
name: Repository name.
enabled: true/false
+ any other repository configuration option
# Run commands at the boot time
# In case we are on apt os
apt_update: true
apt_upgrade: true
packages:
- nginx
- rsync
bootcmd:
- echo 192.168.1.130 us.archive.ubuntu.com >> /etc/hosts
- [cloud-init-per, once, mymkfs, mkfs, /dev/vdb ]
# Run commands only the first time the instance boots
runcmd:
- [ ls, -l, / ]
- [ sh, -xc, "echo $(date) ': hello world!'" ]
- [ sh, -c, echo "=========hello world'=========" ]
- ls -l /root
- sed -i -e '/^Port/s/^.*$/Port 4444/' /etc/ssh/sshd_config
- sed -i -e '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
- restart ssh
ssh_keys:
rsa_private: |
-----BEGIN RSA PRIVATE KEY-----
your_rsa_private_key
-----END RSA PRIVATE KEY-----
rsa_public: your_rsa_public_key
ssh_import_id: [user1, user2]
locale: What you want to configure as the system's locale.
locale_configfile: The path to the file where you want to write the locale configuration.
preserve_hostname: Use true or false to set whether to preserve the host name or allow altering.
prefer_fqdn_over_hostname: Use true or false to set whether to force the use of FQDN in all distros.
fqdn: FQDN
hostname: FQDN/hostname
## To debug check the following log files
# /var/log/cloud-init.log
# /var/log/cloud-init-output.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment