cloud-init: What is the execution order of cloud-config directives?

24,781

From https://git.launchpad.net/cloud-init/tree/config/cloud.cfg (thanks to garbelini)

(As of Sep 2017 the above link is incorrect and is now https://git.launchpad.net/cloud-init/tree/config/cloud.cfg.tmpl, see note below)

# The modules that run in the 'init' stage
cloud_init_modules:
 - migrator
 - ubuntu-init-switch
 - seed_random
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - ca-certs
 - rsyslog
 - users-groups
 - ssh

# The modules that run in the 'config' stage
cloud_config_modules:
# Emit the cloud config ready event
# this can be used by upstart jobs for 'start on cloud-config'.
 - emit_upstart
 - disk_setup
 - mounts
 - ssh-import-id
 - locale
 - set-passwords
 - snappy
 - grub-dpkg
 - apt-pipelining
 - apt-configure
 - package-update-upgrade-install
 - fan
 - landscape
 - timezone
 - lxd
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd
 - byobu

# The modules that run in the 'final' stage
cloud_final_modules:
 - rightscale_userdata
 - scripts-vendor
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change

Additionally:

There is configuration merging "The order is the following: - cli config files override environment config files which override instance configs which override datasource configs which override base configuration which overrides default configuration." (see Changelog)

Within the individual generated script directories the scripts are run in the order given by the python "sorted()" builtin

NOTE: although this was correct at the time of answering, looking at the respository (as of Sept 2017) mentioned above there is now a cloud.cfg.tmpl template file which has slightly different settings for different distros

# The modules that run in the 'init' stage
cloud_init_modules:
 - migrator
 - seed_random
 - bootcmd
 - write-files
 - growpart
 - resizefs
{% if variant not in ["freebsd"] %}
 - disk_setup
 - mounts
{% endif %}
 - set_hostname
 - update_hostname
{% if variant not in ["freebsd"] %}
 - update_etc_hosts
 - ca-certs
 - rsyslog
{% endif %}
 - users-groups
 - ssh

# The modules that run in the 'config' stage
cloud_config_modules:
{% if variant in ["ubuntu", "unknown", "debian"] %}
# Emit the cloud config ready event
# this can be used by upstart jobs for 'start on cloud-config'.
 - emit_upstart
 - snap_config
{% endif %}
 - ssh-import-id
 - locale
 - set-passwords
{% if variant in ["rhel", "fedora"] %}
 - spacewalk
 - yum-add-repo
{% endif %}
{% if variant in ["ubuntu", "unknown", "debian"] %}
 - grub-dpkg
 - apt-pipelining
 - apt-configure
{% endif %}
{% if variant not in ["freebsd"] %}
 - ntp
{% endif %}
 - timezone
 - disable-ec2-metadata
 - runcmd
{% if variant in ["ubuntu", "unknown", "debian"] %}
 - byobu
{% endif %}

# The modules that run in the 'final' stage
cloud_final_modules:
{% if variant in ["ubuntu", "unknown", "debian"] %}
 - snappy
{% endif %}
 - package-update-upgrade-install
{% if variant in ["ubuntu", "unknown", "debian"] %}
 - fan
 - landscape
 - lxd
{% endif %}
{% if variant not in ["freebsd"] %}
 - puppet
 - chef
 - salt-minion
 - mcollective
{% endif %}
 - rightscale_userdata
 - scripts-vendor
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change
Share:
24,781
cgseller
Author by

cgseller

Value people over process Value learners over heroes Value transparency over opacity Value collaboration over competition Value empathy over conformity DevOps promoter Agile fan Leader of change

Updated on April 28, 2020

Comments

  • cgseller
    cgseller almost 4 years

    What is the order of the directives in the cloud-config section of a cloud-init user-data object. This is important to avoid race type conditions.

    I know bootcmd runs early and before runcmd, but is there a good list of the order of all the methods?

  • Tristan
    Tristan over 6 years
    Is there a way to force a change of execution? For instance, I need to make sure to install dirmngr (it's missing in my distro) before apt is ran and attempts to import a source key since it will fail. Then I'll like to continue installing the rest of my packages as normal.
  • dvnguyen
    dvnguyen over 6 years
    Is there any case where the execution order is not respected? In Ubuntu Cloud 16.04 image, runcmd is put in config stage, and package-update-upgrade-install is put in final stage, but I can run runcmd after package install without any error.
  • Vorsprung
    Vorsprung over 6 years
    @Tristan I notice there are now three steps to the apt/dpkg config, mayeb you could hook one of them?
  • Vorsprung
    Vorsprung over 6 years
    @dvnguyen I don't know how the semantics exactly work out. You should open a question with a simple example of this and see if you can find a real expert...sounds interesting
  • dvnguyen
    dvnguyen over 6 years
    @Vorsprung I made a question here: stackoverflow.com/questions/46230371/…
  • David
    David almost 6 years
    This isn't correct for AWS Ubuntu 16.04 imag. runcmd is definitely happening after packages.