Using Ghost-CLI programatically

Tips and tricks for installing ghost with ghost cli via scripts or configuration management

Ghost-CLI is primarily an interactive command line tool for installing Ghost - but that's not all it can do!

It is also possible to use Ghost-CLI entirely programmatically, as part of a bash script or configuration management setup. We use the CLI with ansible internally at Ghost HQ, and if you're a fan of configuration management, we'd recommend that you still use Ghost CLI for installing Ghost itself. By using flags to pass in required data and disable unwanted steps, it is possible to control aspects of the CLI without ever needing to fake answering a prompt!

When using Ghost-CLI programmatically, the recommended system stack and prerequisites are the same as when using it interactively. You'll also use all the same commands, but with some extra flags to disable prompting and control which steps happen when.

stdout & stderr

Most of the output from Ghost-CLI goes to stdout. When the CLI encounters an error, it will write this to stderr and exit with a non-zero status code. This makes it easy to detect when a command results in an error in a programmatic environment. Configuration management tools like ansible will automatically behave as expected.

When used with ansible's command module, or in a bash script, the output sent to stdout will be verbose output. Meaning it is very easy to understand exactly what the CLI has done, and at what point the tool failed, if it encounters an error.

--no-prompt

This is the main flag you'll need to pass to all commands when running Ghost programmatically. This will disable all interactive prompts within the CLI.

If the CLI encounters a situation where it doesn't have the information it needs to continue, it will error instead of prompt. Therefore, when calling commands with --no-prompt, it is important to also supply all prerequisite information. See the command reference <TODO: link to command ref?> for details of the parameters that each command requires.

Install & Setup

The ghost install command will install Ghost & its dependencies, and then run through a full setup of the environment.

If you'd prefer to do these two steps separately, this can be overridden by passing --no-setup to ghost install, and then running ghost setup on its own afterwards.

System stack checks

System stack checks (i.e. ghost doctor) will only be performed automatically as part of ghost install.

If you want to skip the system stack checks as an optimisation, this can be done by passing --no-stack to ghost install. Please only do this after having checked that your script will create the right environment for Ghost first. Removing the stack checks removes Ghost-CLI's best chance at giving you a sane error message 😉

If you do choose to skip these checks, and then hit an error, please please run ghost doctor to check your system is OK 🚑.

ghost setup --no-prompt (+config flags) performs a default stack setup (mysql, nginx, ssl & systemd). Each stage of a setup can be skipped by passing --no-setup-<stage name>. Some stages can be replaced with alternatives, for example --db=sqlite3 will change which database is used and skip the MySQL setup (or it will, after https://github.com/TryGhost/Ghost-CLI/issues/304 is merged) See the ghost setup docs <TODO: link to setup docs> for more information on the various stages and how to use non-default options.

Full Install, Setup, Config & Run in one command

Ensure that your domain is already pointed at your server and the DNS has resolved. Once that is done, you can run the following command and Ghost will be installed and configured, including mail options for mailgun, nginx will be setup with certificates via letsencrypt, systemd will be configured and Ghost will be running

With CloudFlare's API, DNS can be setup as part of a script. Using the proxy:true option means your DNS will be ready instantly, and it will be possible to setup SSLL immediately.

ghost install --url=https://{{ server_domain }} --db=mysql --dbhost=localhost --dbuser=ghost --dbpass={{ mysql_ghost_password }} --dbname=ghost --mail=SMTP --mailservice=Mailgun --mailuser={{ mail_user }} --mailpass={{ mail_pass }} --sslemail={{ letsencrypt_email }} --no-prompt --start

If you need to pause for DNS resolution and setup SSL later, use --no-setup-ssl to skip SSL on the initial setup, and then run ghost setup ssl --sslemail={{ letsencrypt_email }} when your DNS is ready.

Updating Ghost

ghost update is one of the few commands that doesn't require --no-prompt. It should "just work" regardless of whether it's run via a program or interactively. Magic :sparkles+: !

Ghost-CLI & Ansible

Coming soon: a repository full of useful things for running ghost-cli with ansible!

For now you can reference this (slightly out of date already) gist: https://gist.github.com/ErisDS/0edb9c2bca72f99b6a428ae9f17cb747

Using Ghost-CLI programatically

Tips and tricks for installing ghost with ghost cli via scripts or configuration management