https://www.cnblogs.com/wangxiaoqiangs/p/5685239.html
Ansible 運維自動化 ( 配置管理工具 )
安裝 Ansible
yum -y install ansible
配置 Ansible
shell > ls /etc/ansible # ansible.cfg 是 Ansible 工具的配置文件;hosts 用來配置被管理的機器;roles 是一個目錄,playbook 將使用它
ansible.cfg hosts roles
- Ansible 管理機與被管理機做秘鑰認證
shell > ssh-keygen # 生成秘鑰
shell > ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 22 root@192.168.12.129" # 將公鑰寫入被管理機
- hosts 文件添加被管理機
shell > > /etc/ansible/hosts
shell > vim /etc/ansible/hosts
[Client]
192.168.12.129
測試 Ansible
shell > ansible Client -m ping # 操作 Client 組 ( all 為操作 hosts 文件中所有主機 ),-m 指定執行 ping 模塊,下面是返回結果
192.168.12.129 | SUCCESS => {
"changed": false,
"ping": "pong"
}
- -i 指定 hosts 文件位置
- -u username 指定 SSH 連接的用戶名
- -k 指定遠程用戶密碼
- -f 指定并發數
- -s 如需要 root 權限執行時使用 ( 連接用戶不是 root 時 )
- -K -s 時,-K 輸入 root 密碼
附加
- /etc/ansible/hosts 文件
Ansible 定義主機、組規則的配置文件
www.abc.com # 定義域名
192.168.1.100 # 定義 IP
192.168.1.150:37268 # 指定端口號
[WebServer] # 定義分組
192.168.1.10
192.168.1.20
192.168.1.30
[DBServer] # 定義多個分組
192.168.1.50
192.168.1.60
Monitor ansible_ssh_port=12378 ansible_ssh_host=192.168.1.200 # 定義別名
ansible_ssh_host 連接目標主機的地址
ansible_ssh_port 連接目標主機的端口,默認 22 時無需指定
ansible_ssh_user 連接目標主機默認用戶
ansible_ssh_pass 連接目標主機默認用戶密碼
ansible_ssh_connection 目標主機連接類型,可以是 local 、ssh 或 paramiko
ansible_ssh_private_key_file 連接目標主機的 ssh 私鑰
ansible_*_interpreter 指定采用非 Python 的其他腳本語言,如 Ruby 、Perl 或其他類似 ansible_python_interpreter 解釋器
[webservers] # 主機名支持正則描述
www[01:50].example.com
- [dbservers]
db-[a:f].example.com