I'm prototyping Ansible using Vagrant and am using "vagrant provision" to build my stack. I've defined roles to build a core system, a webserver, a database server, and an application server. (The core system would be the roles that would be required for any system type, such as installing system packages and users).
I'd like to configure Vagrant to execute all the roles (to install everything on a single vm), but in a production environment, I might want to install only certain roles on certain machines. What would be the best way to structure my playbook and inventory file such that I can accommodate the single server Vagrant machine and a multi-server production machine group?
Would that involve creating multiple playbooks? One for Vagrant and one for each other configuration? If so, that might lead to a lot of duplication of code.
Here is what I have so far, but it's not working...
Here's the full project: https://github.com/JoeJasinski/docker-ansible-web
Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.provision :ansible do |ansible|
ansible.playbook = "ansible/site.yml"
ansible.verbose = "vvv"
ansible.extra_vars = {}
ansible.inventory_path = "ansible/inventory.ini"
ansible.sudo = true
ansible.groups = {
"nginxServer" => ["nginxServer",],
"djangoServer" => ["djangoServer",],
"postgresServer" => ["postgresServer",]
}
end
inventory.ini
[nginxServer]
127.0.0.1
[djangoServer]
127.0.0.1
[postgresServer]
127.0.0.1
site.yml
---
- name: Base states
hosts: all
vars:
- update_apt_cache: yes
vars_files:
- env_vars/base.yml
roles:
- role: base
- { role: users, tags: ['users'] }
- { role: sites, tags: ['sites'] }
- name: Nginx Host
hosts: nginxServer
vars:
- update_apt_cache: yes
vars_files:
- env_vars/base.yml
roles:
- { role: supervisor, tags: ['supervisor'] }
- { role: nginx, tags: ['nginx'] }
- name: Django Host
hosts: djangoServer
vars:
- update_apt_cache: yes
vars_files:
- env_vars/base.yml
roles:
- { role: supervisor, tags: ['supervisor'] }
- { role: django, tags: ['django'] }
- name: Postgres Host
hosts: postgresServer
vars:
- update_apt_cache: yes
vars_files:
- env_vars/base.yml
roles:
- { role: postgres, tags: ['postgres'],}
- { role: postgis, tags: ['postgis'], }