playbook是一个很是简单的配置管理和多主机部署系统。可做为一个适合部署复杂应用程序的基础。
playbook能够定制配置,能够按指定的操做步骤有序执行,支持同步和异步方式。
playbook是经过YAML格式来进行描述定义的,可实现多台主机应用的部署,对不一样分组的主机执行特定指令步骤。node
配置文件:nginx.ymllinux
--- - hosts: webservers #hosts参数做用:定义操做的对象,本例操做对象为webservers组 vars: #vars参数做用:定义变量(配置模板时会用到),做用域只限于webservers组 worker_processes: 4 num_cpus: 4 max_open_file: 65506 root: /data remote_user: root #指定远程操做的用户名,默认是root,支持sudo运行,经过添加sudo:yes便可 tasks: #定义任务列表(自上而下顺序执行) - name: ensure nginx is at the latest version #每一个事务均可以定义一个name标签,好处是加强可读性,便于观察结果输出时了解运行的位置 yum: pkg=nginx state=latest #yum安装最新版本的nginx - name: write the nginx config file template: src=/home/test/ansible/nginx/nginx2.conf dest=/etc/nginx/nginx.conf #根据模板配置nginx配置文件,src为主控端模板路径,dest为被控端nginx配置文件路径 notify: - restart nginx - name: ensure nginx is running service: name=nginx state=started #启动nginx handlers: #通知处理程序(必需要有notify触发才会执行),根据notify选择handlers中对应的name标签,从而进行相应操做。如notify中是restart nginx,则handlers中的name标签内容也是restart nginx,才能执行 - name: restart nginx service: name=nginx state=restarted
模板:nginx2.confnginx
user nginx; worker_processes {{ worker_prcesses }}; {% if num_cpus == 2 %} worker_cpu_affinity 01 10; {% elif num_cpus == 4 %} worker_cpu_affinity 1000 0100 0010 0001; {% elif num_cpus >= 8 %} worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; {% else %} worker_cpu_affinity 1000 0100 0010 0001; {% endif %} worker_flimit_notifile {{ max_open_file }}; ... ...
格式:web
ansible-playbook playbook.yml(playbook文件,可自定义名称) [参数]
例:app
ansible-playbook /home/test/ansible/playbooks/nginx.yml -f 10 #启用10个并行进程数执行playbook(nginx.yml)
经常使用参数:运维
-u REMOTE_USER #手工指定playbook的系统用户 --syntax-check #检查playbook的语法 --list-hosts playbook #匹配到的主机列表 -T TIMEOUT #定义playbook执行超时时间 --step #以单任务分步骤运行,方便作每一步确认工做 --help #帮助信息
当playbook文件很是大时,想要复用某些功能是就会显得至关吃力,Ansible支持写playbook文件时拆分红多个文件,经过包含(include)的形式进行引用。
例:
功能(复用)文件:tasks/fool.yml异步
--- #possibly saved as tasks/foo.yml - name: placeholder foo command: /bin/foo - name: placeholder bar command: /bin/bar
使用的playbook文件:playbook.ymlspa
tasks: - include: tasks/foo.yml #经过include来引用复用的功能
角色:Ansible定制好的一种标准规范,以不一样级别目录层次及文件对角色、变量、任务、处理程序等进行拆分,为后续功能扩展、可维护性打下基础。
例:
以上面的nginx.yml为例进行拆分,结构以下: rest
hosts code
#自定义主机,非必选项,默认将引用/etc/ansible/hosts的参数,要引用自定义hosts,须要经过-i file参数来实现,如:ansible-playbook -i hosts
[webservers]
192.168.1.111
192.168.1.112
group_vars
#定义组变量目录,目录中的文件名要与组名保持一致,组变量文件定义的变量做用域只在该组内,不能做用到其余组
【group_vars/all】 #表明全部主机
--- #Variables listed here are applicable to all host groups ntpserver: ntp.sjtu.edu.cn
【group_vars/webservers】 #webservers组
--- worker_processes: 4 num_cpus: 4 max_open_file: 66535 root: /data
site.yml
#全局配置文件,下面内容引用了两个角色块,角色的应用范围及实现功能都不同
--- - name: apply common configuration to all nodes hosts: all roles: - common #对应目录为:nginx/roles/common - name: configure and deploy the webservers and application code hosts: webservers roles: - web #对应目录为:nginx/roles/web
roles
#角色目录,一般每一个角色对应着一个特定的功能服务
【roles/common】
handlers/main.yml #处理程序文件
---
- name: restart ntp
service: name=ntp state=restarted
tasks/main.yml #任务列表文件
--- - name: Install ntp yum: name=ntp state=present - name: Configure ntp file template: src=ntp.conf.j2 dest=/etc/ntp.conf #引用模板无需写路径,默认在上级的templates目录中查找 notify: restart ntp - name: Start the ntp service service: name=ntp state=started enabled=true - name: test to see if selinux is running command: getenforce register: sestatus changed_when: false
templates/ngp.conf.j2 #模板
driftfile /var/lib/ntp/drift restrict 127.0.0.1 restrict -6 ::1 server {{ ntpserver }} #此处ntpserver引用vars/main.yml中定义的ntpserver变量 includefile /etc/ntp/rypto/pw keys /etc/ntp/keys
vars/main.yml #变量配置文件
--- #Variable listed here are applicable to all host groups ntpserver: 210.72.145.44
【roles/web】
handlers/main.yml #处理程序文件
---
- name: restart nginx
service: name=nginx state=restarted
tasks/main.yml #任务列表文件
--- - name: ensure nginx is at the latest version #每一个事务均可以定义一个name标签,好处是加强可读性,便于观察结果输出时了解运行的位置 yum: pkg=nginx state=latest #yum安装最新版本的nginx - name: write the nginx config file template: src=/home/test/ansible/nginx/nginx2.conf dest=/etc/nginx/nginx.conf #根据模板配置nginx配置文件,src为主控端模板路径,dest为被控端nginx配置文件路径 notify: - restart nginx - name: ensure nginx is running service: name=nginx state=started #启动nginx
templates/nginx2.conf #模板
user nginx; worker_processes {{ worker_prcesses }}; {% if num_cpus == 2 %} worker_cpu_affinity 01 10; {% elif num_cpus == 4 %} worker_cpu_affinity 1000 0100 0010 0001; {% elif num_cpus >= 8 %} worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; {% else %} worker_cpu_affinity 1000 0100 0010 0001; {% endif %} worker_flimit_notifile {{ max_open_file }}; ... ...
ansible-playbook -i hosts site.yml -f 10 #启用10个并行进程数执行playbook。hosts文件经过-i指向自定义hosts,playbook配置文件为site.yml
参考资料:
根据刘天斯《Python自动化运维技术与最佳实践》整理