一:安裝ansible
mac:
1. 安裝 Homebrew (get the installation command from the Homebrew website).
2. 安裝Python 2.7.x ( brew install python ).
3. 安裝 Ansible ( sudo pip install ansible ).
linux:
如果系統中安裝了python-pip和python-devel,你可以使用pip安裝ansible(假設你已經安裝了開發工具包Development Tools)
$ sudo pip install ansible
Fedora/RHEL/CentOS:
可以使用系統自帶的yum管理工具
$ yum -y install ansible
Debian/Ubuntu:
使用系統自帶的apt管理工具
$ sudo apt-add-repository -y ppa:ansible/ansible $ sudo apt-get update $ sudo apt-get install -y ansible
查看ansibe版本:
$ ansible --version ansible 1.9.2
二:創建基本的資源文件
資源文件包含你所要管理的服務器列表
創建/etc/ansible/hosts(默認ansible資源文件路徑),添加一個測試服務器
$ sudo mkdir /etc/ansible $ sudo touch /etc/ansible/hosts
hosts內容如下:
[example]
10.0.0.132 # 這裏是你要管理的服務器地址或者是域名
三:運行你的第一個AD-HOC ansible 命令
$ ansible example -m ping
此時提示需要主機驗證和提示輸入用戶密碼,按ctrl + c取消
下面使用ssh-keygen 生成祕鑰,使用公鑰驗證。避免提示輸入用戶密碼
$ ssh-keygen #生成證書 $ ssh-copy-id -i [email protected] #上面資源文件裏的地址
此時再次運行ansible,顯示
root@~# ansible example -m ping 10.0.0.132 | success >> { "changed": false, "ping": "pong" }
運行查看內存使用情況的命令
root@~# ansible example -a "free -m" 10.0.0.132 | success | rc=0 >> total used free shared buffers cached Mem: 482 216 266 0 14 95 -/+ buffers/cache: 105 376 Swap: 1983 0 1983
總結:
第一天,到此爲止,你已經學會了配置和管理ansible,安裝ansible,運行常用的ansible命令查看服務器情況。