<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Archives - LucD notes</title>
	<atom:link href="https://www.lucd.info/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.lucd.info/tag/code/</link>
	<description>My PowerShell ramblings</description>
	<lastBuildDate>Mon, 09 Dec 2019 09:09:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.lucd.info/wp-content/uploads/2018/12/cropped-120px-Tibetan_Dharmacakra-32x32.png</url>
	<title>Code Archives - LucD notes</title>
	<link>https://www.lucd.info/tag/code/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>Cloud-init &#8211; Part 2 &#8211; Advanced Ubuntu</title>
		<link>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/</link>
					<comments>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/#comments</comments>
		
		<dc:creator><![CDATA[LucD]]></dc:creator>
		<pubDate>Sat, 07 Dec 2019 07:43:24 +0000</pubDate>
				<category><![CDATA[Cloud-init]]></category>
		<category><![CDATA[Deploy]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[powershellv6]]></category>
		<category><![CDATA[powershellv7]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Visual Studio Code]]></category>
		<category><![CDATA[YAML]]></category>
		<category><![CDATA[cloud-init]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[repository]]></category>
		<category><![CDATA[v6]]></category>
		<category><![CDATA[v7]]></category>
		<guid isPermaLink="false">http://www.lucd.info/?p=6693</guid>

					<description><![CDATA[In Cloud-init – Part 1 – The Basics, we laid the groundwork for [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In <a href="https://www.lucd.info/2019/12/06/cloud-init-part-1-the-basics/" target="_blank" rel="noopener noreferrer">Cloud-init – Part 1 – The Basics</a>, we laid the groundwork for using <strong>cloud-init</strong> in a <strong>vSphere</strong> environment. In this post we will go into more <strong>advanced Ubuntu</strong> setups. This includes deploying PowerShell, v6 and v7, using repositories and if needed, a GUI with Visual Studio Code.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="679" height="296" src="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2.png" alt="" class="wp-image-6708" srcset="https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2.png 679w, https://www.lucd.info/wp-content/uploads/2019/12/cloud-init-part-2-2-300x131.png 300w" sizes="(max-width: 679px) 100vw, 679px" /></figure>



<span id="more-6693"></span>



<p>For the installation of software on the instance we use the <strong>user-data</strong> 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 &#8220;<em>cattle</em>&#8221; station to test/run (and optionally develop) your scripts. </p>



<h2 class="wp-block-heading">Scripting Packages</h2>



<h3 class="wp-block-heading">PowerShell</h3>



<p>Let&#8217;s start by installing <strong>PowerShell</strong> on the instance. This comes down to adding a couple of instructions to the YAML file we used in Part 1.</p>



<pre class="lang:yaml decode:true ">#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</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 33-39</strong>: The instructions to install PowerShell are added to the <strong>runcmd</strong> section.</p>



<p><strong>Line 33</strong>: You can use comments in your YAML file. I highly recommend doing that. It will help others, and probably yourself later on.</p>



<p><strong>Line 34-35</strong>: The method uses the <a rel="noreferrer noopener" aria-label="install.sh (opens in a new tab)" href="https://github.com/PowerShell/PowerShell/blob/master/tools/install-powershell.sh" target="_blank">install-powershell.sh</a> 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 <strong>install-powershell.sh</strong> script determines on which distro it is running, and launches the appropriate command(s).</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img decoding="async" width="550" height="288" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps.png" alt="" class="wp-image-6744" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps.png 550w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicps-300x157.png 300w" sizes="(max-width: 550px) 100vw, 550px" /></figure>



<h3 class="wp-block-heading">Git &amp; repo cloning</h3>



<p>For your scripts, you, of course, use a repository. That means you will need to configure <a rel="noreferrer noopener" aria-label="git (opens in a new tab)" href="https://www.linux.com/tutorials/introduction-using-git/" target="_blank">git</a>, and a way to clone repositories to your instance.</p>



<p>The following extract of the YAML file only shows the lines in the <strong>runcmd</strong> section.</p>



<pre class="lang:yaml decode:true  ">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</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 14-16</strong>: 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.</p>



<p><strong>Line 17-19</strong>: As part of the deployment process through cloud-init, you can clone specific repositories to the instance. In these lines I used <a rel="noreferrer noopener" aria-label="Alan Renouf (opens in a new tab)" href="https://twitter.com/alanrenouf" target="_blank">Alan Renouf</a>&#8216;s <a href="https://github.com/alanrenouf/vCheck-vSphere" target="_blank" rel="noreferrer noopener" aria-label="vCheck-vSphere (opens in a new tab)">vCheck-vSphere</a> repository as an example.</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img decoding="async" width="517" height="293" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck.png" alt="" class="wp-image-6747" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck.png 517w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicvcheck-300x170.png 300w" sizes="(max-width: 517px) 100vw, 517px" /></figure>



<h2 class="wp-block-heading">Going GUI</h2>



<p>There might be occasions when you need to provide a temporary station with a GUI. That can also be done with cloud-init&#8217;s user-data YAML file.</p>



<h3 class="wp-block-heading">The Desktop</h3>



<p>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.</p>



<p>The following sample is intended for an Ubuntu 18.04 LTS instance. Again this extract only shows the <strong>runcmd</strong> section of the YAML file.</p>



<pre class="lang:yaml decode:true   "># Install GUI
- apt-get install -y tasksel
- tasksel install lubuntu-core
# ==&gt; 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</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 1-10</strong>: In this example, I use <a rel="noreferrer noopener" aria-label="lubuntu (opens in a new tab)" href="https://lubuntu.net/" target="_blank">lubuntu</a> as the desktop. Primarily because it is fast and resource-friendly.</p>



<p><strong>Line </strong>2: We use <a rel="noreferrer noopener" aria-label="tasksel (opens in a new tab)" href="https://manpages.ubuntu.com/manpages/xenial/man8/tasksel.8.html" target="_blank">tasksel</a> to install the desktop, so we have to make sure that it is available.</p>



<p><strong>Line 5</strong>: There is a known issue with the <a rel="noreferrer noopener" aria-label="light-locker (opens in a new tab)" href="https://launchpad.net/ubuntu/+source/light-locker" target="_blank">light-locker</a> 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 <a rel="noreferrer noopener" aria-label="xscreensaver (opens in a new tab)" href="https://packages.ubuntu.com/search?keywords=xscreensaver" target="_blank">xscreensaver</a>.</p>



<p><strong>Line 6</strong>: Next we start the display manager <a rel="noreferrer noopener" aria-label="lightdm (opens in a new tab)" href="https://wiki.ubuntu.com/LightDM" target="_blank">lightdm</a>.</p>



<p><strong>Line 7-</strong>10: We need a way to connect to the station we are deploying. In this example, I opted for RDP as my protocol.</p>



<p><strong>Line 8</strong>: The <a rel="noreferrer noopener" aria-label="apt-transport-https (opens in a new tab)" href="https://manpages.ubuntu.com/manpages/bionic/man1/apt-transport-https.1.html" target="_blank">apt-transport-https</a> package is a pre-requisite.</p>



<p><strong>Line 10</strong>: Configure the <a rel="noreferrer noopener" aria-label="xrdp (opens in a new tab)" href="https://help.ubuntu.com/community/xrdp" target="_blank">xrdp</a> service to automatically start at the boot.</p>



<h3 class="wp-block-heading">Visual Studio Code</h3>



<p>Now that we have a GUI, we can use several GUI based applications. For working with PowerShell, the <a rel="noreferrer noopener" aria-label="Visual Studio Code (opens in a new tab)" href="https://code.visualstudio.com/" target="_blank">Visual Studio Code</a> editor with the <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell" target="_blank" rel="noreferrer noopener" aria-label="PowerShell extension (opens in a new tab)">PowerShell extension</a> is an obvious choice.</p>



<pre class="lang:yaml decode:true "># 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/</pre>



<h4 class="wp-block-heading">Annotations</h4>



<p><strong>Line 2-4</strong>: The standard <a href="https://code.visualstudio.com/docs/setup/linux" target="_blank" rel="noopener noreferrer">installation procedure</a> for Code on a Linux platform.</p>
<p><strong>Line 5-8</strong>: This installs the extensions for <a href="https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell" target="_blank" rel="noopener noreferrer">PowerShell</a> and <a href="https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens" target="_blank" rel="noopener noreferrer">GitLens</a>. 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.</p>



<h4 class="wp-block-heading">Result</h4>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="561" src="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1024x561.png" alt="" class="wp-image-6773" srcset="https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1024x561.png 1024w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-300x164.png 300w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-768x421.png 768w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-1536x842.png 1536w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI-720x395.png 720w, https://www.lucd.info/wp-content/uploads/2019/12/ubuntubionicpsdevGUI.png 1633w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<h2 class="wp-block-heading">Conclusion</h2>



<p>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.</p>



<p>If you have suggestions or questions about specific additions on these instances, feel free to use the comments.</p>



<p>Enjoy!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.lucd.info/2019/12/07/cloud-init-part-2-advanced-ubuntu/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
