In Cloud-init – Part 1 – The Basics, we laid the groundwork for using cloud-init in a vSphere environment. In this post we will go into more advanced Ubuntu setups. This includes deploying PowerShell, v6 and v7, using repositories and if needed, a GUI with Visual Studio Code.
For the installation of software on the instance we use the user-data YAML file. In the following sections, we discuss some of the more obvious candidates you want to have on your station. Remember that the basic idea of the series is to have a “cattle” station to test/run (and optionally develop) your scripts.
Scripting Packages
PowerShell
Let’s start by installing PowerShell on the instance. This comes down to adding a couple of instructions to the YAML file we used in Part 1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
#cloud-config hostname: ubuntubionicps fqdn: ubuntubionicps.local.lab write_files: - path: /etc/netplan/50-cloud-init.yaml content: | network: version: 2 ethernets: ens192: addresses: [192.168.10.82/24] gateway4: 192.168.10.1 dhcp6: false nameservers: addresses: - 192.168.10.2 - 192.168.10.3 search: - local.lab dhcp4: false optional: true - path: /etc/sysctl.d/60-disable-ipv6.conf owner: root content: | net.ipv6.conf.all.disable_ipv6=1 net.ipv6.conf.default.disable_ipv6=1 runcmd: - netplan --debug apply - sysctl -w net.ipv6.conf.all.disable_ipv6=1 - sysctl -w net.ipv6.conf.default.disable_ipv6=1 - apt-get -y update - add-apt-repository universe # PowerShell - wget https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/install-powershell.sh - chmod 755 install-powershell.sh # PowerShell v6 - ./install-powershell.sh # PowerShell v7 - ./install-powershell.sh -preview - apt-get -y clean - apt-get -y autoremove --purge timezone: Europe/Brussels system_info: default_user: name: default-user lock_passwd: false sudo: ["ALL=(ALL) NOPASSWD:ALL"] disable_root: false ssh_pwauth: yes users: - default - name: luc gecos: LucD lock_passwd: false groups: sudo, users, admin shell: /bin/bash sudo: ['ALL=(ALL) NOPASSWD:ALL'] chpasswd: list: | default-user:$6$aPp//e2ueP$ETEXcAhAyQuJ4qNCbqxmmSYGZbg2wFwpP/YITvoXdgxwZBnf32drePKi2OIn5fLqtH5pHO03yRdPXK3ToLG6b0 luc:$6$kW1WwJ2K$M6415du1BZd.qt92SvR6X.RuyDhEZmgR4hz4NcKH9XHn2850Vc6zHpubXM6uUeqMUaJQ740ogROB74gfBEhn9. root:$6$Js9CVr06$br9qf0VxuBsdY7Vtg/0pk9jLlycYBDLVsvbKwLDleCK7dSDheOxWaFOWdjkiqSPRrWG./N8V5RgCVwugZGnTc1 expire: false package_upgrade: true package_reboot_if_required: true power_state: delay: now mode: reboot message: Rebooting the OS condition: if [ -e /var/run/reboot-required ]; then exit 0; else exit 1; fi |
Annotations
Line 33-39: The instructions to install PowerShell are added to the runcmd section.
Line 33: You can use comments in your YAML file. I highly recommend doing that. It will help others, and probably yourself later on.
Line 34-35: The method uses the install-powershell.sh bash script to install the latest PowerShell v6 and the latest PowerShell v7 Preview. I personally prefer this method vs the apt-get method, since it is globally usable on multiple Linux distributions. The install-powershell.sh script determines on which distro it is running, and launches the appropriate command(s).
Result
Git & repo cloning
For your scripts, you, of course, use a repository. That means you will need to configure git, and a way to clone repositories to your instance.
The following extract of the YAML file only shows the lines in the runcmd section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
runcmd: - netplan --debug apply - sysctl -w net.ipv6.conf.all.disable_ipv6=1 - sysctl -w net.ipv6.conf.default.disable_ipv6=1 - apt-get -y update - add-apt-repository universe # PowerShell - wget https://raw.githubusercontent.com/PowerShell/PowerShell/master/tools/install-powershell.sh - chmod 755 install-powershell.sh # PowerShell v6 - ./install-powershell.sh # PowerShell v7 - ./install-powershell.sh -preview # Git - git config --global user.name "LucD" - git config --global user.email "lucd@lucd.info" - mkdir /home/luc/vCheck-vSphere - chown luc:luc /home/luc/vCheck-vSphere - git clone https://github.com/alanrenouf/vCheck-vSphere /home/luc/vCheck-vSphere - apt-get -y clean - apt-get -y autoremove --purge |
Annotations
Line 14-16: Since git is by default installed in the Ubuntu 18.04 LTS Cloud Image OVA file, we only need to configure some settings. In this we update some configuration settings, primarily to be able to correct sign off commits.
Line 17-19: As part of the deployment process through cloud-init, you can clone specific repositories to the instance. In these lines I used Alan Renouf‘s vCheck-vSphere repository as an example.
Result
Going GUI
There might be occasions when you need to provide a temporary station with a GUI. That can also be done with cloud-init’s user-data YAML file.
The Desktop
The choice of which desktop and which desktop manager is completely up to you. Just be aware that some of these might require more resources to be assigned to the instance.
The following sample is intended for an Ubuntu 18.04 LTS instance. Again this extract only shows the runcmd section of the YAML file.
1 2 3 4 5 6 7 8 9 10 |
# Install GUI - apt-get install -y tasksel - tasksel install lubuntu-core # ==> know issue - apt-get --purge remove -y light-locker - service lightdm start # Install RDP - apt-get install -y apt-transport-https - apt-get install -y xrdp - systemctl enable xrdp |
Annotations
Line 1-10: In this example, I use lubuntu as the desktop. Primarily because it is fast and resource-friendly.
Line 2: We use tasksel to install the desktop, so we have to make sure that it is available.
Line 5: There is a known issue with the light-locker package. Until a final fix is available, the easiest solution is to just uninstall it. If you absolutely need a screen-saver, install one of the alternatives. Like for example xscreensaver.
Line 6: Next we start the display manager lightdm.
Line 7-10: We need a way to connect to the station we are deploying. In this example, I opted for RDP as my protocol.
Line 8: The apt-transport-https package is a pre-requisite.
Line 10: Configure the xrdp service to automatically start at the boot.
Visual Studio Code
Now that we have a GUI, we can use several GUI based applications. For working with PowerShell, the Visual Studio Code editor with the PowerShell extension is an obvious choice.
1 2 3 4 5 6 7 8 |
# Install Visual Studio Code - add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" - apt-get update - apt-get install -y code - mkdir /home/luc/.vscode - chown luc:luc /home/luc/.vscode - code --install-extension ms-vscode.powershell --user-data-dir /home/luc/.vscode/ --extensions-dir /home/luc/.vscode/extensions/ - code --install-extension eamodio.gitlens --user-data-dir /home/luc/.vscode --extensions-dir /home/luc/.vscode/extensions/ |
Annotations
Line 2-4: The standard installation procedure for Code on a Linux platform.
Line 5-8: This installs the extensions for PowerShell and GitLens. Remember that the cloud-init stages run under the root account, if you need to install packages for a user, make sure to change the owner.
Result
Conclusion
We have now set the stage to actually start using these instances. In one of the next posts in this cloud-init series, we will show how to run scripts on these stations.
If you have suggestions or questions about specific additions on these instances, feel free to use the comments.
Enjoy!