note: I had to replace part of the used hosts names as the forum sees them as links. Used an underscore instead of a point.
I’ve been struggeling to set-up a correct ansible structure for multiple servers and local test environmets that I can extend without to much trouble. Let me try to explain it.
We have various servers that I want to maintain through ansible. Servers include, but not limited to nodejs (with nginx), mariaDB servers and a few plesk servers. All the ansible related files are put into version controll in a single git repository.
The idea is that all the servers are using ansible-pull to pull this git repository and execute one of the playbooks at a set time(s) a day. For testing purposes I want to be able to run the same playbooks on a virtualbox vm using vagrant. Where vagrant creates the vm’s in virtualbox and executes an intial playbook to prepare the vm. And afterwards runs one of the playbooks from the ansible repository.
My current ansible structure is as follows:
|-- inventory
| |-- group_vars
| | |-> vagrant.yml
| |-- host_vars
| | |-- nodejs
| | | |-> sites.yml
| |-> vagrant.yml
|-- playbooks
| |-- nodejs
| | |-> playbook.yml
| | |-> vagrant.yml (can be the same as playbook.yml with vagrant checks)
| |-- mariadb
| | |-> playbook.yml
| | |-> vagrant.yml
| |-- ...
|-- roles
| |-- ansible
| |-- common
| |-- nginx (uses the 'sites' variable to create host files)
| |-- nodejs
| |-- sudo
| |-- users (uses the 'users' variable to create users)
| |-- ...
|-> ansible.cfg
The following list nodes are used. These are fictive hostnames:
production:
- nodejs01.infra_com (running ansible-playbook nodejs playbook.yml)
- nodejs02.infra_com (running ansible-playbook nodejs playbook.yml)
- nodejsne.infra_eu (running ansible-playbook nodejs playbook.yml)
- db01.infra_com (running ansible-playbook mariadb playbook.yml)
- db02.infra_com (running ansible-playbook mariadb playbook.yml)
testing:
- nodejs_test (ansible-playbook nodejs vagrant.yml)
- db_test (ansible-playbook nodejs vagrant.yml)
The vagrant environment will set-up the local vm’s by using a config file (created/updated by the developer) to define what nodes he wants to create. Their hostnames, domains that should be created on them and some other stuff. Based on that configuration file vagrant creates a sites.yml file that contains all the information that ansible needs to create the nginx host files for that particular node. Each node (nodje01.infra_com, nodejs02.infra_com, nodejsne.infra_com, nodejs_test etc) must have their own unique sites.yml file. For an example of a sites.yml file see below.
*** sites.yml ***
---
sites:
- name: site1
port: 3000
fqdn: 'test1_test'
- name: site2
port: 3001
fqdn: 'test2_test'
The problem that I’m facing or rather I’m confused about is the hosts specified in the playbooks and their relation to the various configurations files needed for each node. Where to put them and how to name them. Recall that I primarely use ansible-pull for the moment. But might later on use ansible-push with the same git repository. So that I will be able to maintain all servers from a central ansible host if I want/need to in the future.
I was thinking to have a folder somewhere with a sub folder for each node and put all node releated data in those sub folders. inventory/host_vars for example. Or maybe in the root of the ansible repositiry. The idea is that that is easely maintainable.
For example:
(part of the git repository)
|-- nodejs01.infra_com
| |-> sites.yml
|-- nodejs02.infra_com
| |-> sites.yml
|-- nodejsne.infra_com
| |-> sites.yml
(automatically created localy by vagrant, based on the specified vagrant config file)
|-- nodejs_test
| |-> sites.yml
|-- db_test
| |-> sites.yml
But I’m struggeling to get this working in ansible. The whole inventory file(s) and the relation to the hosts in the playbooks. I can’t seem to wrap my head around it. If anyone is able to shed some light on that or have an idea how I can structure it I’m all ears.