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:可實現多級指揮。
ansible特點
(1)、輕量級,無需在客戶端安裝agent,更新時,只需在操作機上進行一次更新即可;
(2)、批量任務執行可以寫成腳本,而且不用分發到遠程就可以執行;
(3)、使用python編寫,維護更簡單,ruby語法過於復雜;
(4)、支持sudo。
環境准備
[root@node1 ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
[root@node1 ~]# uname -r 3.10.0-229.el7.x86_64
主機名 IP node1 10.0.0.21 node2 10.0.0.22 node3 10.0.0.23 node4 10.0.0.24
安裝ansible(10.0.0.21)
配置所有節密鑰互信, 在node01
可以免密碼登錄各節點,只在node01
上執行
配置epel源
[root@node1 opt]# yum install -y wget
[root@node1 opt]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安裝
[root@node1 opt]# yum install -y ansible
查看版本
[root@node1 opt]# ansible --version
查看ansible 安裝生成的文件
[root@10.0.0.21 opt]# rpm -ql ansible |more /etc/ansible /etc/ansible/ansible.cfg #配置文件 /etc/ansible/hosts #主要文件
hosts文件詳解
[root@10.0.0.21 opt]# cat /etc/ansible/hosts # This is the default ansible 'hosts' file. # # It should live in /etc/ansible/hosts # # - Comments begin with the '#' character #注釋為# # - Blank lines are ignored #空白行被忽略 # - Groups of hosts are delimited by [header] elements #主機組名被寫在[]里面 # - You can enter hostnames or ip addresses #你可以寫ip地址也可以寫hostnames # - A hostname/ip can be a member of multiple groups #一個主機可以被多個主機組包含
配置hosts文件
[root@node1 opt]# vim /etc/ansible/hosts
加入以下內容
[test] #模塊的名稱,可以順便寫
10.0.0.21
10.0.0.22
10.0.0.23
10.0.0.24
生成秘鑰
[root@node1 opt]# ssh-keygen
秘鑰分發
[root@node1 opt]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.21 [root@node1 opt]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.22 [root@node1 opt]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.23 [root@node1 opt]# ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.24 或 [root@node1 opt]# ssh-copy-id root@10.0.0.21 [root@node1 opt]# ssh-copy-id root@10.0.0.22 [root@node1 opt]# ssh-copy-id root@10.0.0.23 [root@node1 opt]# ssh-copy-id root@10.0.0.24
測試是否連通
[root@node1 opt]# ansible -m ping 10.0.0.21
登錄驗證(Ctrl+D 退出連接)
[root@node1 opt]# ssh 10.0.0.21 Last login: Sun Aug 20 13:24:41 2017 from 10.0.0.20 welcome to my service! [root@node2 opt]#