ansible Note (8): acquaintance ansible playbook

Review summary : Let's imagine a scenario to work, to see how the application of knowledge to the job before the scene. Suppose that we want to install nginx on host 192.168.10.2 and start, we can execute the following three commands the host control ansible.

[root@ansible-control /]#ansible 192.168.10.2 -m yum_repository -a 'name=aliEpel description="alibaba EPEL" baseurl=https://mirrors.aliyun.com/epel/$releasever\Server/$basearch/'
[root@ansible-control /]#ansible 192.168.10.2 -m yum -a 'name=nginx disable_gpg_check=yes enablerepo=aliEpel'
[root@ansible-control /]#ansible 192.168.10.2 -m service -a "name=nginx state=started"

  By the above-described three commands to first determine the configuration of the corresponding source yum, yum then installed Nginx module, service module starts the last used Nginx, ultimately achieve our purpose. But the efficiency is too low.
But in the actual work environment, we may need to install nginx often on the new host, is every time a new server is added to the working environment, we have to modify the host name of the above three commands will be executed again and again each command ? Such seems to be some trouble, there must be a better way, yes, we can write the above command script, every change some variables, and then execute the script on the line, so it seems a lot of convenience, but ansible inherently provides this function similar script in the ansible, a similar script file is called a script, the script of the English name for the playbook, we only need to write things to do to playbook, the different modules in the order of arrangement in the script, it will ansible According to the script execution step by step, and ultimately achieve our objective, although the playbook of features and a similar script, but the script is not a simple ad-hoc commands in the order piled in a single executable file, writing the script need to follow the YAML syntax, if you do not come into contact with YAML syntax, do not be afraid, adhere to the back of the example of watching familiar with some fixed routines later, you can also write your own playbook.
So how do you write it playbook? We start with a simple example started

1. Write the primary playbook:

  (1) create a playbook YAML file format. playbook file .yaml or .yml as the file name suffix, where we create a script file named test.yml of.

  (2) Example:

      ansible  192.168.10.2  -m ping

      ansible  192.168.10.2  -m  file  -a  'path=/test/AAA  state=directory'

  (3) started writing playbook: note the space

[Resolved] playbook

第一行使用三个横杠作为开始,在yml语法中,“---”表示文档开始。
第二行使用“-”作为开头(注意横杠后面有空格),hosts关键字表示对应将操作的主机是哪台。如果有多台主机,可以使用逗号将其隔开,hosts: 192.168.10.2,192.168.10.3。如果清单分了组,也可以使用组名。
第三行使用remote_user关键字可以指定在进行远程操作时使用哪个用户进行操作,remote_user: root表示使用192.168.10.2主机上的root用户进行操作。注意:在YAML语法中进行缩进时,不能使用tab键进行缩进,必须使用空格。
第四行使用tasks关键字指明要进行操作的任务列表,之后的行都属于tasks键值对中的值。每个任务都以"-"开头,每个任务都有自己的名字,使用name关键字进行指定。

 2.运行剧本(playbook):

  [root@ansible-control playbook]# ansible-playbook p1.yml 

3.在一个playbook中编写多个play:

 

 4.检测playbook中是否存在语法错误:

[root@ansible-control playbook]# ansible-playbook --syntax-check p1.yml 

如果只返回playbook名称则无语法错误!

 

5.模拟执行playbook,预估playbook是否能够正常运行,实际并不会造成任何改变:

  [root@ansible-control playbook]# ansible-playbook --check p1.yml 

学习大哥:http://www.zsythink.net/archives/2602

 

Guess you like

Origin www.cnblogs.com/python-wen/p/11356328.html