官术网_书友最值得收藏!

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:

The directory layout for roles

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, with roles/ 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 the main.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, and host_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 port 80 on all hosts except for the ones in the staging environment, which should run it on port 8080.
  • 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:

  1. Install a web server. In this case, we will use 'Nginx' as it is a lightweight web server.
  2. Manage configuration for the Nginx web server.
  3. Start the web server after installing it.
  4. 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.

主站蜘蛛池模板: 阿拉善右旗| 巫山县| 大渡口区| 宜州市| 高唐县| 天长市| 德钦县| 富川| 陈巴尔虎旗| 梅州市| 马尔康县| 织金县| 凯里市| 永城市| 平度市| 宽城| 连南| 桑日县| 南充市| 邵阳县| 济南市| 丰县| 太原市| 报价| 佛坪县| 新兴县| 通辽市| 喜德县| 明水县| 盐池县| 石嘴山市| 沂南县| 元江| 鞍山市| 西充县| 元朗区| 嵊泗县| 贵州省| 托里县| 太湖县| 缙云县|