From charlesreid1

(Created page with "==Option 1== Here is a suggested directory layout from the [https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html?highlight=roles#alternative-direc...")
 
Line 1: Line 1:
==Option 1==
==Option 1: one size fits all==


Here is a suggested directory layout from the [https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html?highlight=roles#alternative-directory-layout Ansible documentation].
Here is a suggested directory layout from the [https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html?highlight=roles#alternative-directory-layout Ansible documentation].
Line 73: Line 73:
     fooapp/              # ""
     fooapp/              # ""
</pre>
</pre>


==Option 2==
==Option 2==

Revision as of 02:26, 14 November 2018

Option 1: one size fits all

Here is a suggested directory layout from the Ansible documentation.

This keeps production/staging environment host files separate.

group_vars folder contains yaml files that define variables for each of the groups we define.

host_vars folder contains yaml files that define variables for each of the hosts.

site.yml is the master playbook that defines all the plays.

There is also a sub-playbook for each "tier" - the web server and the database server, for example.

Each role can either use the default or define its own set of each of the following:

  • tasks
  • handlers
  • templates
  • files
  • variables
  • default values

The "common" folder defines the above for all roles.

The "webserver" folder would define the above for all hosts playing a webserver role.

The "fooapp" folder would define the above for all hosts hosting the fooapp.

Etc...

production                # inventory file for production servers
staging                   # inventory file for staging environment

group_vars/
   group1.yml             # here we assign variables to particular groups
   group2.yml
host_vars/
   hostname1.yml          # here we assign variables to particular systems
   hostname2.yml

library/                  # if any custom modules, put them here (optional)
module_utils/             # if any custom module_utils to support modules, put them here (optional)
filter_plugins/           # if any custom filter plugins, put them here (optional)

site.yml                  # master playbook
webservers.yml            # playbook for webserver tier
dbservers.yml             # playbook for dbserver tier

roles/
    common/               # this hierarchy represents a "role"
        tasks/            #
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #
            main.yml      #  <-- handlers file
        templates/        #  <-- files for use with the template resource
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #
            bar.txt       #  <-- files for use with the copy resource
            foo.sh        #  <-- script files for use with the script resource
        vars/             #
            main.yml      #  <-- variables associated with this role
        defaults/         #
            main.yml      #  <-- default lower priority variables for this role
        meta/             #
            main.yml      #  <-- role dependencies
        library/          # roles can also include custom modules
        module_utils/     # roles can also include custom module_utils
        lookup_plugins/   # or other types of plugins, like lookup in this case

    webtier/              # same kind of structure as "common" was above, done for the webtier role
    monitoring/           # ""
    fooapp/               # ""

Option 2

An alternative layout that keeps the inventory file with the corresponding group and host variable files.

This is useful when the variable files for each environment are not alike, so it is easier not to try and share things.

inventories/
   production/
      hosts               # inventory file for production servers
      group_vars/
         group1.yml       # here we assign variables to particular groups
         group2.yml
      host_vars/
         hostname1.yml    # here we assign variables to particular systems
         hostname2.yml

   staging/
      hosts               # inventory file for staging environment
      group_vars/
         group1.yml       # here we assign variables to particular groups
         group2.yml
      host_vars/
         stagehost1.yml   # here we assign variables to particular systems
         stagehost2.yml

library/
module_utils/
filter_plugins/

site.yml
webservers.yml
dbservers.yml

roles/
    common/
    webtier/
    monitoring/
    fooapp/

Flags