ansible - deploy a 3 tiers application on 3 different machines -
i have basic application with:
- db
- api (connected db)
- front end (connected http api)
i'd 3 services run on 3 differents machines. i'm using ansible perform i'm not quite sure how define environment variables each layer knows how connect underlying one.
api needs db_host , db_port connect db. needs provided port own usage.
front end needs api_host , api_port connect api. needs provided port own usage.
inventory.ini
[www] 192.168.1.120 [api] 192.168.1.120 [db] 192.168.1.120 [myproj:children] www api db [myproj:vars] db_host=192.168.1.120 // needed api db_port=27017 // needed api api_host=192.168.1.120 // needed front-end api_port=8000 // needed api + front-end www_port=8001 // needed front-end note: test use 1 vagrant vm 3 machines distinct in production.
playbooks.yml
--- - hosts: db sudo: true roles: - common - db - hosts: api sudo: true roles: - common - api - hosts: www sudo: true roles: - common - www example api roles: (roles/api/tasks/main.yml)
--- - name: clone api git: repo=git@github.com:... - name: install api packages ... - name: start api sudo: false shell: npm start args: chdir: /var/app environment: port: ??? db_host: ??? db_port: ??? i'd have db_host / db_port / port setup hostvars not manage retrieve variable i've set in inventory. way handle ? should declare variables somewhere else ?
i think can refactoring. having variables in inventory file against ansible best prices. page describes optimal layout of playbooks http://docs.ansible.com/playbooks_best_practices.html#content-organization
furthermore, in inventory file www, api , db group names. can this:
[www] web01 ansible_ssh_host=192.168.1.120 [api] api01 ansible_ssh_host=192.168.1.120 [db] db01 ansible_ssh_host=192.168.1.120 this way can refer servers using bit friendlier names, making playbooks more readable.
as getting, example database ip, can use host facts extract such:
{{ hostvars[db01]['ansible_eth0']['ipv4']['address'] }} or, if want via index group:
{{ hostvars[groups['db'][0]]['ansible_eth0']['ipv4']['address'] }} you should take @ other facts ansible collects , see if there suits use-case.
Comments
Post a Comment