0

I'm running several LEMP VMs with mostly identical setups. Each of these VMs has a /etc/profile.d/00-aliases.sh containing a bunch of aliasses and functions to run on the server. Now, whenever I make a change to those aliasses/functions I have to manually copy/paste the file to the other servers.

I want to load the file from an external source every time I log in via SSH. I've seen this behaviour on other servers with a "Loading external scripts.." prompt on login. On top of that it would need some kind of local variables for the functions and some variables because there are slight variations in themes and foldernames on each server.

So best case scenario I'd have something like this for the variables (pseudocode):

var sitename = SiteA
var sitepath = /html/path/

and then one global bash.rc/aliasses file like this:

alias goroot = 'cd {sitepath}'
alias delses = 'rm -rf /var/session/'
...

Is this possible (with the variables), if yes, how? Where do I start to look?

Asitis
  • 97
  • 8
  • 1
    Why don't you deploy the file with ansible or something similar? – Gerald Schneider Sep 24 '18 at 08:56
  • Sure that would work :) I'm using Gcloud Compute Engine; that makes sharing files between instances pretty easy. My main issue is how to get the variables working, because just having one static file shared between them only solves a part of the problem. Any ideas on that one? :) – Asitis Sep 24 '18 at 08:59
  • 1
    ansible has host- and groupvars that can be automatically used during deployment. I'm not familiar with other systems, but I assume they have something similar. – Gerald Schneider Sep 24 '18 at 09:02

1 Answers1

2

You can use ansible with the template module to deploy your files:

.ansible/inventory

[lemp-hosts]
host1
host2
...

.ansible/group_vars/lemp-hosts.yml

profile_d: /etc/profile.d/

.ansible/host_vars/host1.ansible

sitename: SiteA
sitepath: /html/path/

templates/00-aliases.sh

alias goroot = 'cd {{ sitepath }}'
alias delses = 'rm -rf /var/session/'
...

playbooks/deploy_aliases.yml

---

- hosts lemp-hosts
  tasks:
  - name: copy aliases.sh
    template:
      src: templates/00-aliases.sh
      dest: "/{{ profile_d }}/00-aliases.sh"

Create an inventory, along with group- and hostvar files specific to your hosts on your control host. Then you can deploy the file with ansible-playbook playbooks/deploy_aliases.yml to all your hosts at once. Of course you can deploy all your configuration with a single playbook, ansible will only change files that have been changed. Manage your ansible directory with git and you even have revisions of your configuration in a single place.

Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
  • Thanks for the detailed answer! I guess I'll forgo the known possibilities with gcloud and see if I can use ansible for this then :) – Asitis Sep 24 '18 at 09:35