YAML是一种表达资料序列的格式,因为参考了其余多种语言,因此具备很高的可读性。其特性以下:node
1.YAML中两种经常使用的数据类型,分别是list和directorylinux
-teacher -student
2.列表的全部元素均使用“-”开头web
3.字典经过key和value进行标识如:sql
name:zhangsan job:teacher age:25
playbook是由一个或者多个play组成的列表,主要功能是将task定义好的角色并为一组进行统一管理,也就是经过task调用Ansible的模块将多个paly组织在一个playbook中。playbook自己由如下各部分组成:shell
1.下面是一个playbook的简单示例:apache
- hosts: webserver #定义的主机组,即应用的主机 vars: #定义变量 http_port: 80 max_clients: 200 user: root tasks: #执行的任务 - name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: write the apache config file template: src=/srv/httpd.j2 dest=/etc/httpd.conf notify: - restart apache - name: ensure apache is running service: name=httpd state=started handlers: #处理器 - name: restart apache service: name=httpd state=restarted
playbook的设计目的就是为了让某个或者某些主机以某个身份去执行相应的任务。其中用于指定要执行任务的主机hosts定义,能够是一个主机也能够是由冒号分隔的额多个主机组;用于指定被管理主机上执行任务的用户用remote_user来定义,例如:centos
- hosts: webserver remote_user: root
1.remote_user也能够定义指定用户经过sudo的方法在被管理主机上运行指令,甚至能够在使用become指定sudo切换的用户。app
- hosts: webserver remote_user: root tasks: - name: ping test ping: become: yes become_user: zhangsan [root@rabbitmq01 ~]# ansible-playbook a.yml PLAY [webserver] ********************************************************************************* TASK [Gathering Facts] *************************************************************************** ok: [192.168.58.132] TASK [ping test] ********************************************************************************* [WARNING]: Module remote_tmp /home/zhangsan/.ansible/tmp did not exist and was created with a mode of 0700, this may cause issues when running as another user. To avoid this, create the remote_tmp dir with the correct permissions manually ok: [192.168.58.132] PLAY RECAP *************************************************************************************** 192.168.58.132 : ok=2 changed=0 unreachable=0 failed=0
1.Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在全部主机上完成第一个任务后再开始第二个任务。
在运行playbook时(从上到下执行),若是一个host执行task失败,整个tasks都会回滚,请修正playbook 中的错误,而后从新执行便可。
Task的目的是使用指定的参数执行模块,而在模块参数中可使用变量.ssh
2.每个task必须有一个名称name,这样在运行playbook时,从其输出的任务执行信息中能够很好的辨别出是属于哪个task的。若是没有定义name,‘action’的值将会用做输出信息中标记特定的task。tcp
3.定义一个task,常见的格式:”module: options” 例如:yum: name=httpd
tasks: - name: make sure appache is running service: name=httpd state=started
4.ansible的自带模块中,command模块和shell模块无需使用key=value格式
tasks: -name: disable selinux command: /sbin/setenforce 0
Handlers用于当关注的资源发生变化时所采起的操做。在notify中列出的操做便称为handler,也就是在notify中须要调用handler中定义的操做。而notify这个动做在每一个play的最后被触发,仅在全部的变化发生完成后一次性执行指定的操做。handler也是task列表的格式:
notify: - restart httpd #一旦执行这里就会触发name为restart httpd的handler handlers: - name: restart httpd service: name=httpd state=restarted
Jinja是基于Python的模板引擎。Template类是Jinja的另外一个重要组件,能够看做一个编译过的模板文件,用于产生目标文本,传递Python的变量给模板去替换模板中的标记。这里就先演示一个示例,经过ansible在两台被管理主机上安装httpd服务,而且修改httpd.conf文件后,重启。前面的安装已经介绍过,这里我直接说明下实验环境便可:
角色 | 主机名 | IP地址 | 组名 |
---|---|---|---|
控制主机 | node1 | 192.168.58.146 | |
被管理主机 | node2 | 192.168.58.148 | webserver |
被管理主机 | node3 | 192.168.58.149 | sqlserver |
- hosts: webserver remote_user: root vars: #定义几个变量 - httpdport: 192.168.58.148:80 - servername: www.yun.com:80 - service: httpd tasks: - name: install httpd service yum: name={{service}} state=latest - name: install configuration file for httpd template: src=/root/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf # 首先在本地生成一份template文件 notify: - restart httpd handlers: - name: restart httpd service: name={{service}} state=restarted
1.这个是yml文件内容,下面时模板文件中须要修改的内容
[root@node1 ~]# cat httpd.conf.j2 | grep '^Listen' Listen {{httpdport}} [root@node1 ~]# cat httpd.conf.j2 | grep '^ServerName' ServerName {{servername}}
[root@node1 ~]# ansible-playbook abc.yml PLAY [webserver] ********************************************************************************** TASK [Gathering Facts] **************************************************************************** ok: [192.168.58.148] TASK [install httpd service] ********************************************************************** changed: [192.168.58.148] TASK [install configuration file for httpd] ******************************************************* changed: [192.168.58.148] RUNNING HANDLER [restart httpd] ******************************************************************* changed: [192.168.58.148] PLAY RECAP **************************************************************************************** 192.168.58.148 : ok=4 changed=3 unreachable=0 failed=0
2.能够看到node2上服务已经开启,监听端口也已经修改过来了
[root@node2 .ssh]# rpm -q httpd httpd-2.4.6-80.el7.centos.1.x86_64 [root@node2 .ssh]# netstat -ntap | grep 80 tcp 0 0 192.168.58.148:80 0.0.0.0:* LISTEN 3540/httpd [root@node2 .ssh]# cat /etc/httpd/conf/httpd.conf | grep '^Listen' Listen 192.168.58.148:80 [root@node2 .ssh]# cat /etc/httpd/conf/httpd.conf | grep '^ServerName' ServerName www.yun.com:80
若是屡次执行修改playbook会涉及到一些没有变化的代码,可使用tags让用户选择跳过没有变化的代码,只运行olaybook中发生变化的部分代码,能够在playbook中为某个或者某些任务定义“标签”,在执行此playbook时经过ansible-playbook命令使用--tags选项能实现仅运行指定的tasks。
- hosts: sqlserver remote_user: root tasks: - name: build a new file copy: content="this is a test" dest=/root/test1.txt tags: - only - name: bulid another file copy: content="this is another test" dest=/root/test2.txt [root@node1 ~]# ansible-playbook a.yml --tags="only" PLAY [sqlserver] ********************************************************************************* TASK [Gathering Facts] *************************************************************************** ok: [192.168.58.149] TASK [build a new file] ************************************************************************** changed: [192.168.58.149] PLAY RECAP *************************************************************************************** 192.168.58.149 : ok=2 changed=1 unreachable=0 failed=0
咱们去node3中查看到底生成了几个文件
[root@node3 ~]# ls anaconda-ks.cfg mfs-1.6.27-5.tar.gz 公共 视频 文档 音乐 initial-setup-ks.cfg test1.txt 模板 图片 下载 桌面
能够看到只生成了test1.txt