ansible

ansible中文站點:ansible中文網站

一、ansible是自動化運維工具,基於python開發,實現了批量系統配置、批量程序部署、批量運行命令等功能,默認通過SSH協議管理機器。目前需要主機上有python2.6或2.7就可以運行。

  • ansible的特點:
    --模塊化設計
    --僅需要ssh和Python即可以使用
    --無客戶端
    --功能強大,模塊豐富
    --上手容易門檻低
    --基於python開發,做二次開發更容易
    --使用公司較多,社區活躍
  • 工作流程:
    ansible
  • 架構:
    ansible
    --Ansible: Ansible核心程序。
    --HostInventory:記錄由Ansible管理的主機信息,包括端口、密碼、ip等。
    --CoreModules:核心模塊,主要操作是通過調用核心模塊來完成管理任務。
    --CustomModules:自定義模塊,完成核心模塊無法完成的功能,支持多種語言。
    --ConnectionPlugins:連接插件,Ansible和Host通信使用
  • hosts文件:主機清單文件
    環境:6臺虛擬機
    1 管理機器:ansible 192.168.1.10
    2託管機器:web1 192.168.1.11
    3託管機器:web2 192.168.1.12
    4託管機器:db1 192.168.1.21
    5託管機器:db2 192.168.1.22
    6託管機器:cache 192.168.1.33
  • 在管理節點配置yum源,安裝ansible
  • ansible配置文件是 ansible.cfg
  • ansible查找配置文件的順序是:
    1、ANSIBLE_CONF變量定義的配置文件
    2、當前目錄下的./ansible.cfg
    3、當前用戶家目錄下的~/ansible.cfg
    4、/etc/ansible/ansible.cfg(默認配置文件)
    ansible.cfg中inventory行定義年託管主機列表文件。
  • 編輯/etc/ansible/hosts文件
    格式:
    [組名稱]
    主機名稱或ip地址,登彔用戶名,密碼、端口等信息
  • 測試
    ansible [組名稱] --list-hosts
  • 主機定義與分組
    inventory 參數說明
    – ansible_ssh_host
    – 將要連接的遠程主機名.
    – ansible_ssh_port
    – ssh端口號.如果不是默認的端口號,通過此變量設置.
    – ansible_ssh_user
    – 默認的 ssh 用戶名
    – ansible_ssh_pass
    – ssh 密碼(這種方式並並安全,我們強烈建議使用 --ask-pass 或 SSH 密鑰)
    – ansible_sudo_pass
    – sudo 密碼(建議使用 --ask-sudo-pass)
    – ansible_sudo_exe (new in version 1.8)
    – sudo 命令路徑(適用於1.8及以上版本)
    – ansible_connection
    – 與主機的連接類型.比如:local, ssh 或者 paramiko.Ansible 1.2 以前默認使用 paramiko.1.2 以後默認使用 'smart','smart' 方式會根據是否支持ControlPersist, 來判斷'ssh' 方式是否可行.
    – ansible_ssh_private_key_file
    – ssh 使用的私鑰文件.適用於有多個密鑰,而你不想使用SSH 代理的情況.
    ansible_shell_type
    --目標系統的shell類型.默認情況下,命令的執行使用 'sh'用法,可設置爲 'csh' 或 'fish'.
    – ansible_python_interpreter
    – 目標主機的 python 路徑.適用於的情況: 系統中有多個Python, 或者命令路徑不是"/usr/bin/python”
  • hosts文件編輯:定義一個爲web組,db組,cache組,
    [web]
    web1
    web2
    [db]
    db[1:2]
    [cache]
    192.168.1.16
    [app1:children] 指定子組
    web
    db
    [app1:vars] #組的變量 這個組的主機都使用如下配置
    ansible_ssh_user="root"
    ansible_ssh_pass="1"自定義配置文件
  • 自定義分組
    – 創建文件夾 myansible
    – 創建配置文件 ansible.cfg
    [defaults]
    inventory = myhost
    – 配置主機文件
    [nginx]
    192.168.1.11
    192.168.1.12
    192.168.1.13
    – ansible nginx --list-hosts
  • 動態主機
    – Ansible Inventory實際上是包含靜態Inventory和動態Inventory兩部分,靜態Inventory指的是在文件/etc/ansible/hosts中指定的主機和組,DynamicInventory指通過外部腳本獲取主機列表,並按照ansible 所要求的格式返回給ansilbe命令的。
    • json
    – JSON的全稱是”JavaScript Object Notation”,意思是JavaScript對象表示法,它是一種基於文本,獨立於語言的輕量級數據交換格式。
    注意事項:
    – 1、主機部分必須是列表格式的;
    – 2、hostdata行,其中的"hosts" 部分可以省略,但如
    果使用時,必須是"hosts"
    #!/bin/bash
    echo '
    {
    "web"   : ["web1", "web2"],
    "db"    : ["db1", "db2"],
    "other" : ["cache"]
    }'
  • ansible命令基礎
    ansible <host-pattern> [options]
    – host-pattern 主機或定義的分組
    – -M 指定模塊路徑
    – -m 使用模塊,默認 command 模塊
    – -a or --args 模塊參數
    – -i inventory 文件路徑,或可執行腳本
    – -k 使用交互式登彔密碼
    – -e 定義變量
    – -v 詳細信息,-vvvv 開啓debug 模式
  • 批量檢測主機
    – ansible all -m ping
  • 批量執行命令
    – ansible all -m command -a 'id' -k
  • 批量部署密鑰
    生成密鑰:ssh-keygen -N ‘’ -f /root/.ssh
    ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key='$(&lt;/root/.ssh/authorized_keys)'" -k
    報錯
    – "msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this.Please add this host's fingerprint to your known_hosts file to manage this host."
    – 解決方法:
    – 修改 ansible.cfg
    host_key_checking = False
  • 模塊
    ansible-doc
    – 模塊的手冊,相當於 shell 的 man 非常重要,非常重要,非常重要
    – ansible-doc -l 列出所有模塊
    – ansible-doc modulename 查看幫助
  • command模塊
    – 默認模塊,遠程執行命令
    – 用法
    – ansible host-pattern -m command -a '[args]'
    – 查看所有機器負載
    ansible all -m command -a 'uptime'
    – 查看日期和時間
    ansible all -m command -a 'date +%F_%T'模塊
    • command模塊注意事項:
    – 該模塊通過-a跟上要執行的命令可以直接執行,不過命令裏如果有帶有如下字符部分則執行不成功
    – "<", ">", "|", "&"
    – 該模塊不啓動shell 直接在 ssh 過程中執行,所有使用到 shell 特性的命令執行都會失敗
    – 下列命令執行會失敗
    ansible all -m command -a 'ps aux|grep ssh'
    ansible all -m command -a 'set'
    • shell | raw 模塊
    – shell 模塊用法基本和command一樣,區別是 shell模塊是通過/bin/sh迚行執行命令,可以執行任意命令
    – raw模塊,用法和shell 模塊一樣 ,可以執行任意命令
    – 區別是 raw 沒有chdir、creates、removes參數
    – 執行以下命令查看結果
    ansible t1 -m command -a 'chdir=/tmp touch f1'
    ansible t1 -m shell -a 'chdir=/tmp touch f2'
    ansible t1 -m raw -a 'chdir=/tmp touch f3' 在當前目錄下執行的
    • script模塊
    – 直接在本地寫腳本,然後使用 script 模塊批量執行
    – ansible t1 -m script -a 'urscript'
    – 友情提示: 該腳本包含但不限於 shell 腳本,只要指定 Sha-bang 解釋器的腳本都可運行
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章