[ansible-playbook]4 持续集成环境之分布式部署利器 ansible playbook学习
3 ansible-play讲的中太少了,今天稍微深入学习一点预计阅读时间:15分钟
一: 安装部署
参考 http://getansible.com/begin/an_zhuang_ansile
快速检查 (可能需要配置ssh无密访问,可参考https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2 )
配置host文件
192.168.100.1
192.168.100.2
测试能否ping通
# ansible all -i host -m ping
二:常用模块
a. service 用于启动检查服务
b. file 用于文件删除 链接 创建
c.shell 用于执行脚本(不推荐,因为shell操作有时并非幂等,而且不方便检查执行结果)
d.copy 用于拷贝文件
e. vars+ template 用于根据模板文件基于变量创建配置文件
样例:
test.yml
---
#Task1
-
web 0731
root
-test connection
-httpd check
name=sshd state=started
-remove foo.conf
dest=/etc/foo.conf state=absent
#-delete foo.conf
# rm -f /etc/foo.conf
-copy foo.conf
src=/srv/myfiles/foo.conf dest=/etc/foo.conf
owner=root group=root mode=064
- Verify Conf
-Add template Config
src=serversforhackers.com.j2 dest=/etc/{{ domain }}.conf owner=root group=root
-Verify Conf
file /etc/foo.conf
#Task2
-server
server_0731
0731
root
-Add template Config
src=serversforhackers.com.j2 dest=/etc/{{ sdomain }}.conf owner=root group=root
模板文件: templates/serversforhackers.com.j2
server { # Enforce the use of HTTPS
listen
80 default_server;
server_name *.{{ domain }};
return 301 //{{ domain }}$request_uri;
}
hosts文件:
10.0.0.3
10.0.0.4
10.0.0.5
10.0.0.6
10.0.0.7
执行脚本: # ansible-playbook -i host test.yml (-i 表示指定读取的host文件路径)
PLAY *******************************************************************************************************************************************************************************************************************************************************************
TASK *******************************************************************************************************************************************************************************************************************************************************
TASK [
test connection] *******************************************************************************************************************************************************************************************************************************************************
TASK ***********************************************************************************************************************************************************************************************************************************************************
TASK *******************************************************************************************************************************************************************************************************************************************************
TASK *********************************************************************************************************************************************************************************************************************************************************
TASK [
Add template Config] ***************************************************************************************************************************************************************************************************************************************************
RUNNING HANDLER ************************************************************************************************************************************************************************************************************************************************
PLAY ****************************************************************************************************************************************************************************************************************************************************************
TASK *******************************************************************************************************************************************************************************************************************************************************
TASK [
Add template Config] ***************************************************************************************************************************************************************************************************************************************************
PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************
10.0.0.3 : ok=7 changed=3 unreachable=0 failed=0
10.0.0.4 : ok=7 changed=3 unreachable=0 failed=0
10.0.0.5 : ok=2 changed=0 unreachable=0 failed=0
10.0.0.6 : ok=2 changed=0 unreachable=0 failed=0
注:完整模块参见 http://docs.ansible.com/ansible/latest/modules_by_category.html
三 常见检查
语法检查# ansible-playbook --syntax-check test.yml
注:报错信息有可能位置不准,碰见过对齐格式不正确引发的错误
列出主机# ansible-playbook -i hosts --list-hosts test.yml
四 最佳实践
参考 http://docs.ansible.com/ansible/latest/playbooks_best_practices.html
根据 Roles分类,存放template 文件,加密yml(如果需要的话)
Git hub上的实例: https://github.com/ansible/ansible-examples
如何利用ansible 安装go: https://github.com/jlund/ansible-go
如何利用ansible 不是 go-lang-stack: https://github.com/areski/ansible-golang-stack
五 遇到的坑
1. shell/command 模块无法调用source,需要用sh 方式执行
参考 https://groups.google.com/forum/#!topic/ansible-project/PNNpxY4ItSo
-source added profile sh /etc/profile.d/go-path.sh yes
参考资料:
https://serversforhackers.com/c/an-ansible-tutorial
http://getansible.com/advance/best_practice/zui_jia_shi_yong_fang_fa
页:
[1]