ansible 基本使用-1
阿新 • • 發佈:2020-07-10
概述
ansible 當前主流的批量配置管理工具,相比於saltstack 它是無agent 模式,基於ssh 去遠端管理主機。有密碼和金鑰兩種方式遠端認證方式。
安裝
yum -y install ansible (前提是有epel 源,如果沒有需要安裝)
pip install ansible
github原始碼包安裝
環境
服務端
python 2.6/2.6.3.x
openssl
centos debian redhat
被管理端
openssl python2.6/2.6.3.x
ansible 元件
playbook #劇本
inventory #主機清單
modules #功能模組
api #介面
常見的配置
[defaults] # some basic default values... #inventory = /etc/ansible/hosts #預設的主機清單檔案 #forks = 5 #併發執行任務數量,預設5#host_key_checking = False #是否驗證遠端主機的指紋資訊,如果要禁用驗證需要去掉註釋 #sudo_user= root #是否提權,新版本用的是 become = root #timeout = 10 #ssh 超時時間 #remote_port = 22 #log_path = /var/log/ansible.log #日誌路徑,啟用的話,註釋去掉
#private_key_file = /path/to/file #金鑰登入方式,預設關閉
inventory 使用配置
######## Ex 1: 未分組的主機清單,預設屬於all 組,可以使用主機名也可以使用ip ## green.example.com ## blue.example.com ##192.168.100.1 ## 192.168.100.10 ######## Ex 2: 根據主機使用者分組,例如一下清單主機都屬於webservers ## [webservers] ## alpha.example.org ## beta.example.org ## 192.168.1.100 ## 192.168.1.110 # 如果你的主機名字有規律的,可以通過這種方式表示多個主機www.001.example.com www.002.example.com ...
## www[001:006].example.com
######## Ex 3: 資料庫組
## [dbservers] ##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56 ## 10.25.1.57
變數的使用
可以通過給主機或者主機組定義變數,在遠端執行命令的時候使用該變數
vi /etc/ansible/hosts
##k8s-node [node] 10.11.118.164 ansible_ssh_user=root ansible_ssh_pass=asdf http_port=80 #針對主機新增變數 10.11.118.165 ansible_ssh_user=root ansible_ssh_pass=asdf [node:vars] #針對主機組新增變數 http_port=6443
ansible node -a "echo {{http_port}}"
tips:主機後面的變數優先順序大於主機組中相同變數值
vi /etc/ansible/group_vars/etcd.yml #以主機組名字名命的檔案內定義變數
server_name: k8s-etcd
ansible etcd -a "echo {{server_name}}"
tips: 當兩個主機組中的主機有重複,然後兩個主機組的變數定義的不同,那麼前面第一次生效的變數不會被後面定義的覆蓋