Dmitry
Dmitry

Reputation: 7553

Ansible: playbooks and roles difference

I have 4 servers: production web, production db, staging web+db, development web+db (actually there are many dev servers, but they are used on localhosts using vagrant).
Config vars are different for these playbooks.

Actually I can use 4 playbooks, which include other yml files (nginx.yml, php.yml, mysql.yml), and I don't have to use roles.
Is it correct?

How should I organize my ansible files?

Upvotes: 2

Views: 2599

Answers (1)

Saeed
Saeed

Reputation: 600

Ansible is flexile enough to work either way. You can proceed with two aproaches

  • Define roles (i.e. production web, production db, staging web+db, development web+db) and under roles have tasks (nginx, php, mysql)
  • Define each module as role (i.e. nginx role, php role, mysql role) and then specify which roles are included in which playbook.

First way is better for cases that every php setup is different for every environment. Second way is better for cases that all your php setups are the same, and it would be cumbersome to maintain 4 similar setup/config; you would write them once, and call them by their local variables.

Example of second one

./ansible-playbook roles/role1/tasks/production-web.yml -i hosts.yml

- name: playbook name
  hosts: host1
  sudo: yes
  
  vars:
    - php_version: 5.6.16

  vars_files:
    - "vars/local_vars"

  pre_tasks:
    - include: system-preparation.yml

    - include: user-setup.yml  

  tasks:
    - debug: msg="Installing and configuring PHP"
    
    - include: php-setup.yml
    
    - include: php-configuration.yml

  post_tasks:
    - include: health-check.yml

  handlers:

Upvotes: 2

Related Questions