zoukankan      html  css  js  c++  java
  • [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文件

    [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
    - hosts: web
      vars:
       domain: 0731
      remote_user: root
      tasks:
      - name: test connection
        ping:
      - name: httpd check
        service: name=sshd state=started
      - name: remove foo.conf
        file: dest=/etc/foo.conf state=absent
        #- name: delete foo.conf
        #shell: rm -f /etc/foo.conf
    
      - name: copy foo.conf
        copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf
              owner=root group=root mode=064
        notify:
          - Verify Conf
    
      - name: Add template Config
        template: src=serversforhackers.com.j2 dest=/etc/{{ domain }}.conf owner=root group=root
    
      handlers:
      - name: Verify Conf
        shell: file /etc/foo.conf
    
    #Task2
    - hosts: server
      vars:
        sdomain: server_0731
        domain: 0731
      remote_user: root
      tasks:
      - name: Add template Config
        template: 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 https://{{ 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] *******************************************************************************************************************************************************************************************************************************************************
    ok: [10.0.0.4]
    ok: [10.0.0.3]
    
    TASK [test connection] *******************************************************************************************************************************************************************************************************************************************************
    ok: [10.0.0.4]
    ok: [10.0.0.3]
    
    TASK [httpd check] ***********************************************************************************************************************************************************************************************************************************************************
    ok: [10.0.0.3]
    ok: [10.0.0.4]
    
    TASK [remove foo.conf] *******************************************************************************************************************************************************************************************************************************************************
    changed: [10.0.0.3]
    changed: [10.0.0.4]
    
    TASK [copy foo.conf] *********************************************************************************************************************************************************************************************************************************************************
    changed: [10.0.0.4]
    changed: [10.0.0.3]
    
    TASK [Add template Config] ***************************************************************************************************************************************************************************************************************************************************
    ok: [10.0.0.4]
    ok: [10.0.0.3]
    
    RUNNING HANDLER [Verify Conf] ************************************************************************************************************************************************************************************************************************************************
    changed: [10.0.0.3]
    changed: [10.0.0.4]
    
    PLAY [server] ****************************************************************************************************************************************************************************************************************************************************************
    
    TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************************************************
    ok: [10.0.0.5]
    ok: [10.0.0.6]
    
    TASK [Add template Config] ***************************************************************************************************************************************************************************************************************************************************
    ok: [10.0.0.5]
    ok: [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 

       - name: source added profile
         shell: sh /etc/profile.d/go-path.sh
         ignore_errors: yes

    参考资料:

    https://serversforhackers.com/c/an-ansible-tutorial

    http://getansible.com/advance/best_practice/zui_jia_shi_yong_fang_fa

  • 相关阅读:
    QTP的那些事不能识别web上的测试对象
    ASP.NET 2.0 页面事件执行顺序 摘自http://www.cnblogs.com/chinadragon/archive/2009/11/21/1607761.html
    javascript replace方法的使用注意点
    HTML转义字符转载http://www.8189090.com/character/
    有无Global.asax文件对Session的影响
    四叉树索引引用自http://hi.baidu.com/geochenyj/blog/item/189f2fed07d041d6b31cb1b6.html
    GirdView固定行头
    CSS的常用滤镜(filter)属性及语句大全摘自http://www.8tops.com/113_skill_8267CEB6473B4AF1ABF669340E3AD2EF.htm
    Clone基类http://www.legalsoft.com.cn/docs/986.html
    自定义控件不能设置属性
  • 原文地址:https://www.cnblogs.com/carol2000/p/7264442.html
Copyright © 2011-2022 走看看