3

When I start a linux server with Cloud-init, I have a few scripts in /etc/cloud/cloud.cfg.d/ and they run in reverse alphabetical order

# 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

The last to execute is 00-cloudinit-lifecycle-hook.cfg, in which I complete the lifecycle for the Auto Scaling Group with a CONTINUE. The ASG fails if it doesn't receive this signal after a given time out.

The issue is that even if there's an error in 50-cloudinit-tomcat.cfg, it still runs 00-cloudinit-lifecycle-hook.cfg instead of stopping

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.

Here are the files:

EC2 instance user-data:

#cloud-config

bootcmd:
  - [cloud-init-per, once, "app-volume", mkfs, -t, "ext4", "/dev/nvme1n1"]

mounts:
   - ["/dev/nvme1n1", "/app-volume", "ext4", "defaults,nofail", "0", "0"]

merge_how:
  - name: list
    settings: [append]
  - name: dict
    settings: [no_replace, recurse_list]

50-cloudinit-tomcat.cfg

#cloud-config
merge_how:
 - name: list
   settings: [append]
 - name: dict
   settings: [no_replace, recurse_list]

runcmd:
  - "#!/bin/bash -e"
  - 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

when I run ansible-playbook /opt/init-config/tomcat/tomcat-config.yaml directly in the instance I get an error, and I know it returns 2

ansible-playbook /opt/init-config/tomcat/tomcat-config.yaml #shows errors
echo $? # shows "2"

00-cloudinit-lifecycle-hook.cfg

#cloud-config
merge_how:
 - name: list
   settings: [append]
 - name: dict
   settings: [no_replace, recurse_list]

runcmd:
  - "/opt/lifecycles/lifecycle-hook-continue.sh"

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

chriscatfr
  • 2,592
  • 3
  • 24
  • 32

0 Answers0