1.確保機器上安裝的是 Python 2.6 或者 Python 2.7 版本:
python -V
2.查看yum倉庫中是否存在ansible的rpm包
yum list|grep ansible
若不存在或是低版本可更換yum源或者采用源碼安裝
阿里雲的yum源:http://mirrors.aliyun.com/repo/ 備份源文件,然后下載對應的版本至/etc/yum.repos.d/目錄即可,如epel-6.repo (通過該yum源安裝ansible會依賴python 2.6,如果python版本是2.7以上可能會安裝失敗)
3.安裝ansible服務:
yum install ansible -y
4.在服務器端:
執行ssh-keygen -t rsa生成密鑰
接着執行ssh-copy-id root@IP 輸入ip的秘密
最后執行ssh ip如果沒有提示輸入秘密就直接登入ip,說明免密登錄成功了
ansible目錄結構 /etc/ansible
deprecation_warnings = False (防止報錯)
ansible.cfg(配置文件) hosts(IP目錄分組) roles(目錄)
ansible命令
ansible test-server -m ping
格式:
ansbile 操作目標 -m 模塊名 -a 模塊參數
ansible test-server -m copy -a "src=/zhangjie.txt dest=/opt/owner=root group=root mode=0755 force=yes"
ansible IP組 指定參數 復制模塊 執行命令 源目錄 目的目錄 用戶 用戶組 權限 強制覆蓋=yes
1.copy模塊: 拷貝
ansible test-server -m copy -a "src=/zhangjie.txt dest=/opt/"
2.setup模塊: 獲取主機信息
ansible all -m setup
3.command模塊: 用於遠程執行命令(可以省略)
ansible test-server -a 'date'
4.cron模塊: 計划任務模塊
ansible db -m cron -a 'minute="" hour="" day="" month="" weekday="" job="" name="(必須填寫)" state"present"'
模塊 分 時 日 月 周 要執行的任務 介紹 state有兩個狀態:present(添加(默認值))or absent(移除)
5.file模塊: 創建文件目錄
ansible db -m file -a "path=/tmp/test state=touch"
6.yum模塊:yum安裝目錄
ansible web -m yum -a 'name="@Development tools" state=present'
7.service模塊:
ansible web -m service -a 'enabled=yes name=httpd state=started'
8.shell模塊: 命令模塊,比command好用一些
ansible web -m shell -a "ps -ef|grep httpd"
9.script模塊: 腳本執行模塊
ansible db -m script -a '/tmp/script.sh'
10.mount模塊:掛載模塊
ansible test -m mount 'name=/mnt src=/dev/loop0 fstype=ext4 state=mounted opts=rw'