设为首页 收藏本站
查看: 931|回复: 0

[经验分享] [ansible-playbook]4 持续集成环境之分布式部署利器 ansible playbook学习

[复制链接]

尚未签到

发表于 2018-1-3 06:26:55 | 显示全部楼层 |阅读模式
  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文件
[web]

  192.168.100.1
[server]

  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文件:
  

[web]  

10.0.0.3  
10.0.0.4
  
[server]
  
10.0.0.5
  
10.0.0.6
  
[gateway]
  
10.0.0.7
  

  执行脚本: # ansible-playbook -i host test.yml   (-i 表示指定读取的host文件路径)
  

PLAY [web] *******************************************************************************************************************************************************************************************************************************************************************  

  
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
  

[10.0.0.4]  

[10.0.0.3]  

  
TASK [
test connection] *******************************************************************************************************************************************************************************************************************************************************  

[10.0.0.4]  

[10.0.0.3]  

  
TASK [httpd check] ***********************************************************************************************************************************************************************************************************************************************************
  

[10.0.0.3]  

[10.0.0.4]  

  
TASK [remove foo.conf] *******************************************************************************************************************************************************************************************************************************************************
  

[10.0.0.3]  

[10.0.0.4]  

  
TASK [copy foo.conf] *********************************************************************************************************************************************************************************************************************************************************
  

[10.0.0.4]  

[10.0.0.3]  

  
TASK [
Add template Config] ***************************************************************************************************************************************************************************************************************************************************  

[10.0.0.4]  

[10.0.0.3]  

  
RUNNING HANDLER [Verify Conf] ************************************************************************************************************************************************************************************************************************************************
  

[10.0.0.3]  

[10.0.0.4]  

  
PLAY [server] ****************************************************************************************************************************************************************************************************************************************************************
  

  
TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
  

[10.0.0.5]  

[10.0.0.6]  

  
TASK [
Add template Config] ***************************************************************************************************************************************************************************************************************************************************  

[10.0.0.5]  

[10.0.0.6]  

  
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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-431012-1-1.html 上篇帖子: Ansible的基础元素和YAML介绍 下篇帖子: 自动化运维工之Ansible(1)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表