-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker-ami-apt-get.html
345 lines (284 loc) · 25.7 KB
/
packer-ami-apt-get.html
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="google-site-verification" content="w_Gq0a37aj_D2iAwIQodXXlrE7Aj8mF8-W5BpUsK6B4" />
<meta name="robots" content="index, follow" />
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:ital,wght@0,400;0,700;1,400&family=Source+Sans+Pro:ital,wght@0,300;0,400;0,700;1,400&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://www.devlo.io/theme/stylesheet/style.min.css">
<link id="pygments-light-theme" rel="stylesheet" type="text/css"
href="https://www.devlo.io/theme/pygments/arduino.min.css">
<link rel="stylesheet" type="text/css" href="https://www.devlo.io/theme/font-awesome/css/fontawesome.css">
<link rel="stylesheet" type="text/css" href="https://www.devlo.io/theme/font-awesome/css/brands.css">
<link rel="stylesheet" type="text/css" href="https://www.devlo.io/theme/font-awesome/css/solid.css">
<link rel="stylesheet" type="text/css" href="https://www.devlo.io/extra/static/custom.css">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="theme/favicon.ico" type="image/x-icon">
<link href="https://www.devlo.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="DevLo Software Blog Atom">
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-116740360-1', 'auto');
ga('send', 'pageview');
</script>
<meta name="author" content="Miguel Lopez" />
<meta name="description" content="How to avoid Could not get lock /var/lib/dpkg/lock while using Packer" />
<meta name="keywords" content="aws, cloud, EC2, automation, packer, apt, apt-get, ub ubutnu">
<meta property="og:site_name" content="DevLo Software Blog"/>
<meta property="og:title" content="Building Robust Packer AMIs"/>
<meta property="og:description" content="How to avoid Could not get lock /var/lib/dpkg/lock while using Packer"/>
<meta property="og:locale" content="en_US"/>
<meta property="og:url" content="https://www.devlo.io/packer-ami-apt-get.html"/>
<meta property="og:type" content="article"/>
<meta property="article:published_time" content="2018-11-16 00:00:00-07:00"/>
<meta property="article:modified_time" content="2018-11-16 00:00:00-07:00"/>
<meta property="article:author" content="https://www.devlo.io/author/miguel-lopez.html">
<meta property="article:section" content="automation"/>
<meta property="article:tag" content="aws"/>
<meta property="article:tag" content="cloud"/>
<meta property="article:tag" content="EC2"/>
<meta property="article:tag" content="automation"/>
<meta property="article:tag" content="packer"/>
<meta property="article:tag" content="apt"/>
<meta property="article:tag" content="apt-get"/>
<meta property="article:tag" content="ub ubutnu"/>
<meta property="og:image" content="/images/Devlo-Logo.png">
<title>DevLo Software Blog – Building Robust Packer AMIs</title>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>
(adsbygoogle = window.adsbygoogle || []).push({
google_ad_client: "ca-pub-2119138261035978",
enable_page_level_ads: true
});
</script>
<script>
(function(w,d,s,l,i){
w[l]=w[l]||[];
w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});
var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
j.async=true;
j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;
f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-T32GC6Q');
</script>
</head>
<body class="light-theme">
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-T32GC6Q" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<aside>
<div>
<a href="https://www.devlo.io/">
<img src="/images/Devlo-Logo.png" alt="" title="">
</a>
<h1>
<a href="https://www.devlo.io/"></a>
</h1>
<p>Lopez Brothers Blog</p>
<div class="authors-profiles">
<div>
<figure>
<img src="https://www.devlo.io/theme/img/miguel.png" alt="Miguel" title="Miguel">
<figcaption>Miguel</figcaption>
</figure>
<ul class="social">
<li>
<a class="sc-linkedin"
href="https://www.linkedin.com/in/lopezm1/"
target="_blank">
<i class="fa-brands fa-linkedin"></i>
</a>
</li>
<li>
<a class="sc-github"
href="https://github.com/lopezm1"
target="_blank">
<i class="fa-brands fa-github"></i>
</a>
</li>
</ul>
</div>
<div>
<figure>
<img src="https://www.devlo.io/theme/img/sergio.png" alt="Sergio" title="Sergio">
<figcaption>Sergio</figcaption>
</figure>
<ul class="social">
<li>
<a class="sc-linkedin"
href="https://www.linkedin.com/in/sergiolopezjr/"
target="_blank">
<i class="fa-brands fa-linkedin"></i>
</a>
</li>
<li>
<a class="sc-github"
href="https://github.com/sergoh"
target="_blank">
<i class="fa-brands fa-github"></i>
</a>
</li>
</ul>
</div>
</div>
<nav>
<ul class="list">
<li>
<a target="_self"
href="https://www.devlo.io/pages/about.html">
About
</a>
</li>
</ul>
</nav>
</div>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads-aside"
data-ad-client="ca-pub-2119138261035978"
data-ad-slot="7180191250"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</aside>
<main>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads-responsive"
data-ad-client="ca-pub-2119138261035978"
data-ad-slot="7180191250"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<nav>
<a href="https://www.devlo.io/">Home</a>
<a href="/archives.html">Archives</a>
<a href="/categories.html">Categories</a>
<a href="/tags.html">Tags</a>
<a href="https://www.devlo.io/feeds/all.atom.xml">Atom</a>
</nav>
<article class="single">
<header>
<h1 id="packer-ami-apt-get">Building Robust Packer AMIs</h1>
<p>
Posted by <a href="https://www.devlo.io/author/miguel-lopez.html">Miguel Lopez</a> on Fri 16 November 2018 in <a href="https://www.devlo.io/category/automation.html">automation</a>
</p>
</header>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads-responsive"
data-ad-client="ca-pub-2119138261035978"
data-ad-slot="5952522457"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<div>
<p>Technical Stack: AWS EC2 (AWS AMI CentOS), Packer, Ubuntu 18.04
Read: 10 minutes </p>
<h2><strong>Introduction</strong></h2>
<p>Hey everyone! Sorry for the absence. I started a new job in San Francisco that attracted my attetnion the past few months.</p>
<p>I wanted to write a quick post about an issue I encounted while creating AWS AMIs using Packer, EC2, and Ubuntu 18.04. </p>
<p>For those of you who don't know, <a href="">Packer</a> is an automation tool you can use to prepack AMIs with custom libraries or dependency your code may need upon launching. You can choose to run multiple provisioners (scripts, chef, ansible, etc) against an a base AMI in order to create your image. </p>
<h2><strong>Race conditions w/ apt-get update</strong></h2>
<p>The first issue I encountered while using Packer was while I ran <code>sudo apt-get update</code>. No matter how many times I ran it, I would consistently get:</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>Reading<span class="w"> </span>package<span class="w"> </span>lists...
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>Packages<span class="w"> </span><span class="o">(</span>main/binary-amd64/Packages<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>Packages<span class="w"> </span><span class="o">(</span>main/binary-all/Packages<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>Translations<span class="w"> </span><span class="o">(</span>main/i18n/Translation-en<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>CNF<span class="w"> </span><span class="o">(</span>main/cnf/Commands-amd64<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>CNF<span class="w"> </span><span class="o">(</span>main/cnf/Commands-all<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>Installing<span class="w"> </span>unzip.....
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>Packages<span class="w"> </span><span class="o">(</span>main/binary-amd64/Packages<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>Packages<span class="w"> </span><span class="o">(</span>main/binary-all/Packages<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>Translations<span class="w"> </span><span class="o">(</span>main/i18n/Translation-en<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>CNF<span class="w"> </span><span class="o">(</span>main/cnf/Commands-amd64<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>W:<span class="w"> </span>Target<span class="w"> </span>CNF<span class="w"> </span><span class="o">(</span>main/cnf/Commands-all<span class="o">)</span><span class="w"> </span>is<span class="w"> </span>configured<span class="w"> </span>multiple<span class="w"> </span><span class="nb">times</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>/etc/apt/sources.list.d/microsoft-prod.list:1<span class="w"> </span>and<span class="w"> </span>/etc/apt/sources.list.d/microsoft.list:1
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>/tmp/script_4426.sh:<span class="w"> </span><span class="m">6</span>:<span class="w"> </span>/tmp/script_4426.sh:<span class="w"> </span><span class="o">[[</span>:<span class="w"> </span>not<span class="w"> </span>found
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>E:<span class="w"> </span>Could<span class="w"> </span>not<span class="w"> </span>get<span class="w"> </span>lock<span class="w"> </span>/var/lib/dpkg/lock<span class="w"> </span>-<span class="w"> </span>open<span class="w"> </span><span class="o">(</span><span class="m">11</span>:<span class="w"> </span>Resource<span class="w"> </span>temporarily<span class="w"> </span>unavailable<span class="o">)</span>
<span class="w"> </span>AWS<span class="w"> </span>AMI<span class="w"> </span>Builder<span class="w"> </span>-<span class="w"> </span>CIS:<span class="w"> </span>E:<span class="w"> </span>Unable<span class="w"> </span>to<span class="w"> </span>lock<span class="w"> </span>the<span class="w"> </span>administration<span class="w"> </span>directory<span class="w"> </span><span class="o">(</span>/var/lib/dpkg/<span class="o">)</span>,<span class="w"> </span>is<span class="w"> </span>another<span class="w"> </span>process<span class="w"> </span>using<span class="w"> </span>it?
</code></pre></div>
<p>I did not understand why my simple script was causing <code>CIS: E: Could not get lock /var/lib/dpkg/lock</code> to occur. </p>
<p>Here was my provisioner: </p>
<div class="highlight"><pre><span></span><code><span class="s2">"provisioners"</span>:<span class="w"> </span><span class="o">[</span>
<span class="w"> </span><span class="o">{</span>
<span class="w"> </span><span class="s2">"type"</span>:<span class="w"> </span><span class="s2">"shell"</span>,
<span class="w"> </span><span class="s2">"inline"</span>:<span class="w"> </span><span class="o">[</span>
<span class="w"> </span><span class="s2">"sudo apt-get update -y"</span>,
<span class="w"> </span><span class="s2">"echo \"Installing unzip.....\""</span>,
<span class="w"> </span><span class="s2">"sudo apt-get --assume-yes install unzip"</span>,
<span class="w"> </span><span class="s2">"echo \"Installing python.....\""</span>,
<span class="w"> </span><span class="s2">"sudo apt-get --assume-yes install python"</span>,
<span class="w"> </span><span class="o">]</span>
<span class="w"> </span><span class="o">}</span>
<span class="w"> </span><span class="o">]</span>
</code></pre></div>
<p>It seemed like every other run would fail with a <code>CIS: E: Could not get lock /var/lib/dpkg/lock</code> error and I couldn't find a good solution. Further research proved that this error occurs whenever another process had a lock on the following file. It could be <em>any</em> process using <code>apt</code>. </p>
<p>I searched the internet and could only fine people that would suggest <code>pkill</code> on any process using apt found in <code>ps aux</code>. Or they would suggest <code>rm /var/lib/dpkg/lock</code>. Or would suggest <code>sleep 100</code> to give whatever process was using <code>apt</code> enough time to release it's lock. </p>
<p>All seemed like terrible solutions to me and nothing seemed very robust. </p>
<h2><strong>My Solution</strong></h2>
<p>I wanted to be a solution that would listen to the processes and would not continue until the process using <code>apt</code> finishes. Whatever was using <code>apt</code> is probably doing it in a meaninful way so it's best to let it finish before moving on. </p>
<p>My solution was simple. By using <code>ps aux</code> and some pipeing, we could listen to processes and only continue whenever apt was not being used by any other process.</p>
<div class="highlight"><pre><span></span><code><span class="nv">wait_apt</span><span class="o">=</span>$<span class="ss">(</span><span class="nv">ps</span><span class="w"> </span><span class="nv">aux</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="nv">grep</span><span class="w"> </span><span class="nv">apt</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="nv">wc</span><span class="w"> </span><span class="o">-</span><span class="nv">l</span><span class="ss">)</span>
<span class="k">while</span><span class="w"> </span>[<span class="w"> </span><span class="s2">"$wait_apt"</span><span class="w"> </span><span class="o">-</span><span class="nv">gt</span><span class="w"> </span><span class="s2">"1"</span><span class="w"> </span>]<span class="c1">; do echo "waiting for apt-update...."; wait_apt=$(ps aux | grep apt | wc -l); sleep 5; done</span>
</code></pre></div>
<p>To break this down... <code>ps aux</code> shows you a list of active processes, <code>grep apt</code> filters all processes to only include <code>apt</code> related ones and <code>wc -l</code> gives you a line count on your results. So effectively, this little command will show you a <code>2</code> or more whenever a process is using apt and a <code>1</code> if apt is unused. </p>
<p>Combine that with a while loop and you have a pretty robust way of checking for an idle apt. </p>
<p>Here was my complete provisioner afterwards:</p>
<div class="highlight"><pre><span></span><code><span class="w"> </span><span class="s2">"provisioners"</span>:<span class="w"> </span><span class="o">[</span>
<span class="w"> </span><span class="o">{</span>
<span class="w"> </span><span class="s2">"type"</span>:<span class="w"> </span><span class="s2">"shell"</span>,
<span class="w"> </span><span class="s2">"inline"</span>:<span class="w"> </span><span class="o">[</span>
<span class="w"> </span><span class="s2">"while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done"</span>,
<span class="w"> </span><span class="s2">"wait_apt=</span><span class="k">$(</span>ps<span class="w"> </span>aux<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>apt<span class="w"> </span><span class="p">|</span><span class="w"> </span>wc<span class="w"> </span>-l<span class="k">)</span><span class="s2">"</span>,
<span class="w"> </span><span class="s2">"while [ \"</span><span class="nv">$wait_apt</span><span class="s2">\" -gt \"1\" ]; do echo \"waiting for apt to be ready....\"; wait_apt=</span><span class="k">$(</span>ps<span class="w"> </span>aux<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>apt<span class="w"> </span><span class="p">|</span><span class="w"> </span>wc<span class="w"> </span>-l<span class="k">)</span><span class="s2">; sleep 5; done"</span>,
<span class="w"> </span><span class="s2">"sudo apt-get update -y"</span>,
<span class="w"> </span><span class="s2">"wait_apt=</span><span class="k">$(</span>ps<span class="w"> </span>aux<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>apt<span class="w"> </span><span class="p">|</span><span class="w"> </span>wc<span class="w"> </span>-l<span class="k">)</span><span class="s2">"</span>,
<span class="w"> </span><span class="s2">"while [ \"</span><span class="nv">$wait_apt</span><span class="s2">\" -gt \"1\" ]; do echo \"waiting for apt-update....\"; wait_apt=</span><span class="k">$(</span>ps<span class="w"> </span>aux<span class="w"> </span><span class="p">|</span><span class="w"> </span>grep<span class="w"> </span>apt<span class="w"> </span><span class="p">|</span><span class="w"> </span>wc<span class="w"> </span>-l<span class="k">)</span><span class="s2">; sleep 5; done"</span>,
<span class="w"> </span><span class="s2">"echo \"Installing unzip.....\""</span>,
<span class="w"> </span><span class="s2">"sudo apt-get --assume-yes install unzip"</span>,
<span class="w"> </span><span class="s2">"echo \"Installing python.....\""</span>,
<span class="w"> </span><span class="s2">"sudo apt-get --assume-yes install python"</span>,
<span class="w"> </span><span class="o">]</span>
<span class="w"> </span><span class="o">}</span>
</code></pre></div>
<p>Finding this solution was important to me because I wanted to Packer to work 100% of the time. I didn't want to randomly encounter <code>CIS: E: Could not get lock /var/lib/dpkg/lock</code> errors. </p>
</div>
<div class="tag-cloud">
<p>
<a href="https://www.devlo.io/tag/aws.html">aws</a>
<a href="https://www.devlo.io/tag/cloud.html">cloud</a>
<a href="https://www.devlo.io/tag/ec2.html">EC2</a>
<a href="https://www.devlo.io/tag/automation.html">automation</a>
<a href="https://www.devlo.io/tag/packer.html">packer</a>
<a href="https://www.devlo.io/tag/apt.html">apt</a>
<a href="https://www.devlo.io/tag/apt-get.html">apt-get</a>
<a href="https://www.devlo.io/tag/ub-ubutnu.html">ub ubutnu</a>
</p>
</div>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle ads-responsive"
data-ad-client="ca-pub-2119138261035978"
data-ad-slot="5952522457"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</article>
<footer>
<p>© </p>
<p>
Built with <a href="http://getpelican.com" target="_blank">Pelican</a> using <a href="http://bit.ly/flex-pelican" target="_blank">Flex</a> theme
</p></footer> </main>
<script type="application/ld+json">
{
"@context" : "http://schema.org",
"@type" : "Blog",
"name": " DevLo Software Blog ",
"url" : "https://www.devlo.io",
"image": "/images/Devlo-Logo.png",
"description": "DevLo - Software Blog."
}
</script>
</body>
</html>