Different deployment environments for every website

I’ve setup 2 total different websites on my Trellis VM.
Both website are located on different hosting partners.
On none of them, I can setup my own server (although I would prefer Trellis :wink: ).

How can I define the variables for all the environments (dev, staging, production) per website within Trellis?
I can deploy 1 website, but the others haver different users, paths, keys, … And it’s needles complex to change the settings everytime.
Should I setup a new server for every website?

1 Like

Here are some ideas. Not 100% certain I understand your goal. Untested.

For each environment, list all sites, e.g.,

# group_vars/production

wordpress_sites:
  site-1.com:
    site_hosts:
      - site-1.com
    local_path: ../site-1
    repo: git@github.com:mygitusername/bedrock-1.git
    ⋮
    env:
      wp_home: http://site-1.com
      ⋮
  site-2.com:
    site_hosts:
      - site-2.com
    local_path: ../site-2
    repo: git@github.com:mygitusername/bedrock-2.git
    ⋮
    system_cron: true
    env:
      wp_home: http://site-2.com
      ⋮

For all environments, list hosts you’ll deploy to, with “behavioral inventory parameters” for each, e.g.,

# hosts/production

[web]
site-1.com ansible_ssh_host=IP_for_host_1 ansible_ssh_user=host_1_username ansible_ssh_private_key_file=/local/path/to/private_key/for/host_1
site-2.com ansible_ssh_host=IP_for_host_2 ansible_ssh_user=host_2_username ansible_ssh_private_key_file=/local/path/to/private_key/for/host_2

[production:children]
web

Potentially adjust deploy.yml playbook, e.g.,

# deploy.yml

 - name: Deploy WP site
-  hosts: web
+  hosts: "{{ site }}" # Optional: Adjust `deploy.yml` to only ever deploy to a single host (vs. deploying to all hosts in the `web` group)
-  remote_user: "{{ web_user }}" # might be able to just leave this -- might be overriden by ansible_ssh_user

Deploy commands – If you adjusted deploy.yml hosts parameter:

  • To deploy site-1 (production): ./deploy.sh production site-1.com
  • To deploy site-2 (production): ./deploy.sh production site-2.com

Deploy commands – If you did not adjust deploy.yml hosts parameter, you’ll need the --limit option:

  • To deploy site-1 (production):
    ansible-playbook deploy.yml -i hosts/production -e "site=site-1.com" --limit site-1.com
  • To deploy site-2 (production):
    ansible-playbook deploy.yml -i hosts/production -e "site=site-2.com" --limit site-2.com

Does this address your question? What questions remain?

4 Likes

Thanks!

How can I set variables like web_user, www_root, … for every site seperate?

The group_vars/all are intended for all hosts but you could move some of these variable definitions to host-specific vars files in a new directory named host_vars, (similar to group_vars dir), e.g.,

host_vars/
    site-1.com  # vars file specific to host named site-1.com
    site-2.com  # vars file specific to host named site-2.com

Here are some notes on variable precedence in Ansible. There is some similar discussion over at roots/trellis#216

If you only need web_user and www_root to be different for the deploy.yml playbook, you might see if variable precedence allows the following to work. I haven’t tested this.

Add to each site in your wordpress_sites:

wordpress_sites:
  site-1.com:
    web_user: someuser
    www_root: somepath
    ⋮
  site-2.com:
    web_user: otheruser
    www_root: otherpath
    ⋮

Then edit deploy.yml to call those site variables while defining the variables you want.

vars:
  project: "{{ wordpress_sites[site] }}"
  project_root: "{{ www_root }}/{{ site }}"
+ web_user: "{{ project.web_user }}"
+ www_root: "{{ project.www_root }}"
1 Like