<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>ansible on gavincampbell.dev</title>
		<link>https://gavincampbell.dev/tags/ansible/</link>
		<description>Recent content in ansible on gavincampbell.dev</description>
		<generator>Hugo 0.121.2</generator>
		<language>en-gb</language>
		<managingEditor>gavin@gavincampbell.dev (Gavin Campbell)</managingEditor>
		<webMaster>gavin@gavincampbell.dev (Gavin Campbell)</webMaster>
		<copyright>
Except as otherwise noted, all blog posts, photos, graphics, presentations and other media on &lt;a property=&#34;dct:title&#34; rel=&#34;cc:attributionURL&#34; href=&#34;https://gavincampbell.dev&#34;&gt;gavincampbell.dev&lt;/a&gt; by &lt;a rel=&#34;cc:attributionURL dct:creator&#34; property=&#34;cc:attributionName&#34; href=&#34;https://gavincampbell.dev&#34;&gt;Gavin Campbell&lt;/a&gt; are licensed under &lt;a href=&#34;http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1&#34; target=&#34;_blank&#34; rel=&#34;license noopener noreferrer&#34; style=&#34;display:inline-block;&#34;&gt;Attribution 4.0 International&lt;/a&gt;. Except as otherwise noted, all code snippets on &lt;a property=&#34;dct:title&#34; rel=&#34;cc:attributionURL&#34; href=&#34;https://gavincampbell.dev&#34;&gt;gavincampbell.dev&lt;/a&gt; by &lt;a rel=&#34;cc:attributionURL dct:creator&#34; property=&#34;cc:attributionName&#34; href=&#34;https://gavincampbell.dev&#34;&gt;Gavin Campbell&lt;/a&gt; are licensed under &lt;a href=&#34;http://creativecommons.org/publicdomain/zero/1.0?ref=chooser-v1&#34; target=&#34;_blank&#34; rel=&#34;license noopener noreferrer&#34; style=&#34;display:inline-block;&#34;&gt;CC0 1.0 Universal&lt;/a&gt;. Built with the &lt;a href=&#34;https://github.com/spookey/slick&#34;&gt;Slick&lt;/a&gt; theme for &lt;a href=&#34;https://gohugo.io/&#34;&gt;Hugo&lt;/a&gt;, hosted in the UK by &lt;a href=&#34;https://www.mythic-beasts.com//&#34;&gt;Mythic Beasts&lt;/a&gt;</copyright>
		<lastBuildDate>Wed, 15 May 2024 00:00:00 +0000</lastBuildDate>
		<atom:link href="https://gavincampbell.dev/tags/ansible/index.xml" rel="self" type="application/rss+xml"/>
		<item>
			<title>Managing fedora toolboxes with ansible (including pyspark installation)</title>
			<link>https://gavincampbell.dev/post/fedora-toolbox-ansible-pyspark/</link>
			<pubDate>Wed, 15 May 2024 00:00:00 +0000</pubDate>
			<author>gavin@gavincampbell.dev (Gavin Campbell)</author>
			<guid isPermaLink="true">https://gavincampbell.dev/post/fedora-toolbox-ansible-pyspark/</guid>
			<description>&lt;p&gt;I&amp;rsquo;ve been using &lt;a href=&#34;https://containertoolbx.org/&#34;&gt;toolbx&lt;/a&gt; to manage development toolboxes on Fedora for quite a few years now, but I&amp;rsquo;ve been a bit lazy about automating the setup of new toolbox containers. I&amp;rsquo;ve finally got a mostly-working ansible playbook that I can share here. Getting &lt;code&gt;pyspark&lt;/code&gt; to work took a bit of trial and error, so this will definitely be useful in the future.&lt;/p&gt;
