Alex Tomkins - Ansiblehttps://www.alextomkins.com/2018-03-31T17:25:00+01:00RUNLEVEL=1 apt-get install package alternative2018-03-31T17:25:00+01:002018-03-31T17:25:00+01:00Alex Tomkinstag:www.alextomkins.com,2018-03-31:/2018/03/runlevel-apt-get-install-package-alternative/<p>An old documented way of preventing services from starting immediately after installation in
Debian/Ubuntu is using the <tt class="docutils literal">RUNLEVEL</tt> environment variable to trick the runlevel helper into
returning a response that the system isn't fully running, such as:</p>
<pre class="code console literal-block">
<span class="gp">#</span> <span class="nv">RUNLEVEL</span><span class="o">=</span><span class="m">1</span> apt-get install nginx
</pre>
<p>Sadly this doesn't work in newer versions …</p><p>An old documented way of preventing services from starting immediately after installation in
Debian/Ubuntu is using the <tt class="docutils literal">RUNLEVEL</tt> environment variable to trick the runlevel helper into
returning a response that the system isn't fully running, such as:</p>
<pre class="code console literal-block">
<span class="gp">#</span> <span class="nv">RUNLEVEL</span><span class="o">=</span><span class="m">1</span> apt-get install nginx
</pre>
<p>Sadly this doesn't work in newer versions of Debian/Ubuntu, the official way is to use the policy
helper script <tt class="docutils literal"><span class="pre">/usr/sbin/policy-rc.d</span></tt> and return a 101 exit code. This is a a bit more
inconvenient - having to temporarily create this file only to remove it after installing the
package.</p>
<p>Fortunately there is an alternative - <cite>policyrcd-script-zg2</cite>. Install the package:</p>
<pre class="code console literal-block">
<span class="gp">$</span> sudo apt-get install policyrcd-script-zg2
</pre>
<p>Create a new script which returns a 101 exit code, I've created it as
<tt class="docutils literal"><span class="pre">/usr/local/sbin/policy-donotstart</span></tt>:</p>
<pre class="code bash literal-block">
<span class="ch">#!/bin/sh
</span><span class="nb">exit</span> <span class="m">101</span>
</pre>
<p>Then make it executable:</p>
<pre class="code console literal-block">
<span class="gp">$</span> sudo chmod <span class="m">755</span> /usr/local/sbin/policy-donotstart
</pre>
<p>When installing packages where you don't want the service to immediately start, use the
<tt class="docutils literal">POLICYRCD</tt> environment variable:</p>
<pre class="code console literal-block">
<span class="gp">#</span> <span class="nv">POLICYRCD</span><span class="o">=</span>/usr/local/sbin/policy-donotstart apt-get install nginx
</pre>
<p>The service will install, but you'll a message similar to:</p>
<pre class="code text literal-block">
invoke-rc.d: policy-rc.d denied execution of start.
</pre>
<p>For Ansible, you can add environment variables to any task:</p>
<pre class="code yaml literal-block">
<span class="p-Indicator">-</span> <span class="l-Scalar-Plain">name</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">Install nsd</span>
<span class="l-Scalar-Plain">apt</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">pkg=nsd install_recommends=no</span>
<span class="l-Scalar-Plain">environment</span><span class="p-Indicator">:</span>
<span class="l-Scalar-Plain">POLICYRCD</span><span class="p-Indicator">:</span> <span class="l-Scalar-Plain">/usr/local/sbin/policy-donotstart</span>
</pre>
<p>Now you can safely install a package, configure it, and then start it once you've got all the
correct files in place - all with a convenient environment variable.</p>