ansible概述、安裝、命令基礎
ansible概述
Ansible是2013年推出的一款IT自動化和DevOps軟件,目前由Redhat已簽署Ansible收購協議。其是基於Python研發,糅合了很多老運維工具的優點。實現了批量操作系統配置,批量程序的部署,批量運行命令等功能。
為什麽要選擇ansible?
? ansible優點
是僅需要ssh和Python即可使用
無客戶端
? ansible功能強大,模塊豐富
? 上手容易門檻低
? 基於 python 開發,做二次開發更容易
? 使用公司比較多,社區活躍
ansible安裝
環境準備:
virsh制作6臺虛擬機(2G內存、2CPU、20G硬盤、root密碼為123456)
- 1臺管理機ansible(192.168.4.10)
- 另外5臺虛擬機:
- web1 (192.168.4.11)
- web2 (192.168.4.12)
- db3 (192.168.4.13)
- db4 (192.168.4.14)
- cache5 (192.168.4.15)
[root@ansible ~]# cat /etc/yum.repos.d/local.repo //自定義ansible的安裝yum源
[public]
name=public
baseurl=ftp://192.168.4.254/public //真實機ftp有ansible的rpm包及依賴包
enabled=1
gpgcheck=0
[centos7]
name=centos7
baseurl=ftp://192.168.4.254/centos7
enabled=1
gpgcheck=0
[root@ansible ~]# vim /etc/hosts //設置6臺機器IP與主機名對應關系
[root@ansible ~]# yum -y install ansible //使用yum安裝ansible(安裝方式有源碼編譯、yum、pip等)
[root@ansible ~]# ansible --version //查看ansible版本
主機定義與分組
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim ansible.cfg //修改ansible配置文件
inventory = /etc/ansible/hosts //取消註釋
[root@ansible ansible]# vim hosts //設置需要管理的主機的分組
[web]
web1
web2
[db]
db3
db4
[cache]
cache5
[root@ansible ansible]# ansible web,db --list-hosts //列出分組中的主機
hosts (4):
web1
web2
db3
db4
[root@ansible ansible]# ansible all --list-hosts //列出所有主機
hosts (5):
web1
web2
cache5
db3
db4
ssh交互式密碼登錄遠程主機
[root@ansible ansible]# vim ansible.cfg
:61
host_key_checking = False //取消註釋
[root@ansible ansible]# ansible web -m ping -k //交互式登錄
SSH password: //輸入密碼123456
web2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
web1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ssh非交互式登錄遠程主機
(先修改了db3、db4的root密碼分別為a、b)
[root@ansible ansible]# vim hosts
[db]
db3 ansible_ssh_pass="a"
db4 ansible_ssh_pass="b"
[root@ansible ansible]# ansible db -m ping //非交互式登錄
db4 | SUCCESS => {
"changed": false,
"ping": "pong"
}
db3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
註:ansible建立的是長連接。即客戶端和服務端只用一個Socket對象,長期保持Socket的連接。socket文件在/root/.ansible/cp/
[root@ansible ansible]# ls /root/.ansible/cp/
6b13a69203
設置參數、子組
[root@ansible ansible]# vim hosts
[web]
web[1:2]
[web:vars] //參數
ansible_ssh_pass="123456"
[db]
db3
db4
[app:children] //子組
web
db
[cache]
cache5
ansible命令基礎
命令格式:
ansible 主機或分組 -m 模塊名 -a 模塊的參數
-M 指定模塊路徑
-m 使用模塊,默認 command 模塊
-a or --args 模塊參數
-i inventory 文件路徑,或可執行腳本
-k 使用交互式登錄密碼
-e 定義變量
-v 詳細信息,-vvvv 開啟 debug 模式
例如:
[root@ansible ~]# ansible web -m command -a ‘uptime‘
web2 | SUCCESS | rc=0 >>
09:00:52 up 12 min, 1 user, load average: 0.14, 0.15, 0.18
web1 | SUCCESS | rc=0 >>
09:00:52 up 12 min, 1 user, load average: 0.00, 0.13, 0.19
批量部署SSH密鑰登錄
原因:
? 交互輸入密碼較麻煩
? 密碼寫入配置文件安全性很差
? 不同遠程主機如果密碼不同,配置文件將很復雜
步驟:
給所有主機部署公鑰:
[root@ansible ~]# vim /etc/ansible/hosts //先刪除之前實驗設置的密碼
[root@ansible ~]# cd /root/.ssh/
[root@ansible .ssh]# ssh-keygen -t rsa -b 2048 -N ‘ ‘ //生成一對密鑰。-t type 指定密鑰算法,-b bits 指定密鑰長度,對於RSA密鑰,最小要求768位,默認是2048位。 -N new_passphrase 提供一個新的密語。
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:frfCsFFWF9bjKOrFpyGncn/8NBJTzbwqr1xEPb6BKPg root@ansible
The key‘s randomart image is:
+---[RSA 2048]----+
| oo |
| ..o+o|
| . oo+=|
| . o.o.+.o|
| .S+o..= + |
| .+o.=..+o|
| oE*.*+ +|
| ..=+o++o.|
| o .=+... |
+----[SHA256]-----+
[root@ansible .ssh]# ls
authorized_keys id_rsa id_rsa.pub known_hosts
[root@ansible .ssh]# ansible all -m authorized_key -a "user=root exclusive=true manage_dir=true key=‘$(</root/.ssh/id_rsa.pub)‘" -k //批量給所有主機部署SSH密鑰
SSH password: //輸入root密碼(所有主機密碼一樣)
......
若報錯
"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概述、安裝、命令基礎