運維工具Ansible—主機分組管理(hosts)

ansible主機分組管理:


ansible配置shell

說明:關於ansible hosts文件中可用參數:bash

ansible_ssh_port=22                  #遠程主機登錄端口ssh

ansible_ssh_user=root                #遠程主機登錄用戶名ide

ansible_ssh_pass=chekir              #遠程主機登錄用戶名的密碼測試

ansible_ssh_private_key_file=/etc/ansible/hosts   #指定管理主機羣列表文件spa

host_key_checking=False              #跳過第一次鏈接檢測詢問是否登錄的提示(YES/NO)orm


這裏hosts文件我作了互信認證,因此hosts文件我就寫了個主機IP,端口號默認的話能夠不用寫it


一、備份ansible hosts文件:class

[root@soso ~]# cp /etc/ansible/hosts /etc/ansible/hosts.bak

二、清空hosts文件,定義主機分組test

[root@soso ~]# cat /etc/ansible/hosts
[test]
192.168.1.2

三、測試ansible

[root@soso ~]# ansible -i /etc/ansible/hosts test -m shell -a 'uptime'
192.168.1.2 | success | rc=0 >>
 14:28:04 up 21:12,  3 users,  load average: 0.01, 0.02, 0.00

[root@soso ~]# ansible test -i /etc/ansible/hosts  -m command -a 'date'         
192.168.1.2 | success | rc=0 >>
Wed Dec 16 14:30:40 CST 2015

#參數解釋:

-i:  指定主機列表文件        

-u: 指定遠程主機登錄用戶        

-m:指定使用ansible 的模塊                

-a: 指定模塊下使用的參數        

-k: 指定遠程登錄用戶的密碼


上面測試的命令還能夠簡寫:   

[root@soso ~]# ansible  test -a 'date'
192.168.1.2  | success | rc=0 >>
Wed Dec 16 14:33:53 CST 2015

測試遠程主機狀態,查看是否存活

[root@soso ~]# ansible all -m ping 
192.168.1.2  | success >> {
    "changed": false, 
    "ping": "pong"
}
相關文章
相關標籤/搜索