Content Organization

production                # inventory file for production servers
stage # inventory file for stage environment 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
webservers.yml # playbook for webserver tier
dbservers.yml # playbook for dbserver tier roles/
common/ # this hierarchy represents a "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/ # ""

Use Dynamic Inventory With Clouds

Dynamic Inventory

How to Differentiate Stage vs Production

# 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 # 按主机、区域、数据中心划分

Group And Host Variables

---
# 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 # 注意主机变量&组变量的覆盖

Top Level Playbooks Are Separated By Role

---
# file: site.yml
- include: webservers.yml
- include: dbservers.yml ---
# file: webservers.yml
- hosts: webservers
roles:
- common
- webtier ansible-playbook site.yml --limit webservers
ansible-playbook webservers.yml

Task And Handler Organization For A Role

---
# 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

What This Organization Enables (Examples)

ansible-playbook -i production site.yml
ansible-playbook -i production site.yml --tags ntp
ansible-playbook -i production webservers.yml ansible-playbook -i production webservers.yml --limit boston
ansible-playbook -i production webservers.yml --limit boston[0-10]
ansible-playbook -i production webservers.yml --limit boston[10-20] 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

Deployment vs Configuration Organization

Stage(test) vs Production

Rolling Updates

Delegation, Rolling Updates, and Local Actions.

Always Mention The State

Group By Roles

Operating System and Distribution Variance

ansible使用8-Best Practices的更多相关文章

  1. Ansible自动化运维笔记1(安装配置)

    1.Ansible的安装 pip install ansible==1.9.1 ansible1.9.1版本依赖的软件有 Python2.6以上版本 paramiko模块 PyYAML Jinja2 ...

  2. 如何利用ansible callback插件对执行结果进行解析

    最近在写一个批量巡检工具,利用ansible将脚本推到各个机器上执行,然后将执行的结果以json格式返回来. 如下所示: # ansible node2 -m script -a /root/pyth ...

  3. 《Ansible权威指南》笔记(2)——Inventory配置

    四.Inventory配置ansible通过Inventory来定义主机和组,使用时通过-i指定读取,默认/etc/ansible/hosts.可以存在多个Inventory,支持动态生成.1.定义主 ...

  4. useful Ansible commands

    This article includes some useful Ansible commands. I will try to write blogs by English. You may wa ...

  5. 《Ansible权威指南》笔记(4)——Playbook

    七.Playbook1.语法特性如下:(1)"---"首行顶格开始(2)#号注释(3)缩进统一,不同的缩进代表不同的级别,缩进要对齐,空格和tab不能混用(4)区别大小写,键值对k ...

  6. 《Ansible权威指南》笔记(3)——Ad-Hoc命令集,常用模块

    五.Ad-Hoc命令集1.Ad-Hoc命令集通过/usr/bin/ansible命令实现:ansible <host-pattern> [options]    -v,--verbose  ...

  7. 《Ansible权威指南》笔记(1)——安装,ssh密钥登陆,命令

    2016-12-23 读这本<Ansible权威指南>学习ansible,根据本书内容和网上的各种文档,以及经过自己测试,写出以下笔记.另,这本书内容很好,但印刷错误比较多,作者说第二版会 ...

  8. 自动化运维工具ansible部署以及使用

    测试环境master 192.168.16.74webserver1 192.168.16.70webserver2 192.168.16.72安装ansiblerpm -Uvh http://ftp ...

  9. Ansible Ubuntu 安装部署

    一.安装: $ sudo apt-get install ansible 二.配置: a.基本配置 $ cd /etc/ansible/ $ sudo cp hosts hosts_back 备份一个 ...

随机推荐

  1. msf连接PostgreSQL数据库

    一.启动PostgreSQL服务######################################################################?root@root:~# ...

  2. matplotlib学习笔记(四)

    利用matplotlib可以显示图像 imread()和imshow()提供了简单的图像载入和显示功能. img = plt.imread("xxx.jpg") imread()可 ...

  3. 【Cracking the Code Interview(5th edition)】一、数组与字符串(C++)

    1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.不允许使用额外的数据结构. 解答:这里假定字符集为ASCII码,可以与面试官沟通确认字符串使用的字符集.由于字符集是有限的,建立一个数组模拟 ...

  4. spring 和 spirngMvc 中 异常处理

    spring 中 的 异常处理 使用的是aspectj @Aspect @Component /** * * @author **** * @createData 2017年7月13日 上午8:36: ...

  5. 75th LeetCode Weekly Contest Rotate String

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  6. 华东交通大学2015年ACM“双基”程序设计竞赛1004

    Problem D Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Sub ...

  7. day15 面向对象 成员

    成员 1. 变量 1.实例变量 格式: 变量.xxx=xx (称为实例变量,也叫属性,字段)给对象用的 2.类变量 类变量:直接写在类中的变量就是类变量,类变量一般用类名来访问 其实就是类中相同的属性 ...

  8. ftp功能深度剖析 + 线程 031

    一 打印进度条 import time for i in range(20): # \r 回到行首打印内容 如果有同一行内容,那么就被抹掉了 n = '>'* i print('\r%s'%n, ...

  9. sqoop数据校验

    sqoop数据校验 # check data oracle_cnt=$(sqoop eval \ -Dmapred.job.queue.name=${queue} \ --connect ${conn ...

  10. spark Failed to get database default, returning NoSuchObjectException

    解决方法:1)Copy winutils.exe from here(https://github.com/steveloughran/winutils/tree/master/hadoop-2.6. ...