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

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.

主站蜘蛛池模板: 运城市| 寿阳县| 三都| 瑞金市| 柳河县| 西丰县| 浮山县| 涟水县| 灌南县| 汪清县| 河西区| 定日县| 平果县| 腾冲县| 蓝山县| 宜春市| 乐山市| 蚌埠市| 五莲县| 东光县| 仁布县| 盖州市| 阿瓦提县| 习水县| 监利县| 南投市| 溧阳市| 志丹县| 舞阳县| 昌黎县| 密山市| 图片| 苏尼特左旗| 思茅市| 八宿县| 永泰县| 林周县| 广南县| 瑞丽市| 乡城县| 资源县|