- Ansible Playbook Essentials
- Gourav Shah
- 431字
- 2021-07-16 20:47:49
Creating a site-wide playbook, nesting, and using include statements
As a best practice, we will create a top-level file, which will contain the blueprint of our complete infrastructure. Technically, we can include everything that we need to configure inside just one file. However, that would have two problems:
- It would quickly get out of control as we start adding tasks, variables, and handlers to this single file. It would be a nightmare to maintain such code.
- It would also be difficult to reuse and share such code. One of the advantages of using a tool such as Ansible is its ability to separate data from code. Data is organization-specific, and code is generic. This generic code can then be shared with others. However, if you write everything in a single file, it would be impossible to do so.
To avoid this problem, we will start organizing our code in a modular fashion, as follows:
- We will create roles for each of the applications that we need to configure. In this case, it is Nginx
- Our web server may need to install more than one application in addition to Nginx, for example, PHP and OpenSSL. To encapsulate all of these, we will create a playbook named
www.yml
. - The preceding playbook that we created will map hosts with the Nginx role. We may add more roles to it later.
- We will add this playbook to the top-level playbook, that is,
site.yml
The following diagram depicts the preceding steps in a very simple manner:

Here is our site.yml
file:
--- # site.yml : This is a sitewide playbook - include: www.yml
The preceding include
directive assists us with modularizing the code. Instead of writing it all in one file, we split the logic and import what is required. In this case, we will include another playbook, which is called a nested playbook.
The following are some guidelines on what can be included and how:
- The
include
directive can be used to include tasks, handlers, and even other playbooks - If you include a playbook inside another like we did in the
site.yml
file, you cannot substitute the variables - The
include
keyword can be used in combination with regular task/handler specifications - It's possible to pass parameters with include statements. This is called as parameterized include
Tip
Roles and auto-includes
Roles have implicit rules to auto include files. So long as you follow the directory layout conventions, you are assured that all your tasks, handlers, and the rest of the files are included automatically. Hence, it's important to create the subdirectories with the exact names as specified by Ansible.
- JavaScript+jQuery開發(fā)實(shí)戰(zhàn)
- Python 3網(wǎng)絡(luò)爬蟲實(shí)戰(zhàn)
- 人臉識(shí)別原理及算法:動(dòng)態(tài)人臉識(shí)別系統(tǒng)研究
- GeoServer Beginner's Guide(Second Edition)
- C++ 從入門到項(xiàng)目實(shí)踐(超值版)
- Highcharts Cookbook
- Python編程從0到1(視頻教學(xué)版)
- Hands-On Full Stack Development with Go
- INSTANT Sinatra Starter
- JavaScript動(dòng)態(tài)網(wǎng)頁編程
- jQuery技術(shù)內(nèi)幕:深入解析jQuery架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)原理
- Android熱門應(yīng)用開發(fā)詳解
- Spring Boot學(xué)習(xí)指南:構(gòu)建云原生Java和Kotlin應(yīng)用程序
- Swift Essentials(Second Edition)
- Visual Basic 開發(fā)從入門到精通