- Practical Ansible 2
- Daniel Oh James Freeman Fabio Alessandro Locati
- 752字
- 2021-06-24 16:06:53
Defining plays and tasks
So far when we have worked with playbooks, we have been creating one single play per playbook (which logically is the minimum you can do). However, you can have more than one play in a playbook, and a "play" in Ansible terms is simply a set of tasks (and roles, handlers, and other Ansible facets) associated with a host (or group of hosts). A task is the smallest possible element of a play and is responsible for running a single module with a set of arguments to achieve a specific goal. Of course, in theory, this sounds quite complex, but when backed up by a practical example, it becomes quite simple to understand.
If we refer to our example inventory, this describes a simple two-tier architecture (we've left out the database tier for now). Now, suppose we want to write a single playbook to configure both the frontend servers and the application servers. We could use two separate playbooks to configure the front end and application servers, but this risks fragmenting your code and making it difficult to organize. However, front end servers and application servers are going to be (by their very nature) fundamentally different and so are unlikely to be configured with the same set of tasks.
The solution to this problem is to create a single playbook with two plays in it. The start of each play can be identified by the line at the lowest indentation (that is, zero spaces in front of it). Let's get started with building up our playbook:
- Add the first play to the playbook and define some simple tasks to set up the Apache server on the front end, as shown here:
---
- name: Play 1 - configure the frontend servers
hosts: frontends
become: yes
tasks:
- name: Install the Apache package
yum:
name: httpd
state: latest
- name: Start the Apache server
service:
name: httpd
state: started
- Immediately below this, in the same file, add the second play to configure the application tier servers:
- name: Play 2 - configure the application servers
hosts: apps
become: true
tasks:
- name: Install Tomcat
yum:
name: tomcat
state: latest
- name: Start the Tomcat server
service:
name: tomcat
state: started
Now, you have two plays: one to install web servers in the frontends group and one to install application servers in the apps group, all combined into one simple playbook.
When we run this playbook, we'll see the two plays performed sequentially, in the order they appear in the playbook. Note the presence of the PLAY keyword, which denotes the start of each play:
$ ansible-playbook -i hosts playandtask.yml
PLAY [Play 1 - configure the frontend servers] *********************************
TASK [Gathering Facts] *********************************************************
changed: [frt02.example.com]
changed: [frt01.example.com]
TASK [Install the Apache package] *********************************************
changed: [frt01.example.com]
changed: [frt02.example.com]
TASK [Start the Apache server] *************************************************
changed: [frt01.example.com]
changed: [frt02.example.com]
PLAY [Play 2 - configure the application servers] *******************************
TASK [Gathering Facts] *********************************************************
changed: [app01.example.com]
changed: [app02.example.com]
TASK [Install Tomcat] **********************************************************
changed: [app02.example.com]
changed: [app01.example.com]
TASK [Start the Tomcat server] *************************************************
changed: [app02.example.com]
changed: [app01.example.com]
PLAY RECAP *********************************************************************
app01.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
app02.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
frt01.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
frt02.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
There we have it—one playbook, yet two distinct plays operating on different sets of hosts from the provided inventories. This is very powerful, especially when combined with roles (which will be covered later in this book). Of course, you can have just one play in your playbook—you don't have to have multiple ones, but it is important to be able to develop multi-play playbooks as you will almost certainly find them useful as your environment gets more complex.
Playbooks are the lifeblood of Ansible automation—they extend it beyond single task/commands (which in themselves are incredibly powerful) to a whole series of tasks organized in a logical fashion. As you extend your library of playbooks, however, how do you keep your work organized? How do you efficiently reuse the same blocks of code? In the preceding example, we installed Apache, and this might be a requirement on a number of your servers. However, should you attempt to manage them all from one playbook? Or should you perhaps keep copying and pasting the same block of code over and over again? There is a better way, and in Ansible terms, we need to start looking at roles, which we shall do in the very next section.
- 機器學習實戰:基于Sophon平臺的機器學習理論與實踐
- Project 2007項目管理實用詳解
- 工業機器人產品應用實戰
- 網絡服務器架設(Windows Server+Linux Server)
- 腦動力:PHP函數速查效率手冊
- 計算機網絡應用基礎
- 樂高創意機器人教程(中級 下冊 10~16歲) (青少年iCAN+創新創意實踐指導叢書)
- Photoshop CS3圖像處理融會貫通
- 網絡化分布式系統預測控制
- 手機游戲策劃設計
- 一步步寫嵌入式操作系統
- Web璀璨:Silverlight應用技術完全指南
- Kubernetes on AWS
- 歐姆龍CP1系列PLC原理與應用
- Microsoft Office 365:Exchange Online Implementation and Migration(Second Edition)