- Practical Ansible 2
- Daniel Oh James Freeman Fabio Alessandro Locati
- 1049字
- 2021-06-24 16:06:49
Organizing your automation code
As you can imagine, if you were to write all of your required Ansible tasks in one massive playbook, it would quickly become unmanageable—that is to say, it would be difficult to read, difficult for someone else to pick up and understand, and—most of all—difficult to debug when things go wrong. Ansible provides a number of ways for you to divide your code into manageable chunks; perhaps the most important of these is the use of roles. Roles (for the sake of a simple analogy) behave like a library in a conventional high-level programming language. We will go into more detail about roles in Chapter 4, Playbooks and Roles.
There are, however, other ways that Ansible supports splitting your code into manageable chunks, which we will explore briefly in this section as a precursor to the more in-depth exploration of roles later in this book.
Let's build up a practical example. To start, we know that we need to create an inventory for Ansible to run against. In this instance, we'll create four notional groups of servers, with each group containing two servers. Our hypothetical example will contain a frontend server and application servers for a fictional application, located in two different geographic locations. Our inventory file will be called production-inventory and the example contents are as follows:
[frontends_na_zone]
frontend1-na.example.com
frontend2-na.example.com
[frontends_emea_zone]
frontend1-emea.example.com
frontend2-emea.example.com
[appservers_na_zone]
appserver1-na.example.com
appserver2-na.example.com
[appservers_emea_zone]
appserver1-emea.example.com
appserver2-emea.example.com
Now, obviously, we could just write one massive playbook to address the required tasks on these different hosts, but as we have already discussed, this would be cumbersome and inefficient. Let's instead break the task of automating these different hosts down into smaller playbooks:
- Create a playbook to run a connection test on a specific host group, such as frontends_na_zone. Put the following contents into the playbook:
--- - hosts: frontends_na_zone remote_user: danieloh tasks: - name: simple connection test ping:
- Now, try running this playbook against the hosts (note that we have configured it to connect to a remote user on the inventory system, called danieloh, so you will either need to create this user and set up the appropriate SSH keys or change the user in the remote_user line of your playbook). When you run the playbook after setting up the authentication, you should see an output similar to the following:
$ ansible-playbook -i production-inventory frontends-na.yml
PLAY [frontends_na_zone] *******************************************************
TASK [Gathering Facts] *********************************************************
ok: [frontend1-na.example.com]
ok: [frontend2-na.example.com]
TASK [simple connection test] **************************************************
ok: [frontend1-na.example.com]
ok: [frontend2-na.example.com]
PLAY RECAP *********************************************************************
frontend1-na.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
frontend2-na.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- Now, let's extend our simple example by creating a playbook that will only run on the application servers. Again, we will use the Ansible ping module to perform a connection test, but in a real-world situation, you would perform more complex tasks, such as installing packages or modifying files. Specify that this playbook is run against this host group from the appservers_emea_zone inventory. Add the following contents to the playbook:
--- - hosts: appservers_emea_zone remote_user: danieloh tasks: - name: simple connection test ping:
As before, you need to ensure you can access these servers, so either create the danieloh user and set up authentication to that account or change the remote_user line in the example playbook. Once you have done this, you should be able to run the playbook and you will see an output similar to the following:
$ ansible-playbook -i production-inventory appservers-emea.yml
PLAY [appservers_emea_zone] ****************************************************
TASK [Gathering Facts] *********************************************************
ok: [appserver2-emea.example.com]
ok: [appserver1-emea.example.com]
TASK [simple connection test] **************************************************
ok: [appserver2-emea.example.com]
ok: [appserver1-emea.example.com]
PLAY RECAP *********************************************************************
appserver1-emea.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
appserver2-emea.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- So far, so good. However, we now have two playbooks that we need to run manually, which only addresses two of our inventory host groups. If we want to address all four groups, we need to create a total of four playbooks, all of which need to be run manually. This is hardly reflective of best automation practices. What if there was a way to take these individual playbooks and run them together from one top-level playbook? This would enable us to divide our code to keep it manageable, but also prevents a lot of manual effort when it comes to running the playbooks. Fortunately, we can do exactly that by taking advantage of the import_playbook directive in a top-level playbook that we will call site.yml:
---
- import_playbook: frontend-na.yml
- import_playbook: appserver-emea.yml
Now, when you run this single playbook using the (by now, familiar) ansible-playbook command, you will see that the effect is the same as if we had actually run both playbooks back to back. In this way, even before we explore the concept of roles, you can see that Ansible supports splitting up your code into manageable chunks without needing to run each chunk manually:
$ ansible-playbook -i production-inventory site.yml
PLAY [frontends_na_zone] *******************************************************
TASK [Gathering Facts] *********************************************************
ok: [frontend2-na.example.com]
ok: [frontend1-na.example.com]
TASK [simple connection test] **************************************************
ok: [frontend1-na.example.com]
ok: [frontend2-na.example.com]
PLAY [appservers_emea_zone] ****************************************************
TASK [Gathering Facts] *********************************************************
ok: [appserver2-emea.example.com]
ok: [appserver1-emea.example.com]
TASK [simple connection test] **************************************************
ok: [appserver2-emea.example.com]
ok: [appserver1-emea.example.com]
PLAY RECAP *********************************************************************
appserver1-emea.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
appserver2-emea.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
frontend1-na.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
frontend2-na.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
There's much more that you can do with geographically diverse environments, such as our simple example here, as we have not even touched on things such as placing variables in your inventory (which, for example, associates different parameters with different environments). We will explore this in more detail in Chapter 3, Defining Your Inventory.
However, hopefully that has armed with enough knowledge so that you can start making informed choices about how to organize the code for your playbooks. As you complete further chapters of this book, you will be able to establish whether you wish to make use of roles or the import_playbook directive (or perhaps even both) as part of your playbook organization.
Let's carry on with our crash course on Ansible, in the next section, with a look at the configuration file and some of the key directives that you might find valuable.
- Word 2003、Excel 2003、PowerPoint 2003上機指導與練習
- 構建高質量的C#代碼
- Word 2000、Excel 2000、PowerPoint 2000上機指導與練習
- 腦動力:C語言函數速查效率手冊
- Seven NoSQL Databases in a Week
- 一本書玩轉數據分析(雙色圖解版)
- Java開發技術全程指南
- Implementing Oracle API Platform Cloud Service
- Apache Superset Quick Start Guide
- Troubleshooting OpenVPN
- 單片機技術一學就會
- 計算機與信息技術基礎上機指導
- 計算機組網技術
- Natural Language Processing and Computational Linguistics
- 玩轉PowerPoint