Ansible Playbook目录结构详解
顶层目录结构应当包括下列文件和目录
production # inventory file for production servers 关于生产环境服务器的清单文件
stage # inventory file for stage environment 关于 stage 环境服务器的清单文件
group_vars/
group1 # here we assign variables to particular groups 这里我们给特定的组定义变量
group2 # ""
host_vars/
hostname1 # if systems need specific variables, put them here 如果系统需要特定的变量,把它们放置在这里.
hostname2 # ""
library/ # if any custom modules, put them here (optional) 如果有自定义的模块,放在这里(可选)
filter_plugins/ # if any custom filter plugins, put them here (optional) 如果有自定义的过滤插件,放在这里(可选)
site.yml # master playbook 主 playbook
webservers.yml # playbook for webserver tier Web 服务器的 playbook
dbservers.yml # playbook for dbserver tier 数据库服务器的 playbook
roles/
common/ # this hierarchy represents a "role" 这里的结构代表了一个 "role"
tasks/ #
main.yml # <-- tasks file can include smaller files if warranted
handlers/ #
main.yml # <-- handlers file
templates/ # <-- files for use with the template resource
ntp.conf.j2 # <------- templates end in .j2
files/ #
bar.txt # <-- files for use with the copy resource
foo.sh # <-- script files for use with the script resource
vars/ #
main.yml # <-- variables associated with this role
defaults/ #
main.yml # <-- default lower priority variables for this role
meta/ #
main.yml # <-- role dependencies
webtier/ # same kind of structure as "common" was above, done for the webtier role
monitoring/ # ""
fooapp/ # ""
file: production
[atlanta-webservers]
www-atl-1.example.com
www-atl-2.example.com
[boston-webservers]
www-bos-1.example.com
www-bos-2.example.com
[atlanta-dbservers]
db-atl-1.example.com
db-atl-2.example.com
[boston-dbservers]
db-bos-1.example.com
# webservers in all geos
[webservers:children]
atlanta-webservers
boston-webservers
# dbservers in all geos
[dbservers:children]
atlanta-dbservers
boston-dbservers
# everything in the atlanta geo
[atlanta:children]
atlanta-webservers
atlanta-dbservers
# everything in the boston geo
[boston:children]
boston-webservers
boston-dbservers
file: group_vars/atlanta
ntp: ntp-atlanta.example.com
backup: backup-atlanta.example.com
file: group_vars/webservers
apacheMaxRequestsPerChild: 3000
apacheMaxClients: 900
file: group_vars/all
ntp: ntp-boston.example.com
backup: backup-boston.example.com
file: host_vars/db-bos-1.example.com
foo_agent_port: 86
bar_agent_port: 99
file: site.yml
- include: webservers.yml
- include: dbservers.yml
file: webservers.yml
- hosts: webservers
roles:
- common
- webtier
file: roles/common/tasks/main.yml
- name: be sure ntp is installed
yum: pkg=ntp state=installed
tags: ntp
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
tags: ntp
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
tags: ntp
file: roles/common/handlers/main.yml
- name: restart ntpd
service: name=ntpd state=restarted
那这种结构适用于何种应用场景? 很多!若我想重新配置整个基础设施,如此即可:
ansible-playbook -i production site.yml
那只重新配置所有的 NTP 呢?太容易了.:
ansible-playbook -i production site.yml --tags ntp
只重新配置我的 Web 服务器呢?:
ansible-playbook -i production webservers.yml
只重新配置我在 boston 的 Web服务器呢?:
ansible-playbook -i production webservers.yml --limit boston
前10台和接下来的10台呢?
ansible-playbook -i production webservers.yml –limit boston[0-10]
ansible-playbook -i production webservers.yml –limit boston[10-20]
当然,只使用基础的 ad-hoc 也是 OK 的啦.:
ansible boston -i production -m ping
ansible boston -i production -m command -a '/sbin/reboot'
confirm what task names would be run if I ran this command and said "just ntp tasks"
ansible-playbook -i production webservers.yml --tags ntp --list-tasks
confirm what hostnames might be communicated with if I said "limit to boston"
ansible-playbook -i production webservers.yml --limit boston --list-hosts