title: How Cloud-init can be stopped on first error
body: <p>When I start a linux server with Cloud-init, I have a few scripts in <code>/etc/cloud/cloud.cfg.d/</code> and they run in reverse alphabetical order</p>
  <pre class="lang-sh prettyprint-override"><code># ll /etc/cloud/cloud.cfg.d/
  total 28
  -rw-r--r-- 1 root root  173 Dec 10 12:38 00-cloudinit-lifecycle-hook.cfg
  -rw-r--r-- 1 root root 2120 Jun  1  2021 05_logging.cfg
  -rw-r--r-- 1 root root  590 Oct 26 17:55 10_aws_yumvars.cfg
  -rw-r--r-- 1 root root   29 Dec  1 18:22 20_amazonlinux_repo_https.cfg
  -rw-r--r-- 1 root root  586 Dec 10 12:38 50-cloudinit-tomcat.cfg
  -rw-r--r-- 1 root root  585 Dec 10 12:40 60-cloudinit-newrelic.cfg
  </code></pre>
  <p>The last to execute is <code>00-cloudinit-lifecycle-hook.cfg</code>, in which I complete the lifecycle for the Auto Scaling Group with a <code>CONTINUE</code>. The ASG fails if it doesn't receive this signal after a given time out.</p>
  <p>The issue is that even if there's an error in <code>50-cloudinit-tomcat.cfg</code>, it still runs <code>00-cloudinit-lifecycle-hook.cfg</code> instead of stopping</p>
  <p>How can I ensure cloud-init stops and never reaches the last script? I would like the ASG to never receive the CONTINUE signal if there's any error.</p>
  <p>Here are the files:</p>
  <p>EC2 instance user-data:</p>
  <pre><code>#cloud-config
  
  bootcmd:
    - [cloud-init-per, once, &quot;app-volume&quot;, mkfs, -t, &quot;ext4&quot;, &quot;/dev/nvme1n1&quot;]
  
  mounts:
     - [&quot;/dev/nvme1n1&quot;, &quot;/app-volume&quot;, &quot;ext4&quot;, &quot;defaults,nofail&quot;, &quot;0&quot;, &quot;0&quot;]
  
  merge_how:
    - name: list
      settings: [append]
    - name: dict
      settings: [no_replace, recurse_list]
  </code></pre>
  <p><code>50-cloudinit-tomcat.cfg</code></p>
  <pre><code>#cloud-config
  merge_how:
   - name: list
     settings: [append]
   - name: dict
     settings: [no_replace, recurse_list]
  
  runcmd:
    - &quot;#!/bin/bash -e&quot;
    - set +x
    - echo ' '
    - echo '# ===================================='
    - echo '#          Tomcat Cloud Init '
    - echo '#       /etc/cloud/cloud.cfg.d/'
    - echo '# ===================================='
    - echo ' '
    - echo '#===================================='
    - echo '#          Run Ansible'
    - echo '#===================================='
    - echo ' '
    - set -x
    - ansible-playbook /opt/init-config/tomcat/tomcat-config.yaml
  
  </code></pre>
  <p>when I run <code>ansible-playbook /opt/init-config/tomcat/tomcat-config.yaml</code> directly in the instance I get an error, and I know it returns 2</p>
  <pre class="lang-sh prettyprint-override"><code>ansible-playbook /opt/init-config/tomcat/tomcat-config.yaml #shows errors
  echo $? # shows &quot;2&quot;
  </code></pre>
  <p><code>00-cloudinit-lifecycle-hook.cfg</code></p>
  <pre><code>#cloud-config
  merge_how:
   - name: list
     settings: [append]
   - name: dict
     settings: [no_replace, recurse_list]
  
  runcmd:
    - &quot;/opt/lifecycles/lifecycle-hook-continue.sh&quot;
  
  </code></pre>
  <p>An alternative I can think of, is to send a ABANDON signal instead of CONTINUE as soon as there's en error in one of the cloud-init config. But I can't find in the documentation on to define if there's an error</p>