- 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.
- 自動化控制工程設(shè)計(jì)
- Python Data Science Essentials
- 西門子S7-200 SMART PLC實(shí)例指導(dǎo)學(xué)與用
- 人工智能與人工生命
- 突破,Objective-C開發(fā)速學(xué)手冊
- 基于企業(yè)網(wǎng)站的顧客感知服務(wù)質(zhì)量評價(jià)理論模型與實(shí)證研究
- 奇點(diǎn)將至
- 會聲會影X4中文版從入門到精通
- 30天學(xué)通Java Web項(xiàng)目案例開發(fā)
- 基于元胞自動機(jī)的人群疏散系統(tǒng)建模與分析
- Getting Started with Tableau 2019.2
- 巧學(xué)活用Photoshop
- Learn SOLIDWORKS 2020
- 巧學(xué)活用電腦維護(hù)108問
- 系統(tǒng)與服務(wù)監(jiān)控技術(shù)實(shí)踐