ansible工做原理及使用

​ansible是基於 Python paramiko 開發,分佈式,無需客戶端,輕量級,配置語法使用 YMAL 及 Jinja2模板語言,更強的遠程命令執行操做。linux

架構圖及工做邏輯

​ Ansible 在管理節點將 Ansible 模塊經過 SSH 協議(或者 Kerberos、LDAP)推送到被管理端執
行,執行完以後自動刪除,能夠使用 SVN 等來管理自定義模塊及編排。web

image.png

  • Ansible:核心
  • Modules:包括 Ansible 自帶的核心模塊及自定義模塊.
  • Plugins:完成模塊功能的補充,包括鏈接插件、郵件插件等.
  • Playbooks:網上不少翻譯爲劇本,我的以爲理解爲編排更爲合理;定義 Ansible 多任務配置文件,有 Ansible 自動執行.
  • Inventory:定義 Ansible 管理主機的清單

經常使用模塊

ansible提供上千個模塊,經常使用的模塊就一小部分列出經常使用模塊一些shell

linux

#拷貝本地的/etc/hosts 文件到 atlanta 主機組全部主機的/tmp/hosts
ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"  
#file 模塊容許更改文件的用戶及權限和建立文件
ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"
ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan  state=directory「 
#service更改服務狀態
ansible webservers -m service -a "name=httpd state=started"
#shell模塊遠程執行命令
ansible raleigh -m shell -a 'echo $TERM'
#yum模塊rpm軟件包管理
ansible webservers -m yum -a "name=acme state=present"
#user 模塊對於建立新用戶和更改、刪除已存在用戶
ansible all -m user -a "name=foo password=<crypted password here>"
ansible all -m user -a "name=foo state=absent"

window

  • win_feature : 安裝和卸載功能.
  • win_get_url : 從給定的 url 下載文件
  • win_group : 添加和刪除本地組
  • win_msi : 安裝和卸載 MSI 文件
  • win_ping : windows 版本的 ping 模塊
  • win_service : 管理 windows 服務
  • win_stat : 返回關於 windows 文件的信息
  • win_user : 管理本地帳號

playbook

​Playbooks 是 Ansible 管理配置、部署應用和編排的語言,能夠使用 Playbooks 來描述你想在遠
程主機執行的策略或者執行的一組步驟過程等apache

  • Playbooks 組成:windows

    • Target section
      定義將要執行 playbook 的遠程主機組
    • Variable section
      定義 playbook 運行時須要使用的變量
    • Task section
      定義將要在遠程主機上執行的任務列表
    • Handler section
      定義 task 執行完成之後須要調用的任務

例子:架構

- hosts: webservers
    vars:
        http_port: 80
        max_clients: 200
        remote_user: root
    tasks:
    - name: ensure apache is at the latest version
        yum: pkg=httpd state=latest
    - name: write the apache config file
        template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    notify:
        - restart apache
    - name: ensure apache is running
        service: name=httpd state=started
    handlers:
    - name: restart apache
        service: name=httpd state=restarted
相關文章
相關標籤/搜索