4

I have researched Ansible to provision new servers and this works great. My play installs MySQL, Nginx and all the rest. Now I wonder what is the best way to use it as configuration management tool as well.

We run multiple websites on each server. For me this means a general Nginx setup on each server and specific configuration required for each site/domain. I have in mind that I can update both the general setup and the specific configuration independently with Ansible when I need to (let's say new insights or security).

What would I nee to do in this situation? Does this require me to keep a variable that holds all the sites and on which site they are? Or, are there better alternatives?

RikW
  • 151
  • 3

2 Answers2

1

I use vars to do specific stuff to that host in question. My advice is to group as much as you can your hosts in groups and use group variables. Remember that Ansible playbooks are awesome for versioning in git or SVN, so you can keep track of your changes.

Something that might work for you is to use vars_prompt, specially if you don't run your playbooks scheduled on cron.

Hope this helps!

Stefano Martins
  • 1,131
  • 7
  • 10
0

A feature of template that you could use to great benefit for this purpose is its templating capability.

The template module allows you to deploy a file with unique values for each host. The name of the host currently being worked on is kept in the inventory_hostname and inventory_hostname_short variables (the latter being the first dotted component of the hostname).

You could keep the host specific variables in a dictionary in group_vars/all. For example:

www_sites: {
    host1: "www.mysite1.com",
    host2: "www.mysite2.com",
  }

Then the template for the nginx conf file could include something along the following lines:

server {
  listen       80;
  server_name  {{ www_sites[inventory_hostname_short] }};
  access_log   logs/{{ www_sites[inventory_hostname_short] }}.access.log  main;

Documentation for the Jinja2 templating language used by Ansible can be found here.

Leynos
  • 216
  • 2
  • 5
  • So that would require me to have a variable containing all sites per host and store that file when adding a site? – RikW Oct 15 '15 at 10:51
  • @RikW there are various methods for having variables asigned on a per host basis. These include the above group_vars method (which is read by Ansible prior to execution of a play), and the host vars method (see https://docs.ansible.com/ansible/intro_inventory.html). If there are multiple www sites per host, you could use a with_items loop to create multiple domain config files per host. – Leynos Oct 15 '15 at 11:28
  • Thank you @Lenos this is helpful for making up my mind about this. Now I'm wrapping my head around the playbook/roles stuff. When to trigger what in order not re-install thing that are already there. Thanks for the help. – RikW Oct 15 '15 at 13:28
  • in this setup, what is the appropriate way to add a per host config to a single server? In this case, my backup server (Bareos) needs to know of each server in the inventory. – RikW Oct 15 '15 at 19:36