- Ansible Playbook Essentials
- Gourav Shah
- 781字
- 2021-07-16 20:47:48
The directory layout for roles
Roles are nothing but directories laid out in a specific manner. Roles follow predefined directory layout conventions and expect each component to be in the path meant for it.
The following is an example of a role, called Nginx:

Let's now look at the rules of the game and what each of the components in the preceding diagram is for:
- Each role contains a directory which is named after itself, for example,
Nginx
, withroles/
as its parent directory. Each named role directory contains one or more optional subdirectories. The most common subdirectories to be present are tasks, templates, and handlers. Each of these subdirectories typically contain themain.yml
file, which is a default file. - Tasks contain the core logic, for example, they will have code specifications to install packages, start services, manage files, and so on. If we consider a role to be a movie, a task would be the protagonist.
- Tasks alone cannot do everything. Considering our analogy with movies, it's incomplete without the supporting cast. Protagonists have friends, cars, lovers, and antagonists to complete the story. Similarly, tasks consume data, call for static or dynamic files, trigger actions, and so on. That's where files, handlers, templates, defaults, and
vars
come in. Let's look at what these are for. - Vars and defaults provide data about your application/role, for example, which port your server should run on, the path for storing the application data, which user to run the service as, and so on. Default variables were introduced in version 1.3 and these allow us to provide sane defaults. These can later be overridden from other places, for example,
vars
,group_vars
, andhost_vars
. Variables are merged and precedence rules apply. This gives us a lot of flexibility to configure our servers selectively. For example, running the web server on port80
on all hosts except for the ones in the staging environment, which should run it on port8080
. - Files and templates subdirectories provide options for managing files. Typically, the files subdirectory is used to copy over static files to destination hosts, for example, some application installers archive static text files, and so on. In addition to static files, frequently you may need to manage files that are are generated on the fly. For example, a configuration file that has parameters such as port, user, and memory, which can be provided dynamically using variables. Generating such files requires a special type of primitive, called templates.
- Tasks can trigger actions based on the change of a state or a condition. In a movie, the protagonist may chase the antagonist and take revenge based on the provocation or an event. An example event is kidnapping the protagonist's lady love. Similarly, you may need to perform an action on your hosts, for example, restarting a service based on what happened earlier, which could be a change in the state of a configuration file. This trigger-action relationship can be specified using a handler.
Continuing our analogy, many popular movies have sequels and sometimes even prequels. In such cases, one should watch it in a particular order, as the storyline of a sequel depends on something that happened in the previous movie. Similarly, a role can have a dependency on another role. A very common example is, before installing Tomcat, Java should be present on the system. These dependencies are defined in the meta subdirectory of a role.
Let's get hands-on with this by creating a role for the Nginx application. Let's take a problem statement, try to solve it, and learn about roles in the process.
Consider the following scenario. With the onset of the soccer world cup, we need to create a web server to serve up a page on sports news.
Being a follower of agile methodologies, we will do this in phases. In the first phase, we will just install a web server and serve up a home page. Let's now break this down into the steps we need to take to achieve this:
- Install a web server. In this case, we will use 'Nginx' as it is a lightweight web server.
- Manage configuration for the Nginx web server.
- Start the web server after installing it.
- Copy over an HTML file, which will be served as a home page.
Now that we have identified what steps to take, we also need to map them to the respective module types we will use to achieve each of these:
- Installing Nginx = Package module (apt)
- Configuring Nginx = File module (file)
- Starting Nginx = Systems module (service)
- Serve Webpage = Files module (file)
Before we start writing code, we will start creating a layout to organize our files.
- Instant Node Package Manager
- Java程序設計(慕課版)
- Learn TypeScript 3 by Building Web Applications
- Vue.js前端開發基礎與項目實戰
- 重學Java設計模式
- Hands-On Natural Language Processing with Python
- 用戶體驗增長:數字化·智能化·綠色化
- OpenCV with Python By Example
- 現代C:概念剖析和編程實踐
- 監控的藝術:云原生時代的監控框架
- Python滲透測試編程技術:方法與實踐(第2版)
- Linux Networking Cookbook
- Python人工智能項目實戰
- Python 3.8編程快速入門
- Hands-On Game Development Patterns with Unity 2019