Ansible Best Practices Summary

3840 ワード

ansibleベストプラクティスのまとめは、公式ドキュメントAnsible Best Practicesを参照してください.主にディレクトリ構造と一部の実践提案を紹介した.
コンテンツ組織:
  • 「roles」組織プロパティ
  • を使用
  • 推奨ディレクトリ構造-1
  • production                # inventory file for production servers
    staging                   # inventory file for staging 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      #  
  • 推奨ディレクトリ構造-2
  • はマルチ環境に適しており、環境間の変数が少ない
  • ファイル数が多く、
  • のメンテナンスが難しい
    inventories/
       production/
          hosts               # inventory file for production servers
          group_vars/
             group1           # here we assign variables to particular groups
             group2           # ""
          host_vars/
             hostname1        # if systems need specific variables, put them here
             hostname2        # ""
    
       staging/
          hosts               # inventory file for staging environment
          group_vars/
             group1           # here we assign variables to particular groups
             group2           # ""
          host_vars/
             stagehost1       # if systems need specific variables, put them here
             stagehost2       # ""
    
    library/
    filter_plugins/
    
    site.yml
    webservers.yml
    dbservers.yml
    
    roles/
        common/
        webtier/
        monitoring/
        fooapp/
    
  • 動的Inventory
  • を使用
  • はhostの用途(ロール)と、位置、機械室に基づいてgroups
  • を定義することを提案する.
    # 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_を使用vars/host_varsは変数
  • を設定する
    #         
    
    ---
    # 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
    
  • 最上階のプレイブックにはRoleしか含まれておらず、非常に短いです
  • site.ymlでは、別のplaybooks
  • のみを含むインフラストラクチャを定義します.
    ---
    # file: site.yml
    - include: webservers.yml
    - include: dbservers.yml
    
  • playbookにはRoles
  • のみが含まれています
    ---
    # file: webservers.yml
    - hosts: webservers
      roles:
        - common
        - webtier
    
  • Role組織TaskおよびHandler
  • を使用
    その他
  • マルチ環境使用ポリシー:異なる環境(本番またはテスト)は異なるinventoryプロファイルを使用し、-iを使用して対応する構成
  • を選択します.
  • は、stateがpresentまたはabsent
  • であるにもかかわらずmodulesの状態を示す.
  • 異なるキャラクタのhostをグループ化する
  • は、スペースを使用してコンテンツを区切ることを奨励し、注釈
  • を「#」で書く.
  • Tasksに命名または記述(name)
  • を追加する
  • Ansibleのすべての特性を一度に使用しようとしないで、あなたに役立つものだけを使用して、簡潔で簡単に保つことができます.
  • バージョン管理システムを使用してansibleスクリプト
  • を管理する