&lt;p&gt;Briefly, a toolbox is a &lt;em&gt;mutable&lt;/em&gt; container that allows you to install development toolchains - or anything else - in a way that is isolated from the host operating system. The advantage of using &lt;code&gt;toolbox&lt;/code&gt; over ordinary &lt;code&gt;podman&lt;/code&gt; or &lt;code&gt;docker&lt;/code&gt; containers is that everything &amp;ldquo;just works&amp;rdquo; - your home directory is mounted, networking is all configured, usb devices work, etc.&lt;/p&gt;
&lt;p&gt;The full playbook is embedded below, but I thought I&amp;rsquo;d call out the interesting parts.&lt;/p&gt;
&lt;h4 id=&#34;prerequisites&#34;&gt;Prerequisites&lt;/h4&gt;
&lt;p&gt;The playbook doesn&amp;rsquo;t create the toolbox so we have to do this manually.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;toolbox create fedora40-pyspark
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A new toolbox doesn&amp;rsquo;t include &lt;code&gt;ansible&lt;/code&gt;, so we need to install that too.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;toolbox enter fedora40-pyspark
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;sudo dnf install ansible
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In order to get Spark to work, I needed to create a few environment variables. However, I didn&amp;rsquo;t want to add them to my main &lt;code&gt;.bashrc&lt;/code&gt;, as they will only apply to this container.&lt;/p&gt;
&lt;p&gt;I added the following section at the end of my &lt;code&gt;.bashrc&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;# toolbox-specific env vars&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt; -f /run/.toolboxenv &lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;; &lt;span style=&#34;color:#66d9ef&#34;&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    source /run/.containerenv
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt; -f ~/.toolbox-$name.rc &lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;; &lt;span style=&#34;color:#66d9ef&#34;&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        source ~/.toolbox-$name.rc
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    &lt;span style=&#34;color:#66d9ef&#34;&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This detects whether we are running in a toolbox, and if we are it looks for a supplementary file called &lt;code&gt;toolbox-&lt;/code&gt;&lt;em&gt;&lt;code&gt;&amp;lt;toolbox-name&amp;gt;&lt;/code&gt;&lt;/em&gt;&lt;code&gt;.rc&lt;/code&gt;. This change in &lt;code&gt;.bashrc&lt;/code&gt; is done manually, but the playbook will create the toolbox-specific file.&lt;/p&gt;
&lt;h4 id=&#34;details&#34;&gt;Details&lt;/h4&gt;
&lt;p&gt;At the beginning we make sure we are running inside a toolbox, by checking for the same &lt;code&gt;.toolboxenv&lt;/code&gt; file. This is important as the playbook relies on being able to write to our home directory as well as to the container filesystem. We also retrieve the name of the toolbox so we can use it to create the toolbox-specific environment file.&lt;/p&gt;
&lt;p&gt;After that there is some generic setup stuff, mostly installing the tools I use for work.&lt;/p&gt;
&lt;p&gt;For Spark, we download and install to &lt;code&gt;/opt/spark-&lt;/code&gt;&lt;em&gt;&lt;code&gt;x.y.z&lt;/code&gt;&lt;/em&gt;, where &lt;em&gt;&lt;code&gt;x.y.z&lt;/code&gt;&lt;/em&gt; is the spark version number.&lt;/p&gt;
&lt;p&gt;For the Spark-specific environment variables, we write them to the toolbox-specific environment file.&lt;/p&gt;
&lt;p&gt;The key to getting &lt;code&gt;pyspark&lt;/code&gt; to work inside the toolbox turned out to be adding &lt;code&gt;export SPARK_LOCAL_IP=localhost&lt;/code&gt; to the rest of the &amp;ldquo;standard&amp;rdquo; variables. This is because the hostname inside the toolbox is always &lt;code&gt;toolbox&lt;/code&gt; and this name can&amp;rsquo;t be resolved to an IP address. &lt;code&gt;/etc/hosts&lt;/code&gt; is symlinked (indirectly) to the host&amp;rsquo;s &lt;code&gt;/etc/hosts&lt;/code&gt; and is not writeable.&lt;/p&gt;
&lt;p&gt;Finally we do a bit of Spark config and install pyspark.&lt;/p&gt;
&lt;h4 id=&#34;running-the-playbook&#34;&gt;Running the playbook&lt;/h4&gt;
&lt;p&gt;I run this from inside the container with&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;ansible-playbook ~/code/misc/toolbox-setup.yml --ask-become-pass 
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;toolbox&lt;/code&gt; containers have passwordless &lt;code&gt;sudo&lt;/code&gt;, but ansible doesn&amp;rsquo;t know about this by default, so entering the password by hand was easier.&lt;/p&gt;
&lt;p&gt;After running the playbook, it&amp;rsquo;s best to exit and reenter the toolbox so that the environment variables are picked up.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;exit
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;toolbox enter fedora40-pyspark
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The complete playbook follows, hopefully this is a useful starting point for somebody else.&lt;/p&gt;
&lt;hr&gt;
&lt;script src=&#34;https://gist.github.com/gavincampbell/4a709825325635d4d0e5b07abbf5358a.js&#34;&gt;&lt;/script&gt;

</description>
		</item>
	</channel>
</rss>
