Ansible series (5): Playbook
Before playbook
After the first two modules, I believe that anyone who uses shell will discover the power of ansible.
Here we will introduce playbook, a truly powerful place. Let you get rid of the difficulty of executing shell.
Definition of Playbook
The definition of Playbook is difficult to describe accurately in Chinese. Reference the official website:
Playbooks are Ansible's configuration, deployment, and orchestration language. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.
Playbook is a language that combines ansible's configuration management, deployment action execution, and orchestration capabilities. It is used to orchestrate and execute the general steps and processes you want to execute on remote nodes. Very easy to understand.
There is also a simple saying from the official website:
If Ansible modules are the tools in your workshop, playbooks are your design plans.
This means that the ansible module is your partner, so Playbook is your work plan.
One responsible for execution, one responsible for planning, Nice.
A simple playbook
A simple playbook should be as follows:
--- <--- Start of playbook-hosts: webservers <--- declare the node group where the Playbook runs vars: <--- declare the variable http_port: 80 <--- the variable http_port value is 80 max_clients: 200 remote_user: root <--- declare that the remote user is root tasks: <--- declare the remote task-name: ensure apache is at the latest version <--- ansible name module, when the text description is yum: pkg = httpd state = latest <--- ansible yum module, we will talk about-name: write the apache config file template: src =/srv/httpd later. j2 dest =/etc/httpd. conf <--- ansible template module. Later, we will talk about notify: <--- ansible handler usage. Later, we will talk about-restart apache-name: ensure apache is running (and enable it at boot) service: name = httpd state = started enabled = yes <--- ansible service module. Later I will talk about handlers: <--- ansible handler usage and will talk about-name: restart apache service: name = httpd state = restarted
What did the above playbook do? Explain the task section. People who are familiar with Linux may be able to guess 78:
1. Use yum to install the latest apache version.
2. overwrite the httpd. conf configuration file from/srv/httpd. j2 of the ansible execution node to/etc/httpd. conf.
3. Call Handler to restart apache
4. Call the service to confirm that apache has been restarted and set it to start apache on startup.
5. Define handler
If you use a traditional shell to execute the above many things, it will be very troublesome and troublesome. You can understand all the things you have done through shell.
Can you understand what it does?
Its format is YAML syntax. Let's take a look at it. It's very simple.
Declare again
The new modules in this chapter will introduce the calling methods in playbook. No longer use ansible as a case study.