首先ansible基於python2.X 環境
默認centos都已經安裝好了python2環境html
ansible能夠經過源碼,yum,pip等方式安裝
本文采用pip安裝方式python
centos默認沒有安裝pip,這裏咱們要安裝pipgit
參考文獻:傳送門github
1 curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
2
3 python get-pip.py 4
5 [root@localhost ~]# pip -V 6 pip 19.0.2 from /usr/lib/python2.7/site-packages/pip (python 2.7)
yum install gcc glibc-devel zlib-devel rpm-build openssl-deve -y yum install -y python-devel
[root@localhost ~]# pip install ansible
[root@localhost ~]# ansible --version
ansible 2.7.7
config file = None
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
pip安裝是沒有config file文件的 咱們能夠將官網的默認文件上傳到服務器
官方文檔:傳送門
mkdir /etc/ansible touch /ect/ansible.cfg
建立後ansible會自動搜索,無需操做shell
[root@localhost ~]# ansible --version ansible 2.7.7 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
到這裏pip安裝ansible就算完成了,接下來來實現一些具體的操做bootstrap
主機說明
控制主機:10.0.15.59 用於控制其它機器的主機
管理主機:10.0.15.60 被控制的主機
管理主機:10.0.15.66 被控制的主機
A.在控制主機建立祕鑰
ssh-keygen -t rsa
在root/.ssh目錄下存在兩個文件
id_rsa是私鑰(不能外泄),id_rsa.pub這個是公鑰
B.將控制主機的公鑰遠程傳輸到管理主機
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.15.60
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.15.66
輸入密碼後查看管理主機的.ssh目錄下是否出現authorized_keys
C.測試免密是否成功
ssh root@10.0.15.60
ssh root@10.0.15.66
vim /etc/ansible/hosts
[group1]
10.0.15.60:2222
10.0.15.66
[group2]
10.0.15.66
A.方括號[]中是組名,用於對系統進行分類,便於對不一樣系統進行個別的管理.vim
B.一個系統能夠屬於不一樣的組centos
C.若是有主機的SSH端口不是標準的22端口,可在主機名以後加上端口號,用冒號分隔服務器
等等一些詳細的說明能夠查看:官方文檔app
[root@localhost ~]# ansible group1 -m ping
10.0.15.66 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.15.60 | SUCCESS => {
"changed": false,
"ping": "pong"
}
磁盤檢測
[root@localhost ~]# ansible all -m shell -a "df -hP|awk 'NR>1 && int(\$5) '"
10.0.15.60 | CHANGED | rc=0 >>
/dev/mapper/centos-root 9.8G 5.0G 4.9G 51% /
tmpfs 488M 7.7M 480M 2% /run
/dev/mapper/centos-home 8.1G 2.6G 5.5G 32% /home
/dev/sda1 197M 120M 77M 61% /boot
10.0.15.66 | CHANGED | rc=0 >>
/dev/mapper/centos-root 17G 1.1G 16G 7% /
tmpfs 488M 7.7M 480M 2% /run
/dev/sda1 1014M 130M 885M 13% /boot