码迷,mamicode.com
首页 > 其他好文 > 详细

Ansible 二: playbook

时间:2020-05-22 17:27:02      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:ued   esc   using   isa   级别   创建文件   failed   剧本   使用方法   

playbook

playbook可以定义为一些列任务的配置集合。也称为剧本,每一个playbook都包含一系列的任务,每个任务在Ansible中称为play。Playbook的写法采用缩排的方式呈现,结构通过缩进来表示,连续的项目通过减号 “-”来表示。Playbook的语法具有如下的特性:

1 需要用---开始,而且是顶行首写

2 使用#注释

3 缩进必须统一,不能使用空格和Tab

4 缩进的级别必须是统一的,同样的缩进代表同样的级别

5 一个完整的代码块功能需最少元素,需包括name:task

6 一个name只能包含一个task

下面是一个playbook的样本。是采用file命令创建一个文件,使用方法是touch,mode=0755

---

- hosts: webservers

  remote_user: wuqi

  tasks:

   - name: for command test

file: path=/home/wuqi/go/ansible.go state=touch mode=0755

 

同样的不采用file命令,仅仅采用shell命令也是可以做到的。但是要分成2个task来做。不能将两个command写在同一个task里面,这样的话只会执行最后一个command

---

- hosts: webservers

  remote_user: wuqi

  tasks:

   - name: create a go file

     command: touch /home/wuqi/go/ansible1.go

   - name: change the mod

     command: chmod 755 /home/wuqi/go/ansible1.go

 

如果我们想看执行过程中的详细输出的话可以加上 –verbose命令。

ansible-playbook ./test2.yml –verbose

输出如下:

Using /etc/ansible/ansible.cfg as config file

PLAY [webservers] **************************************************************

 

TASK [Gathering Facts] *********************************************************

[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host 10.0.1.48 should use

/usr/bin/python3, but is using /usr/bin/python for backward compatibility with

prior Ansible releases. A future Ansible release will default to using the

discovered platform python for this host. See https://docs.ansible.com/ansible/

2.9/reference_appendices/interpreter_discovery.html for more information. This

feature will be removed in version 2.12. Deprecation warnings can be disabled

by setting deprecation_warnings=False in ansible.cfg.

ok: [10.0.1.48]

 

TASK [create a go file] ********************************************************

[WARNING]: Consider using the file module with state=touch rather than running

‘touch‘.  If you need to use command because file is insufficient you can add

‘warn: false‘ to this command task or set ‘command_warnings=False‘ in

ansible.cfg to get rid of this message.

changed: [10.0.1.48] => {"changed": true, "cmd": ["touch", "/home/wuqi/go/ansible3.go"], "delta": "0:00:00.001532", "end": "2020-05-19 16:30:30.831893", "rc": 0, "start": "2020-05-19 16:30:30.830361", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

 

TASK [change the mod] **********************************************************

[WARNING]: Consider using the file module with mode rather than running

‘chmod‘.  If you need to use command because file is insufficient you can add

‘warn: false‘ to this command task or set ‘command_warnings=False‘ in

ansible.cfg to get rid of this message.

changed: [10.0.1.48] => {"changed": true, "cmd": ["chmod", "755", "/home/wuqi/go/ansible3.go"], "delta": "0:00:00.001517", "end": "2020-05-19 16:30:31.301749", "rc": 0, "start": "2020-05-19 16:30:31.300232", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

 

PLAY RECAP *********************************************************************

10.0.1.48                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

 

 

Handler:

可以把handlers理解成另一种tasks,handlers是另一种‘任务列表‘,handlers中的任务会被tasks中的任务进行"调用",但是,被"调用"并不意味着一定会执行,只有当tasks中的任务"真正执行"以后(真正的进行实际操作,造成了实际的改变),handlers中被调用的任务才会执行,如果tasks中的任务并没有做出任何实际的操作,那么handlers中的任务即使被‘调用‘,也并不会执行。来看下面的例子:

---

- hosts: webservers

  remote_user: wuqi

  vars:

          file_name: ansible3.go

  tasks:

   - name: create a go file

     command: touch /home/wuqi/go/{{file_name}}

     #file: path=/home/wuqi/go/ansible.go state=touch mode=0755

   - name: change the mod

     command: chmod 755 /home/wuqi/go/{{file_name}}

     notify: rm

  handlers:

   - name: rm

     command: rm /home/wuqi/go/{{file_name}}

 

我们使用handlers关键字,指明哪些任务可以被‘调用‘,之前说过,handlers是另一种任务列表,你可以把handlers理解成另外一种tasks,你可以理解成它们是‘平级‘的,所以,handlers与tasks是‘对齐‘的(缩进相同)。在上例中,首先是创建文件,然后是chmod。最后使用notify关键字调用handlers中的任务。对应的任务名也是rm的。然后删掉之前创建的文件。

我们还可以在一个task中一次性notify多个handler,怎样才能一次性notify多个handler呢?你可能会尝试将多个handler使用相同的name,但是这样并不可行,因为当多个handler的name相同时,只有一个handler会被执行,所以,我们并不能通过这种方式notify多个handler,如果想要一次notify多个handler,则需要借助另一个关键字,它就是‘listen‘,你可以把listen理解成"组名",我们可以把多个handler分成"组",当我们需要一次性notify多个handler时,只要将多个handler分为"一组",使用相同的"组名"即可,当notify对应的值为"组名"时,"组"内的所有handler都会被notify。来看下面的这个例子:

---

- hosts: webservers

  remote_user: wuqi

  vars:

          file_name: ansible3.go

  tasks:

   - name: create a go file

     command: touch /home/wuqi/go/{{file_name}}

   - name: change the mod

     command: chmod 755 /home/wuqi/go/{{file_name}}

     notify: handler group1

  handlers:

   - name: rm

     listen: handler group1

     command: rm /home/wuqi/go/{{file_name}}

   - name: create again

     listen: handler group1

     command: touch /home/wuqi/go/{{file_name}}

 

在这里,handlers下面虽然有2个name不一样的task,但是listen都是 handler group1,因此可以算是一组。当notify调用 handler group1的时候,两个任务都会运行

Ansible 二: playbook

标签:ued   esc   using   isa   级别   创建文件   failed   剧本   使用方法   

原文地址:https://www.cnblogs.com/zhanghongfeng/p/12937829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!