+

Search Tips   |   Advanced Search

Sample Ansible setup

You have learned about playbooks, inventory, roles, and variables. This section pulls all those elements together, outlining a sample setup for automating a web service. You can find more example playbooks illustrating these patterns in our ansible-examples repository. (NOTE: These may not use all of the features in the latest release, but are still an excellent reference!).

The sample setup organizes playbooks, roles, inventory, and variables files by function, with tags at the play and task level for greater granularity and control. This is a powerful and flexible approach, but there are other ways to organize Ansible content. Your usage of Ansible should fit your needs, not ours, so feel free to modify this approach and organize your content as you see fit.


Sample directory layout

This layout organizes most tasks in roles, with a single inventory file for each environment and a few playbooks in the top-level directory:


Alternative directory layout

Alternatively you can put each inventory file with its group_vars/host_vars in a separate directory. This is particularly useful if your group_vars/host_vars don't have that much in common in different environments. The layout could look something like this:

This layout gives you more flexibility for larger environments, as well as a total separation of inventory variables between different environments. However, this approach is harder to maintain, because there are more files. For more information on organizing group and host variables, see Organizing host and group variables.


Sample group and host variables

These sample group and host variables files record the variable values that apply to each machine or group of machines. For instance, the data center in Atlanta has its own NTP servers, so when setting up ntp.conf, we should use them:

Similarly, the webservers have some configuration that does not apply to the database servers:

Default values, or values that are universally true, belong in a file called group_vars/all:

If necessary, you can define specific hardware variance in systems in a host_vars file:

Again, if you are using dynamic inventory, Ansible creates many dynamic groups automatically. So a tag like 'class:webserver' would load in variables from the file 'group_vars/ec2_tag_class_webserver' automatically.


Sample playbooks organized by function

With this setup, a single playbook can define all the infrastructure. The site.yml playbook imports two other playbooks, one for the webservers and one for the database servers:

The webservers.yml file, also at the top level, maps the configuration of the webservers group to the roles related to the webservers group:

With this setup, you can configure your whole infrastructure by 'running' site.yml, or run a subset by running webservers.yml. This is analogous to the Ansible '-limit' parameter but a little more explicit:


Sample task and handler files in a function-based role

Ansible loads any file called main.yml in a role sub-directory. This sample tasks/main.yml file is simple - it sets up NTP, but it could do more if we wanted:

Here is an example handlers file. As a review, handlers are only fired when certain tasks report changes, and are run at the end of each play:

See Roles for more information.


What the sample setup enables

The basic organizational structure described above enables a lot of different automation options. To reconfigure your entire infrastructure:

To reconfigure NTP on everything:

To reconfigure only the webservers:

To reconfigure only the webservers in Boston:

To reconfigure only the first 10 webservers in Boston, and then the next 10:

The sample setup also supports basic ad-hoc commands:

To discover what tasks would run or what hostnames would be affected by a particular Ansible command:


Organizing for deployment or configuration

The sample setup models a typical configuration topology. When doing multi-tier deployments, there are going to be some additional playbooks that hop between tiers to roll out an application. In this case, 'site.yml' may be augmented by playbooks like 'deploy_exampledotcom.yml' but the general concepts still apply. Ansible allows you to deploy and configure using the same tool, so you would likely reuse groups and keep the OS configuration in separate playbooks or roles from the app deployment.

Consider 'playbooks' as a sports metaphor - you can have one set of plays to use against all your infrastructure and situational plays that you use at different times and for different purposes.


Using local Ansible modules

If a playbook has a ./library directory relative to its YAML file, this directory can be used to add Ansible modules that will automatically be in the Ansible module path. This is a great way to keep modules that go with a playbook together. This is shown in the directory structure example at the start of this section.


See also

YAML Syntax

Learn about YAML syntax

Working with playbooks

Review the basic playbook features

Collection Index

Browse existing collections, modules, and plugins

Should you develop a module?

Learn how to extend Ansible by writing your own modules

Patterns: targeting hosts and groups

Learn about how to select hosts

GitHub examples directory

Complete playbook files from the github project source

Mailing List

Questions? Help? Ideas? Stop by the list on Google Groups

Next Previous