Tip

While choosing between the vars and defaults directories, the rule of thumb is to put variables that will not change within the vars directory. Put variables that are likely to change within the defaults directory.

So, we’ll go and use the defaults directory as much as we can. There are some best practices regarding roles that we should follow as well. Let’s look at some of them.

Tip

Think about the full life cycle of a specific service while designing roles rather than building the entire stack—in other words, instead of using lamp as a role, use apache and mysql roles instead.

We will create three roles for our use—common, apache, and mysql.

Tip

Use specific roles, such as apache or mysql, instead of using webserver or dbserver.
Typical enterprises have a mix and match of multiple web servers and database technologies.
Therefore, giving a generic name to a role will confuse things.

The following directory contains all the resources for the exercises in this section:
$ cd ~/modern-devops/ch9/lamp-stack-roles

The following is the directory structure we will follow for our scenario:
├── ansible.cfg
├── hosts
├── output.log
├── playbook.yaml

There are three roles that we’ll create—apache, mysql, and common. Let’s look at the directory structure of the apache role first:
└── roles
├── apache
│ ├── defaults
│ │ └── main.yaml
│ ├── handlers
│ │ └── main.yaml
│ ├── tasks
│ │ ├── install-apache.yaml
│ │ ├── main.yaml
│ │ └── setup-apache.yaml
│ └── templates
│ └── index.php.j2

There is also a common role that will apply to all scenarios. The following directory structure defines that:
├── common
│ └── tasks
│ └── main.yaml

Finally, let’s define the mysql role through the following directory structure:
└── mysql
├── defaults
│ └── main.yaml ├── files
├── handlers
│ └── main.yaml ├── tasks
│ ├── install-mysql.yaml
│ ├── main.yaml
│ └── setup-mysql.yaml └── templates
└── client.my.cnf.j2

The apache directory consists of the following:
• We’ve used the same index.php file we created in the last exercise, converted it to a Jinja2 template called index.php.j2, and copied it to roles/apache/templates.
• The handlers directory contains a main.yaml file that contains the Restart Apache handler.

• The tasks directory contains an install-apache.yaml file that includes all tasks required to install Apache. The setup-apache.yaml file consists of a list of tasks that will set up Apache, similar to what we did in the previous exercise. The main.yaml file contains tasks from both files, using include directives such as the following:
  • include: install-apache.yaml
  • include: setup-apache.yaml
    • The defaults directory contains the main.yaml file, which contains the mysql_username and mysql_password variables and their default values.

Tip

Use as few variables as possible and try to default them. Use defaults for variables in such a way that minimal custom configuration is needed.

The mysql directory consists of the following:
• We’ve modified client.my.cnf and converted that to a j2 file. The j2 file is a Jinja2 template file we will use in the role through the template module in the Set the root password task. The file exists within the templates directory:
[client]
user=root
password={{ mysql_root_password }}

As we can see, we’re providing the password through a Jinja2 expression. When we run the mysql role through the playbook, the value of mysql_root_password will be substituted in the password section.

• The handlers directory contains the Restart MySQL handler.

• The tasks directory consists of three files. The install-mysql.yaml file contains tasks that install mysql, and the setup-mysql.yaml file contains tasks that set up mysql. The main.yaml file combines both these files using include task directives, as follows:
  • include: install-mysql.yaml
  • include: setup-mysql.yaml
    • The defaults directory contains a main.yaml file with a list of variables we will use within the role. In this case, it just contains the value of mysql_root_password.

Leave a Reply

Your email address will not be published. Required fields are marked *