Ansible detailed explanation (7) - simple use of Ansible palybook

Today, I will continue to introduce the relevant knowledge of Linux operation and maintenance. The main content of this article is the simple use of Ansible's playbook.

1. Ansible Playbook format

A simple Ansible Playbook example is as follows:

---
- hosts: exp
  remote_user: root
  tasks:
  - name: Install Apache by YUM
    yum: name=httpd state=installed
  - name: Start Apache
    service: name=httpd state=started

The above Ansible Playbook playbook is very simple and can control the device of the exp module to install Apache and start the service. Simple as it may seem, the above playbook is a bit tricky to write due to the strict format of YAML.
First, start with three dashes, which is the standard opening format for Ansible Playbooks. On the second line, start with a dash followed by a space. This space must exist and cannot be added! Otherwise, it will fail to execute due to malformed YAML. The hosts parameter is followed by a colon, which must also be followed by a space. This space is the same as before, it must exist and cannot be added! After that, write the hosts controlled by Ansible. All other lines have a format similar to the above, with a colon after the parameter and a space after the colon. Also, spaces must exist and cannot be added! The parameter in the third line is remote_user, which must be strictly aligned with hosts! Otherwise, Ansible will also report an error. The following tasks tasks should also be aligned with the short bar in front of the name, and the module name on the next line of the name should be aligned with the "n" letter of the name.
It can be seen that a very simple Ansible Playbook file has so many formats, and it is also very strict. Sometimes we read a lot of Playbooks, but when we actually write the Playbooks, there are always errors of one kind or another, which are caused by the strict format of YAML. Therefore, we need to practice more to master the writing of Ansible Playbooks.
The above Playbook, after writing, is as follows:
insert image description here
Note that the above Playbook example is just a very simple example in Ansible. In the production environment, Ansible combines many variables, templates, and roles, and the written Playbook will be much more complicated, and also Can accomplish more complex tasks.
However, the above basic and simple Playbook is the foundation of everything complex, so I hope you will study hard.

2. Ansible Playbook execution

When we have finished writing the above Playbook, we can use the ansible-playbook command to execute it. Suppose we store the above Playbook as a tmp.yml file. (In fact, we should store the Playbook as a file ending in .yml) Then we execute the script command as follows:

ansible-playbook tmp.yml

The result of the above command is as follows:
insert image description here
On the controlled device, we delete Apache first, and then wait for the Playbook on the Ansible device to finish executing, and then check the effect just executed. The result is as follows:
insert image description here
It can be seen that Apache has been reinstalled on the device and enabled With this service, our Playbook performed successfully!
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200

Guess you like

Origin blog.csdn.net/weixin_40228200/article/details/123488366