一、介紹
1.自動化運維工具對比
1.Puppet:基於 Ruby 開發,採用 C/S 架構,擴展性強,基於 SSL,遠程命令執行相對較弱
2.SaltStack:基於 Python 開發,採用 C/S 架構,相對 puppet 更輕量級,配置語法使用 YAML,使得配置腳本更簡單.需要配置客戶端以及服務器端。每臺被控制節點需要安裝agent
3.Ansible:基於 Python開發,分佈式,無需客戶端,輕量級,配置語法使用YAML語言,更強的遠程命令執行操作
2.ansible簡介
ansible是新出現的自動化運維工具,基於Python開發,分佈式,無需客戶端,輕量級,實現了批量系統配置、批量程序部署、批量運行命令等功能,ansible是基於模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。
Ansible特性
1)、no agents:不需要在被管控主機上安裝任何客戶端,更新時,只需在操作機上進行一次更新即可(不用安裝客戶端。分佈式的)
2)、no server:無服務器端,使用時直接運行命令即可
3)、modules in any languages:基於模塊工作,可使用任意語言開發模塊
4)、yaml,not code:使用yaml語言定製劇本playbook
5)、ssh by default:基於SSH工作
6)、strong multi-tier solution:可實現多級指揮
connection plugins:連接插件,負責和被監控端實現通信,默認使用SSH連接
host inventory:主機清單,是一個配置文件裏面定義監控的主機
modules : 模塊,核心模塊、command模塊、自定義模塊等
plugins : modules功能的補充,包括連接插件,郵件插件等
playbook:編排,定義 Ansible 多任務配置文件,非必需
二、ansible安裝
1、 準備環境----關閉防護牆和selinux
環境:
主機:4臺 一個控制節點 3個被控制節點
解析:本地互相解析(所有機器)
# vim /etc/hosts
192.168.1.10 ansible-web1
192.168.1.11 ansible-web2
192.168.1.12 ansible-web3
192.168.1.9 ansible-server (控制節點服務器端)
配置ssh公鑰認證:控制節點需要發送ssh公鑰給所有被控制節點
[root@ansible-server ~]# ssh-keygen (生成祕鑰隊)
[root@ansible-server ~]# ssh-copy-id -i 192.168.1.10 #所有機器
然後ssh 192.168.1.10 可以免密登錄了
所有機器:
systemctl stop firewalld && setenforce 0
2、安裝
安裝:控制節點
1. 配置EPEL網絡yum源
[root@ansible-server ~]# yum install -y epel-release (安裝源不安裝也行)
2. 安裝ansible
[root@ansible-server ~]# yum install -y ansible
3.查看版本
[root@ansiable-server ~]# ansible --version
4.看幫助
[root@ansible-server ~]# ansible --help
3、ansible基礎----inventory主機清單
inventory文件通常用於定義要管理主機的認證信息,例如ssh登錄用戶名、密碼以及key相關信息。
查看配置文件:
[root@ansible-server ~]# rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
-q:---query查詢
1.主配置文件:
/etc/ansible/ansible.cfg #主要設置一些ansible初始化的信息,比如日誌存放路徑、模塊、等配置信息
2.主機清單文件:
默認位置/etc/ansible/hosts
語法:
1.添加主機或者主機組:
[root@ansible-server ~]# vim /etc/ansible/hosts #在最後追加被管理端的機器
ansible-web1 #單獨指定主機,可以使用主機名稱或IP地址
2.添加主機組:
[webservers] #使用[]標籤指定主機組 ----標籤自定義
192.168.10.11 #如果未解析添加ip
ansible-web2 #解析添加主機名
3.組可以包含其他組:
[webservers1] #組一
ansible-web1
[webservers2] #組二
ansible-web2
[weball:children] #children-照寫 #weball包括兩個子組
webservers1 #組一
webservers2 #組二
4.爲一個組指定變量,組內每個主機都可以使用該變量:
[weball:vars] #設置變量,vars--照寫
ansible_ssh_port=22
ansible_ssh_user=root
ansible_ssh_private_key_file=/root/.ssh/id_rsa
#ansible_ssh_pass=1 #也可以定義密碼,如果沒有互傳祕鑰可以使用密碼。
Ansible Inventory 常見的內置參數:
查看組內主機列表:
語法:ansible 組名 --list-hosts
[root@ansible-server ~]# ansible weball --list-hosts
hosts (2):
ansible-web1
ansible-web2
====================================
擴展:自定義主機列表使用密碼登錄:(瞭解)
[root@ansible-server ~]# vim /opt/hostlist
[all:vars]
ansible_ssh_port=22
ansible_ssh_user=root
#ansible_ssh_private_key_file=/root/.ssh/id_rsa
ansible_ssh_pass=test
[all]
ansible-web1
ansible-web2
使用:
[root@ansible-server ~]# ansible -i /opt/hostlist all -m ping -o
小注釋:如果不通,手動連接第一次,第一次需要手動輸入密碼。"第一次"
-i:指定清單文件
注意:這裏的ping並不是真正意義上的ping而是探測遠程主機ssh是否可以連接!判斷ssh端口是否存活
4、測試
語法:
# ansible <pattern> -m <module_name> -a <arguments>
pattern--主機清單裏定義的主機組名,主機名,IP,別名等,all表示所有的主機,支持通配符,正則
-m module_name: 模塊名稱,默認爲command
-a arguments: 傳遞給模塊的參數
-o 橫着顯示(單行顯示)
使用案例:
使用ping模塊檢查ansible節點的連通性:
1.指定單臺機器:
[root@ansible-server ~]# ansible ansible-web1 -m ping -o
ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
2.同時指定多臺機器:
[root@ansible-server ~]# ansible ansible-web1,ansible-web2 -m ping -o
ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
ansible-web2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
3.指定組名:
[root@ansible-server ~]# ansible webservers1 -m ping -o
ansible-web1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "ping": "pong"}
執行shell命令:
[root@ansible-server ~]# ansible webservers1 -m shell -a 'uptime'
ansible-web1 | CHANGED | rc=0 >>
23:32:47 up 5:24, 3 users, load average: 0.00, 0.01, 0.05
不加 -m 默認是 command 模塊
[root@ansible-server ~]# ansible webservers1 -a 'uptime'
ansible-web1 | CHANGED | rc=0 >>
23:34:01 up 5:25, 3 users, load average: 0.16, 0.05, 0.06
執行shell命令:
[root@ansible-server ~]# ansible webservers1 -m shell -a 'uptime'
ansible-web1 | CHANGED | rc=0 >>
23:32:47 up 5:24, 3 users, load average: 0.00, 0.01, 0.05
不加 -m 默認是 command 模塊
[root@ansible-server ~]# ansible webservers1 -a 'uptime'
ansible-web1 | CHANGED | rc=0 >>
23:34:01 up 5:25, 3 users, load average: 0.16, 0.05, 0.06